CONNECT.H 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/CONNECT.H 1 3/03/97 10:24a 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.H *
  22. * *
  23. * Programmer : Bill Randolph *
  24. * *
  25. * Start Date : December 19, 1994 *
  26. * *
  27. * Last Update : April 1, 1995 [BR] *
  28. * *
  29. *-------------------------------------------------------------------------*
  30. * *
  31. * DESCRIPTION: *
  32. * This class represents a single "connection" with another system. It's *
  33. * a pure virtual base class that acts as a framework for other classes. *
  34. * *
  35. * This class contains a CommBufferClass member, which stores received *
  36. * & transmitted packets. The ConnectionClass has virtual functions to *
  37. * handle adding packets to the queue, reading them from the queue, *
  38. * a Send routine for actually sending data, and a Receive_Packet function *
  39. * which is used to tell the connection that a new packet has come in. *
  40. * *
  41. * The virtual Service routines handle all ACK & Retry logic for *
  42. * communicating between this system & another. Thus, any class derived *
  43. * from this class may overload the basic ACK/Retry logic. *
  44. * *
  45. * THE PACKET HEADER: *
  46. * The Connection Classes prefix every packet sent with a header that's *
  47. * local to this class. The header contains a "Magic Number" which should *
  48. * be unique for each product, and Packet "Code", which will tell the *
  49. * receiving end if this is DATA, or an ACK packet, and a packet ID, which *
  50. * is a unique numerical ID for this packet (useful for detecting resends).*
  51. * The header is stored with each packet in the send & receive Queues; *
  52. * it's removed before it's passed back to the application, via *
  53. * Get_Packet() *
  54. * *
  55. * THE CONNECTION MANAGER: *
  56. * It is assumed that there will be a "Connection Manager" class which *
  57. * will handle parsing incoming packets; it will then tell the connection *
  58. * that new packets have come in, and the connection will process them in *
  59. * whatever way it needs to for its protocol (check for resends, handle *
  60. * ACK packets, etc). The job of the connection manager is to parse *
  61. * incoming packets & distribute them to the connections that need to *
  62. * store them (for multi-connection protocols). *
  63. * *
  64. * NOTES ON ACK/RETRY: *
  65. * This class provides a "non-sequenced" ACK/Retry approach to packet *
  66. * transmission. It sends out as many packets as are in the queue, whose *
  67. * resend delta times have expired; and it ACK's any packets its received *
  68. * who haven't been ACK'd yet. Thus, order of delivery is NOT guaranteed; *
  69. * but, the performance is better than a "sequenced" approach. Also, the *
  70. * Packet ID scheme (see below) ensures that the application will read *
  71. * the packets in the proper order. Thus, this class guarantees delivery *
  72. * and order of deliver. *
  73. * *
  74. * Each packet has a unique numerical ID; the ID is set to a count of the *
  75. * number of packets sent. Different count values are provided, for both *
  76. * DATA_ACK & DATA_NOACK packets. This ensures that the counter can be *
  77. * used to detect resends of DATA_ACK packets; the counters for DATA_NOACK *
  78. * packets aren't currently used. Other counters keep track of the *
  79. * last-sequentially-received packet ID (for DATA_ACK packets), so we *
  80. * can check for resends & missed packets, and the last-sequentially-read *
  81. * packet ID, so we can ensure the app reads the packets in order. *
  82. * *
  83. * If the protocol being used already guarantees delivery of packets, *
  84. * no ACK is required for the packets. In this case, the connection *
  85. * class for this protocol can overload the Service routine to avoid *
  86. * sending ACK packets, or the Connection Manager can just mark the *
  87. * packet as ACK'd when it adds it to the Receive Queue for the connection.*
  88. * *
  89. * Derived classes must provide: *
  90. * - Init: Initialization of any hardware-specific values. *
  91. * - Send: a hardware-dependent send routine. *
  92. * *
  93. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  94. #ifndef CONNECTION_H
  95. #define CONNECTION_H
  96. /*
  97. ********************************* Includes **********************************
  98. */
  99. #include "combuf.h"
  100. /*
  101. ********************************** Defines **********************************
  102. */
  103. #define CONN_DEBUG 0
  104. /*---------------------------------------------------------------------------
  105. This structure is the header prefixed to any packet sent by the application.
  106. MagicNumber: This is a number unique to the application; it's up to the
  107. Receive_Packet routine to check this value, to be sure we're
  108. not getting data from some other product. This value should
  109. be unique for each application.
  110. Code: This will be one of the below-defined codes.
  111. PacketID: This is a unique numerical ID for this packet. The Connection
  112. sets this ID on all packets sent out.
  113. ---------------------------------------------------------------------------*/
  114. typedef struct {
  115. unsigned short MagicNumber;
  116. unsigned char Code;
  117. unsigned long PacketID;
  118. } CommHeaderType;
  119. /*
  120. ***************************** Class Declaration *****************************
  121. */
  122. class ConnectionClass
  123. {
  124. /*
  125. ---------------------------- Public Interface ----------------------------
  126. */
  127. public:
  128. /*.....................................................................
  129. These are the possible values for the Code field of the CommHeaderType:
  130. .....................................................................*/
  131. enum ConnectionEnum {
  132. PACKET_DATA_ACK, // this is a data packet requiring an ACK
  133. PACKET_DATA_NOACK, // this is a data packet not requiring an ACK
  134. PACKET_ACK, // this is an ACK for a packet
  135. PACKET_COUNT // for computational purposes
  136. };
  137. /*.....................................................................
  138. Constructor/destructor.
  139. .....................................................................*/
  140. ConnectionClass (int numsend, int numrecieve, int maxlen,
  141. unsigned short magicnum, unsigned long retry_delta,
  142. unsigned long max_retries, unsigned long timeout, int extralen = 0);
  143. virtual ~ConnectionClass ();
  144. /*.....................................................................
  145. Initialization.
  146. .....................................................................*/
  147. virtual void Init (void);
  148. /*.....................................................................
  149. Send/Receive routines.
  150. .....................................................................*/
  151. virtual int Send_Packet (void * buf, int buflen, int ack_req);
  152. virtual int Receive_Packet (void * buf, int buflen);
  153. virtual int Get_Packet (void * buf, int * buflen);
  154. /*.....................................................................
  155. The main polling routine for the connection. Should be called as often
  156. as possible.
  157. .....................................................................*/
  158. virtual int Service (void);
  159. /*.....................................................................
  160. This routine is used by the retry logic; returns the current time in
  161. 60ths of a second.
  162. .....................................................................*/
  163. static unsigned long Time (void);
  164. /*.....................................................................
  165. Utility routines.
  166. .....................................................................*/
  167. unsigned short Magic_Num (void) { return (MagicNum); }
  168. unsigned long Retry_Delta (void) { return (RetryDelta); }
  169. void Set_Retry_Delta (unsigned long delta) { RetryDelta = delta;}
  170. unsigned long Max_Retries (void) { return (MaxRetries); }
  171. void Set_Max_Retries (unsigned long retries) { MaxRetries = retries;}
  172. unsigned long Time_Out (void) { return (Timeout); }
  173. void Set_TimeOut (unsigned long t) { Timeout = t;}
  174. unsigned long Max_Packet_Len (void) { return (MaxPacketLen); }
  175. static char * Command_Name(int command);
  176. /*.....................................................................
  177. The packet "queue"; this non-sequenced version isn't really much of
  178. a queue, but more of a repository.
  179. .....................................................................*/
  180. CommBufferClass *Queue;
  181. /*
  182. -------------------------- Protected Interface ---------------------------
  183. */
  184. protected:
  185. /*.....................................................................
  186. Routines to service the Send & Receive queues.
  187. .....................................................................*/
  188. virtual int Service_Send_Queue(void);
  189. virtual int Service_Receive_Queue(void);
  190. /*.....................................................................
  191. This routine actually performs a hardware-dependent data send. It's
  192. pure virtual, so it must be defined by a derived class. The routine
  193. is protected; it's only called by the ACK/Retry logic, not the
  194. application.
  195. .....................................................................*/
  196. virtual int Send(char *buf, int buflen, void *extrabuf,
  197. int extralen) = 0;
  198. /*.....................................................................
  199. This is the maximum packet length, including our own internal header.
  200. .....................................................................*/
  201. int MaxPacketLen;
  202. /*.....................................................................
  203. Packet staging area; this is where the CommHeaderType gets tacked onto
  204. the application's packet before it's sent.
  205. .....................................................................*/
  206. char *PacketBuf;
  207. /*.....................................................................
  208. This is the magic number assigned to this connection. It is the first
  209. few bytes of any transmission.
  210. .....................................................................*/
  211. unsigned short MagicNum;
  212. /*.....................................................................
  213. This value determines the time delay before a packet is re-sent.
  214. .....................................................................*/
  215. unsigned long RetryDelta;
  216. /*.....................................................................
  217. This is the maximum number of retries allowed for a packet; if this
  218. value is exceeded, the connection is probably broken.
  219. .....................................................................*/
  220. unsigned long MaxRetries;
  221. /*.....................................................................
  222. This is the total timeout for this connection; if this time is exceeded
  223. on a packet, the connection is probably broken.
  224. .....................................................................*/
  225. unsigned long Timeout;
  226. /*.....................................................................
  227. Running totals of # of packets we send & receive which require an ACK,
  228. and those that don't.
  229. .....................................................................*/
  230. unsigned long NumRecNoAck;
  231. unsigned long NumRecAck;
  232. unsigned long NumSendNoAck;
  233. unsigned long NumSendAck;
  234. /*.....................................................................
  235. This is the ID of the last consecutively-received packet; anything older
  236. than this, we know is a resend. Anything newer than this MUST be lying
  237. around in the Queue for us to detect it as a resend.
  238. .....................................................................*/
  239. unsigned long LastSeqID;
  240. /*.....................................................................
  241. This is the ID of the PACKET_DATA_ACK packet we read last; it ensures
  242. that the application reads that type of packet in order.
  243. .....................................................................*/
  244. unsigned long LastReadID;
  245. /*.....................................................................
  246. Names of all packet commands
  247. .....................................................................*/
  248. static char * Commands[PACKET_COUNT];
  249. };
  250. #endif
  251. /**************************** end of connect.h *****************************/