CONNECT.H 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.h_v 1.12 16 Oct 1995 16:46:04 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 CommQueueClass 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 routine will handle all ACK & Retry logic for *
  46. * communicating between this system & another. Thus, any class derived *
  47. * from this class must provide the basic ACK/Retry logic. *
  48. * *
  49. * THE 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. * The packet's ID is used to check for re-sends. The ID is set to the *
  70. * Queue's total Send Count; if the receiving system checks this value, *
  71. * and it's less than that system's Receive Count, this is a resend. *
  72. * (ie packet 0 will be the 1st packet sent, since the Send Queue's count *
  73. * is 0 when it's sent; as soon as it's received, the Receive Count goes *
  74. * up to 1, and an ID of 0 then means a resend.) This scheme keeps the *
  75. * application from seeing the same packet twice. All the Connection *
  76. * Manager has to do is mark the resent packet as non-ACK'd. Of course, *
  77. * the Manager doesn't have to use this value at all. *
  78. * *
  79. * Both DATA_ACK packets and DATA_NOACK packets must go through the Send *
  80. * Queue when "sent", so that the SendTotal value for this system *
  81. * will still match the ReceiveTotal value for the other system; this is *
  82. * why a NOACK packet can't just be sent immediately; it must go through *
  83. * the queue. *
  84. * *
  85. * If the protocol being used already guarantees delivery of packets, *
  86. * no ACK is required for the packets. In this case, the connection *
  87. * class for this protocol can overload the Service routine to avoid *
  88. * sending ACK packets, or the Connection Manager can just mark the *
  89. * packet as ACK'd when it adds it to the Receive Queue for the connection.*
  90. * *
  91. * Derived classes must provide: *
  92. * - Init a version of Init that gives the connection *
  93. * access to any hardware-specific values it needs *
  94. * Must chain to the parent's Init routine. *
  95. * - Send_Packet adds the CommHeaderType header, adds the packet *
  96. * to the out-going queue *
  97. * - Receive_Packet processes incoming ACK packets, detects resends,*
  98. * adds new packets to the in-coming queue *
  99. * - Get_Packet reads the next-available packet from the *
  100. * receive queue *
  101. * - Send the hardware-dependent data-sending routine *
  102. * - Service_Send_Queue services the send queue; handles re-sends, *
  103. * detects when outgoing packets have been ACK'd; *
  104. * cleans out the queue of old packets *
  105. * - Service_Receive_Queue services the receive queue; handles sending *
  106. * ACK's for new or re-sent packets; cleans out *
  107. * the queue of old packets *
  108. * *
  109. * Any other routines can be overloaded as the derived class needs. *
  110. * *
  111. * CLASS HIERARCHY: *
  112. * ConnectionClass *
  113. * | *
  114. * | *
  115. * -------------------------------------- *
  116. * | | *
  117. * | | *
  118. * SequencedConnClass NonSequencedConnClass *
  119. * | | *
  120. * | | *
  121. * IPXConnClass ------------------------ *
  122. * | | | *
  123. * | | | *
  124. * IPXGlobalConnClass NullModemConnClass ModemConnClass *
  125. * *
  126. * *
  127. * ConnectionClass: *
  128. * Abstract base class. *
  129. * Provides: Queue for sent/recv'd packets *
  130. * PacketBuf for preparing packets *
  131. * Timeout variables *
  132. * Service() routine *
  133. * *
  134. * SequencedConnClass: *
  135. * Abstract base class *
  136. * Provides: * "Sequenced" ACK/Retry logic, in Service_Send_Queue() & *
  137. * Service_Receive_Queue() routines *
  138. * * Send_Packet(): adds header to packet, adds it to Queue *
  139. * * Receive_Packet(): adds incoming packet to receive Queue, *
  140. * handles incoming ACK's & resends *
  141. * * Get_Packet(): gets packet from the receive queue *
  142. * *
  143. * NonSequencedConnClass: *
  144. * Abstract base class *
  145. * Provides: * "Non-Sequenced" ACK/Retry logic, in Service_Send_Queue() *
  146. * & Service_Receive_Queue() routines *
  147. * * Send_Packet(): adds header to packet, adds it to Queue *
  148. * * Receive_Packet(): adds incoming packet to receive Queue, *
  149. * handles incoming ACK's & resends *
  150. * * Get_Packet(): gets packet from the receive queue *
  151. * *
  152. * IPXConnClass: *
  153. * Provides: * Hardware-dependent IPX interface routines, which allow *
  154. * Service_Send_Queue() & Service_Receive_Queue() to do *
  155. * their job *
  156. * * Ability to associate an IPX Address, a numerical ID, and *
  157. * a character-string Name with a connection *
  158. * Inherits: * Sequenced ACK/Retry logic, Service routines, Queue & *
  159. * PacketBuf, timeout variables *
  160. * *
  161. * IPXGlobalConnClass: *
  162. * Special type of IPX Connection; supports receiving packets from *
  163. * multiple systems at once, and sending packets via Broadcast or *
  164. * to a specific address. *
  165. * Provides: * Specialized Receive_Packet() routine, which handles *
  166. * receiving packets from multiple systems *
  167. * * Specialized Send_Packet() & Get_Packet() routines, *
  168. * which pass IPX address of destination through to *
  169. * the application, giving the application control over *
  170. * whether the packet will be Broadcast or sent to a *
  171. * specific destination (embeds destination address within *
  172. * the packet itself) *
  173. * * Specialized Send routine, which extracts the destination *
  174. * address from the packet *
  175. * Inherits: * Sequenced ACK/Retry logic, Service routines, Queue & *
  176. * PacketBuf, timeout variables, IPX-specific routines *
  177. * *
  178. * NullModemConnClass: *
  179. * Provides: * Hardware-dependent Serial-communication routines, which *
  180. * allow Service_Send_Queue() & Service_Receive_Queue() to *
  181. * do their job *
  182. * Inherits: * Non-Sequenced ACK/Retry logic, Service routines, Queue & *
  183. * PacketBuf, timeout variables *
  184. * *
  185. * ModemConnClass: *
  186. * Provides: * Hardware-dependent Modem-communication routines, which *
  187. * allow Service_Send_Queue() & Service_Receive_Queue() to *
  188. * do their job *
  189. * Inherits: * Non-Sequenced ACK/Retry logic, Service routines, Queue & *
  190. * PacketBuf, timeout variables *
  191. * *
  192. * So, do ya think this header is long enough, or what? *
  193. * *
  194. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  195. #ifndef CONNECTION_H
  196. #define CONNECTION_H
  197. #define CONN_DEBUG 0
  198. /*
  199. ********************************** Defines **********************************
  200. */
  201. /*---------------------------------------------------------------------------
  202. This structure is the header prefixed to any packet sent by the application.
  203. MagicNumber: This is a number unique to the application; it's up to the
  204. Receive_Packet routine to check this value, to be sure we're
  205. not getting data from some other product. This value should
  206. be unique for each application.
  207. Code: This will be one of the below-defined codes.
  208. PacketID: This is a unique numerical ID for this packet. The Connection
  209. sets this ID on all packets sent out.
  210. ---------------------------------------------------------------------------*/
  211. typedef struct {
  212. unsigned short MagicNumber;
  213. unsigned char Code;
  214. unsigned long PacketID;
  215. } CommHeaderType;
  216. /*
  217. ***************************** Class Declaration *****************************
  218. */
  219. class ConnectionClass
  220. {
  221. /*
  222. ---------------------------- Public Interface ----------------------------
  223. */
  224. public:
  225. /*.....................................................................
  226. These are the possible values for the Code field of the CommHeaderType:
  227. .....................................................................*/
  228. enum ConnectionEnum {
  229. PACKET_DATA_ACK, // this is a data packet requiring an ACK
  230. PACKET_DATA_NOACK, // this is a data packet not requiring an ACK
  231. PACKET_ACK, // this is an ACK for a packet
  232. PACKET_COUNT, // for computational purposes
  233. };
  234. /*.....................................................................
  235. Constructor/destructor.
  236. .....................................................................*/
  237. ConnectionClass (int maxlen, unsigned short magicnum,
  238. unsigned long retry_delta, unsigned long max_retries,
  239. unsigned long timeout);
  240. virtual ~ConnectionClass ();
  241. /*.....................................................................
  242. Initialization.
  243. .....................................................................*/
  244. virtual void Init (void) {};
  245. /*.....................................................................
  246. Send/Receive routines.
  247. .....................................................................*/
  248. virtual int Send_Packet (void * buf, int buflen, int ack_req) = 0;
  249. virtual int Receive_Packet (void * buf, int buflen) = 0;
  250. virtual int Get_Packet (void * buf, int * buflen) = 0;
  251. /*.....................................................................
  252. The main polling routine for the connection. Should be called as often
  253. as possible.
  254. .....................................................................*/
  255. virtual int Service (void);
  256. /*.....................................................................
  257. This routine is used by the retry logic; returns the current time in
  258. 60ths of a second.
  259. .....................................................................*/
  260. static unsigned long Time (void);
  261. /*.....................................................................
  262. Utility routines.
  263. .....................................................................*/
  264. unsigned short Magic_Num (void) { return (MagicNum); }
  265. unsigned long Retry_Delta (void) { return (RetryDelta); }
  266. void Set_Retry_Delta (unsigned long delta) { RetryDelta = delta;}
  267. unsigned long Max_Retries (void) { return (MaxRetries); }
  268. void Set_Max_Retries (unsigned long retries) { MaxRetries = retries;}
  269. unsigned long Time_Out (void) { return (Timeout); }
  270. void Set_TimeOut (unsigned long t) { Timeout = t;}
  271. unsigned long Max_Packet_Len (void) { return (MaxPacketLen); }
  272. static char * Command_Name(int command);
  273. /*
  274. -------------------------- Protected Interface ---------------------------
  275. */
  276. protected:
  277. /*.....................................................................
  278. Routines to service the Send & Receive queues.
  279. .....................................................................*/
  280. virtual int Service_Send_Queue(void) = 0;
  281. virtual int Service_Receive_Queue(void) = 0;
  282. /*.....................................................................
  283. This routine actually performs a hardware-dependent data send. It's
  284. pure virtual, so it >must< be defined by a derived class.
  285. .....................................................................*/
  286. virtual int Send(char *buf, int buflen) = 0;
  287. /*.....................................................................
  288. This is the maximum packet length, including our own internal header.
  289. .....................................................................*/
  290. int MaxPacketLen;
  291. /*.....................................................................
  292. Packet staging area; this is where the CommHeaderType gets tacked onto
  293. the application's packet before it's sent.
  294. .....................................................................*/
  295. char *PacketBuf;
  296. /*.....................................................................
  297. This is the magic number assigned to this connection. It is the first
  298. few bytes of any transmission.
  299. .....................................................................*/
  300. unsigned short MagicNum;
  301. /*.....................................................................
  302. This value determines the time delay before a packet is re-sent.
  303. .....................................................................*/
  304. unsigned long RetryDelta;
  305. /*.....................................................................
  306. This is the maximum number of retries allowed for a packet; if this
  307. value is exceeded, the connection is probably broken.
  308. .....................................................................*/
  309. unsigned long MaxRetries;
  310. /*.....................................................................
  311. This is the total timeout for this connection; if this time is exceeded
  312. on a packet, the connection is probably broken.
  313. .....................................................................*/
  314. unsigned long Timeout;
  315. /*.....................................................................
  316. Names of all packet commands
  317. .....................................................................*/
  318. static char *ConnectionClass::Commands[PACKET_COUNT];
  319. };
  320. #endif