NULLCONN.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/NULLCONN.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /***************************************************************************
  16. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  17. ***************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : NULLCONN.CPP *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : April 5, 1995 *
  26. * *
  27. * Last Update : April 20, 1995 [DRD] *
  28. * *
  29. *-------------------------------------------------------------------------*
  30. * Functions: *
  31. * NullModemConnClass::NullModemConnClass -- class constructor *
  32. * NullModemConnClass::~NullModemConnClass -- class destructor *
  33. * NullModemConnClass::Init -- hardware-dependent initialization *
  34. * NullModemConnClass::Send -- hardware-dependent packet sending *
  35. * NullModemConnClass::Compute_CRC -- computes CRC for given buffer *
  36. * NullModemConnClass::Packet_Overhead_Size -- number of extra bytes *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "function.h"
  39. #ifdef WIN32
  40. #include "wincomm.h"
  41. #endif //WIN32
  42. #include <stdio.h>
  43. //#include <mem.h>
  44. #include "nullconn.h"
  45. /***************************************************************************
  46. * NullModemConnClass::NullModemConnClass -- class constructor *
  47. * *
  48. * INPUT: *
  49. * numsend desired # send queue entries *
  50. * numreceive desired # send receive entries *
  51. * maxlen max length of application's packets *
  52. * magicnum application-defined magic # for the packets *
  53. * *
  54. * OUTPUT: *
  55. * none. *
  56. * *
  57. * WARNINGS: *
  58. * none. *
  59. * *
  60. * HISTORY: *
  61. * 12/20/1994 BR : Created. *
  62. *=========================================================================*/
  63. NullModemConnClass::NullModemConnClass (int numsend, int numreceive,
  64. int maxlen, unsigned short magicnum) :
  65. ConnectionClass (numsend, numreceive, maxlen, magicnum,
  66. 60, // Retry Delta Time
  67. -1, // Max Retries (-1 means ignore this timeout parameter)
  68. 1200) // Timeout: 20 seconds
  69. {
  70. /*------------------------------------------------------------------------
  71. Pre-set the port value to NULL, so Send won't send until we've been Init'd
  72. ------------------------------------------------------------------------*/
  73. #ifdef WIN32
  74. PortHandle = NULL;
  75. #else //WIN32
  76. Port = NULL;
  77. #endif //WIN32
  78. /*------------------------------------------------------------------------
  79. Allocate the Send Buffer; the parent constructor has set MaxPacketLen,
  80. so we can use it in our computation.
  81. ------------------------------------------------------------------------*/
  82. SendBuf = new char [ Actual_Max_Packet() ];
  83. } /* end of NullModemConnClass */
  84. /***************************************************************************
  85. * NullModemConnClass::~NullModemConnClass -- class destructor *
  86. * *
  87. * INPUT: *
  88. * *
  89. * OUTPUT: *
  90. * none. *
  91. * *
  92. * WARNINGS: *
  93. * none. *
  94. * *
  95. * HISTORY: *
  96. * 12/20/1994 BR : Created. *
  97. *=========================================================================*/
  98. NullModemConnClass::~NullModemConnClass ()
  99. {
  100. delete [] SendBuf;
  101. } /* end of ~NullModemConnClass */
  102. /***************************************************************************
  103. * NullModemConnClass::Init -- hardware-dependent initialization *
  104. * *
  105. * INPUT: *
  106. * port GreenLeaf port handle *
  107. * *
  108. * OUTPUT: *
  109. * none. *
  110. * *
  111. * WARNINGS: *
  112. * none. *
  113. * *
  114. * HISTORY: *
  115. * 12/20/1994 BR : Created. *
  116. *=========================================================================*/
  117. #ifdef WIN32
  118. void NullModemConnClass::Init (HANDLE port_handle)
  119. {
  120. ConnectionClass::Init();
  121. PortHandle = port_handle;
  122. }
  123. #else //WIN32
  124. void NullModemConnClass::Init (PORT *port)
  125. {
  126. ConnectionClass::Init();
  127. Port = port;
  128. } /* end of Init */
  129. #endif //WIN32
  130. /***************************************************************************
  131. * NullModemConnClass::Send -- hardware-dependent packet sending *
  132. * *
  133. * INPUT: *
  134. * buf buffer to send *
  135. * buflen length of buffer to send *
  136. * extrabuf (not used by this class) *
  137. * extralen (not used by this class) *
  138. * *
  139. * OUTPUT: *
  140. * none. *
  141. * *
  142. * WARNINGS: *
  143. * 1 = OK, 0 = error *
  144. * *
  145. * HISTORY: *
  146. * 12/20/1994 BR : Created. *
  147. *=========================================================================*/
  148. int NullModemConnClass::Send (char *buf, int buflen, void *, int )
  149. {
  150. return 0;
  151. #if (0)//PG
  152. int *ibuf;
  153. SerialHeaderType *header;
  154. unsigned long sendlen;
  155. /*------------------------------------------------------------------------
  156. Error if we haven't been properly initialized
  157. ------------------------------------------------------------------------*/
  158. #ifdef WIN32
  159. if ( PortHandle == NULL ) return(false);
  160. #else //WIN32
  161. int status;
  162. if ( Port == NULL ) {
  163. return(0);
  164. }
  165. #endif //WIN32
  166. /*------------------------------------------------------------------------
  167. Package the data into the Send Buffer
  168. ------------------------------------------------------------------------*/
  169. header = (SerialHeaderType *) SendBuf;
  170. header->MagicNumber = PACKET_SERIAL_START;
  171. header->Length = (short) buflen;
  172. header->MagicNumber2 = PACKET_SERIAL_VERIFY;
  173. sendlen = sizeof( SerialHeaderType );
  174. memcpy (SendBuf + sendlen, buf, buflen);
  175. sendlen += buflen;
  176. ibuf = (int *)(SendBuf + sendlen);
  177. *ibuf = Compute_CRC( buf, buflen );
  178. sendlen += sizeof( int );
  179. *(SendBuf + sendlen) = '\r';
  180. sendlen += 1;
  181. /*------------------------------------------------------------------------
  182. Send the data
  183. ------------------------------------------------------------------------*/
  184. #ifdef WIN32
  185. SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
  186. return (true);
  187. #else //WIN32
  188. status = WriteBuffer( Port, SendBuf, sendlen );
  189. if ( status == ASSUCCESS ) {
  190. return(1);
  191. }
  192. else {
  193. return(0);
  194. }
  195. #endif //WIN32
  196. #endif//PG
  197. } /* end of Send */
  198. /***************************************************************************
  199. * NullModemConnClass::Compute_CRC -- computes CRC for given buffer *
  200. * *
  201. * INPUT: *
  202. * buf buffer to compute CRC for *
  203. * buflen length of buffer in bytes *
  204. * *
  205. * OUTPUT: *
  206. * none. *
  207. * *
  208. * WARNINGS: *
  209. * none. *
  210. * *
  211. * HISTORY: *
  212. * 12/20/1994 BR : Created. *
  213. *=========================================================================*/
  214. int NullModemConnClass::Compute_CRC (char *buf, int buflen)
  215. {
  216. unsigned int sum, hibit;
  217. sum = 0;
  218. for (int i = 0; i < buflen; i++) {
  219. if ( sum & 0x80000000 ) { // check hi bit to rotate into low bit
  220. hibit = 1;
  221. }
  222. else {
  223. hibit = 0;
  224. }
  225. sum <<= 1;
  226. sum += (hibit + (unsigned char)buf[i]);
  227. }
  228. return((int)sum);
  229. } /* end of Compute_CRC */
  230. /***************************************************************************
  231. * NullModemConnClass::Packet_Overhead_Size -- number of extra bytes *
  232. * *
  233. * INPUT: *
  234. * none. *
  235. * *
  236. * OUTPUT: *
  237. * number of bytes used for communications only. *
  238. * *
  239. * WARNINGS: *
  240. * none. *
  241. * *
  242. * HISTORY: *
  243. * 04/20/1995 DRD : Created. *
  244. *=========================================================================*/
  245. int NullModemConnClass::Packet_Overhead_Size ( void )
  246. {
  247. /*------------------------------------------------------------------------
  248. short for Null Modem Magic Number
  249. short for Null Modem length of packet
  250. int for Null Modem CRC check
  251. CommHeaderType for Queued packets
  252. ------------------------------------------------------------------------*/
  253. return( (PACKET_SERIAL_OVERHEAD_SIZE + sizeof(CommHeaderType)) );
  254. } /* end of Packet_Overhead_Size */
  255. /************************** end of nullconn.cpp ****************************/