CONNECT.H 14 KB

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