CONNECT.CPP 12 KB

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