NULLCONN.CPP 13 KB

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