NULLCONN.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. ** Command & Conquer(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: F:\projects\c&c\vcs\code\nullconn.cpv 1.10 16 Oct 1995 16:51:36 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. #include "wincomm.h"
  44. #include "tcpip.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. NonSequencedConnClass (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. PortHandle = NULL;
  74. /*------------------------------------------------------------------------
  75. Allocate the Send Buffer; the parent constructor has set MaxPacketLen,
  76. so we can use it in our computation.
  77. ------------------------------------------------------------------------*/
  78. // SendBuf = new char [MaxPacketLen + sizeof(int) * 3];
  79. SendBuf = new char [ Actual_Max_Packet() ];
  80. } /* end of NullModemConnClass */
  81. /***************************************************************************
  82. * NullModemConnClass::~NullModemConnClass -- class destructor *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * none. *
  88. * *
  89. * WARNINGS: *
  90. * none. *
  91. * *
  92. * HISTORY: *
  93. * 12/20/1994 BR : Created. *
  94. *=========================================================================*/
  95. NullModemConnClass::~NullModemConnClass ()
  96. {
  97. delete [] SendBuf;
  98. } /* end of ~NullModemConnClass */
  99. /***************************************************************************
  100. * NullModemConnClass::Init -- hardware-dependent initialization *
  101. * *
  102. * INPUT: *
  103. * port GreenLeaf port handle *
  104. * *
  105. * OUTPUT: *
  106. * none. *
  107. * *
  108. * WARNINGS: *
  109. * none. *
  110. * *
  111. * HISTORY: *
  112. * 12/20/1994 BR : Created. *
  113. *=========================================================================*/
  114. void NullModemConnClass::Init (HANDLE port_handle)
  115. {
  116. NonSequencedConnClass::Init();
  117. PortHandle = port_handle;
  118. } /* end of Init */
  119. /***************************************************************************
  120. * NullModemConnClass::Send -- hardware-dependent packet sending *
  121. * *
  122. * INPUT: *
  123. * port GreenLeaf port handle *
  124. * *
  125. * OUTPUT: *
  126. * none. *
  127. * *
  128. * WARNINGS: *
  129. * 1 = OK, 0 = error *
  130. * *
  131. * HISTORY: *
  132. * 12/20/1994 BR : Created. *
  133. *=========================================================================*/
  134. int NullModemConnClass::Send (char *buf, int buflen)
  135. {
  136. //int status;
  137. int *ibuf;
  138. SerialHeaderType *header;
  139. unsigned long sendlen;
  140. /*------------------------------------------------------------------------
  141. Error if we haven't been properly initialized
  142. ------------------------------------------------------------------------*/
  143. if ( PortHandle == NULL )
  144. return(false);
  145. /*------------------------------------------------------------------------
  146. Package the data into the Send Buffer
  147. ------------------------------------------------------------------------*/
  148. header = (SerialHeaderType *) SendBuf;
  149. header->MagicNumber = PACKET_SERIAL_START;
  150. header->Length = (short) buflen;
  151. header->MagicNumber2 = PACKET_SERIAL_VERIFY;
  152. sendlen = sizeof( SerialHeaderType );
  153. memcpy (SendBuf + sendlen, buf, buflen);
  154. sendlen += buflen;
  155. ibuf = (int *)(SendBuf + sendlen);
  156. *ibuf = Compute_CRC( buf, buflen );
  157. sendlen += sizeof( int );
  158. *(SendBuf + sendlen) = '\r';
  159. sendlen += 1;
  160. /*------------------------------------------------------------------------
  161. Send the data
  162. ------------------------------------------------------------------------*/
  163. //status =
  164. #ifdef FORCE_WINSOCK
  165. if (Winsock.Get_Connected() || GameToPlay == GAME_INTERNET){
  166. Winsock.Write(SendBuf, (int)sendlen);
  167. }else{
  168. SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
  169. }
  170. #else
  171. SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
  172. #endif //WINSOCK
  173. //if ( status == ASSUCCESS ) {
  174. return(true);
  175. //} else {
  176. // Smart_Printf( "Write Buffer status %d, Port->status %d, sendlen %d \n", status, Port->status, sendlen );
  177. // return(false);
  178. //}
  179. }
  180. /***************************************************************************
  181. * NullModemConnClass::Compute_CRC -- computes CRC for given buffer *
  182. * *
  183. * INPUT: *
  184. * buf buffer to compute CRC for *
  185. * buflen length of buffer in bytes *
  186. * *
  187. * OUTPUT: *
  188. * none. *
  189. * *
  190. * WARNINGS: *
  191. * none. *
  192. * *
  193. * HISTORY: *
  194. * 12/20/1994 BR : Created. *
  195. *=========================================================================*/
  196. int NullModemConnClass::Compute_CRC (char *buf, int buflen)
  197. {
  198. unsigned int sum, hibit;
  199. sum = 0;
  200. for (int i = 0; i < buflen; i++) {
  201. if ( sum & 0x80000000 ) { // check hi bit to rotate into low bit
  202. hibit = 1;
  203. } else {
  204. hibit = 0;
  205. }
  206. sum <<= 1;
  207. sum += (hibit + (unsigned char)buf[i]);
  208. }
  209. return((int)sum);
  210. }
  211. /***************************************************************************
  212. * NullModemConnClass::Packet_Overhead_Size -- number of extra bytes *
  213. * *
  214. * INPUT: *
  215. * none. *
  216. * *
  217. * OUTPUT: *
  218. * number of bytes used for communications only. *
  219. * *
  220. * WARNINGS: *
  221. * none. *
  222. * *
  223. * HISTORY: *
  224. * 04/20/1995 DRD : Created. *
  225. *=========================================================================*/
  226. int NullModemConnClass::Packet_Overhead_Size ( void )
  227. {
  228. //
  229. // short for Null Modem Magic Number
  230. // short for Null Modem length of packet
  231. // int for Null Modem CRC check
  232. // CommHeaderType for Queued packets
  233. //
  234. return( (PACKET_SERIAL_OVERHEAD_SIZE + sizeof(CommHeaderType)) );
  235. } /* end of Packet_Overhead_Size */