CONNECT.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: F:\projects\c&c\vcs\code\connect.cpv 1.9 16 Oct 1995 16:48:56 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 : CONNECT.CPP *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : December 20, 1994 *
  26. * *
  27. * Last Update : May 31, 1995 [BRR] *
  28. *-------------------------------------------------------------------------*
  29. * Functions: *
  30. * ConnectionClass::ConnectionClass -- class constructor *
  31. * ConnectionClass::~ConnectionClass -- class destructor *
  32. * ConnectionClass::Service -- main polling routine; services packets *
  33. * ConnectionClass::Time -- gets current time *
  34. * ConnectionClass::Command_Name -- returns name for a packet command *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "function.h"
  37. #ifdef WWLIB32_H
  38. #include "TIMER.H"
  39. #else
  40. #include <sys\timeb.h>
  41. #endif
  42. /*
  43. ********************************* Globals ***********************************
  44. */
  45. char *ConnectionClass::Commands[PACKET_COUNT] = {
  46. "ADATA",
  47. "NDATA",
  48. "ACK"
  49. };
  50. /***************************************************************************
  51. * ConnectionClass::ConnectionClass -- class constructor *
  52. * *
  53. * If either max_retries or timeout is -1, that parameter is ignored in *
  54. * timeout computations. If both are -1, the connection will just keep *
  55. * retrying forever. *
  56. * *
  57. * INPUT: *
  58. * numsend desired # of entries for the send queue *
  59. * numreceive desired # of entries for the recieve queue *
  60. * maxlen max length of an application packet *
  61. * magicnum the packet "magic number" for this connection *
  62. * retry_delta the time to wait between sends *
  63. * max_retries the max # of retries allowed for a packet *
  64. * (-1 means retry forever, based on this parameter) *
  65. * timeout the max amount of time before we give up on a packet *
  66. * (-1 means retry forever, based on this parameter) *
  67. * *
  68. * OUTPUT: *
  69. * none. *
  70. * *
  71. * WARNINGS: *
  72. * none. *
  73. * *
  74. * HISTORY: *
  75. * 12/20/1994 BR : Created. *
  76. *=========================================================================*/
  77. ConnectionClass::ConnectionClass (int maxlen, unsigned short magicnum,
  78. unsigned long retry_delta, unsigned long max_retries, unsigned long timeout)
  79. {
  80. /*------------------------------------------------------------------------
  81. Compute our maximum packet length
  82. ------------------------------------------------------------------------*/
  83. MaxPacketLen = maxlen + sizeof(CommHeaderType);
  84. /*------------------------------------------------------------------------
  85. Assign the magic number
  86. ------------------------------------------------------------------------*/
  87. MagicNum = magicnum;
  88. /*------------------------------------------------------------------------
  89. Initialize the retry time. This is the time that t2 - t1 must be greater
  90. than before a retry will occur.
  91. ------------------------------------------------------------------------*/
  92. RetryDelta = retry_delta;
  93. /*------------------------------------------------------------------------
  94. Set the maximum allowable retries.
  95. ------------------------------------------------------------------------*/
  96. MaxRetries = max_retries;
  97. /*------------------------------------------------------------------------
  98. Set the timeout for this connection.
  99. ------------------------------------------------------------------------*/
  100. Timeout = timeout;
  101. /*------------------------------------------------------------------------
  102. Allocate the packet staging buffer. This will be used to
  103. ------------------------------------------------------------------------*/
  104. PacketBuf = new char[ MaxPacketLen ];
  105. } /* end of ConnectionClass */
  106. /***************************************************************************
  107. * ConnectionClass::~ConnectionClass -- class destructor *
  108. * *
  109. * INPUT: *
  110. * none. *
  111. * *
  112. * OUTPUT: *
  113. * none. *
  114. * *
  115. * WARNINGS: *
  116. * none. *
  117. * *
  118. * HISTORY: *
  119. * 12/20/1994 BR : Created. *
  120. *=========================================================================*/
  121. ConnectionClass::~ConnectionClass ()
  122. {
  123. /*------------------------------------------------------------------------
  124. Free memory.
  125. ------------------------------------------------------------------------*/
  126. delete [] PacketBuf;
  127. } /* end of ~ConnectionClass */
  128. /***************************************************************************
  129. * ConnectionClass::Service -- main polling routine; services packets *
  130. * *
  131. * INPUT: *
  132. * none. *
  133. * *
  134. * OUTPUT: *
  135. * 1 = OK, 0 = error (connection is broken!) *
  136. * *
  137. * WARNINGS: *
  138. * none. *
  139. * *
  140. * HISTORY: *
  141. * 12/20/1994 BR : Created. *
  142. *=========================================================================*/
  143. int ConnectionClass::Service (void)
  144. {
  145. /*------------------------------------------------------------------------
  146. Service the Send Queue. This [re]sends packets in the Send Queue which
  147. haven't been ACK'd yet, and if their retry timeout has expired, and
  148. updates the FirstTime, LastTime & SendCount values in the Queue entry.
  149. Entries that have been ACK'd should be removed.
  150. ------------------------------------------------------------------------*/
  151. // if (!Service_Send_Queue())
  152. // return(0);
  153. /*------------------------------------------------------------------------
  154. Service the Receive Queue. This sends ACKs for packets that haven't
  155. been ACK'd yet. Entries that the app has read, and have been ACK'd,
  156. should be removed.
  157. ------------------------------------------------------------------------*/
  158. // if (!Service_Receive_Queue())
  159. // return(0);
  160. // return(1);
  161. if ( Service_Send_Queue() && Service_Receive_Queue() ) {
  162. return(1);
  163. } else {
  164. return(0);
  165. }
  166. } /* end of Service */
  167. // ST = 12/17/2018 5:44PM
  168. #ifndef TickCount
  169. extern TimerClass TickCount;
  170. #endif
  171. /***************************************************************************
  172. * ConnectionClass::Time -- gets current time *
  173. * *
  174. * INPUT: *
  175. * *
  176. * OUTPUT: *
  177. * none. *
  178. * *
  179. * WARNINGS: *
  180. * none. *
  181. * *
  182. * HISTORY: *
  183. * 12/20/1994 BR : Created. *
  184. *=========================================================================*/
  185. unsigned long ConnectionClass::Time (void)
  186. {
  187. #ifdef WWLIB32_H
  188. return(TickCount.Time()); // Westwood Library time
  189. #else
  190. static struct timeb mytime; // DOS time
  191. unsigned long msec;
  192. ftime(&mytime);
  193. msec = (unsigned long)mytime.time * 1000L + (unsigned long)mytime.millitm;
  194. return((msec / 100) * 6);
  195. #endif
  196. } /* end of Time */
  197. /***************************************************************************
  198. * ConnectionClass::Command_Name -- returns name for given packet command *
  199. * *
  200. * INPUT: *
  201. * command packet Command value to get name for *
  202. * *
  203. * OUTPUT: *
  204. * ptr to command name, NULL if invalid *
  205. * *
  206. * WARNINGS: *
  207. * none. *
  208. * *
  209. * HISTORY: *
  210. * 05/31/1995 BRR : Created. *
  211. *=========================================================================*/
  212. char *ConnectionClass::Command_Name(int command)
  213. {
  214. if (command >= 0 && command < PACKET_COUNT) {
  215. return(Commands[command]);
  216. } else {
  217. return(NULL);
  218. }
  219. }