natsock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/natsock.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 9/17/01 4:09p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma once
  36. #ifndef NATSOCK_H
  37. #define NATSOCK_H
  38. #include "always.h"
  39. #include "assert.h"
  40. #include "vector.h"
  41. #include <winsock.h>
  42. #ifndef DebugString
  43. #include "wwdebug.h"
  44. #ifdef WWDEBUG_SAY
  45. #define DebugString WWDEBUG_SAY
  46. #endif
  47. #endif
  48. #ifdef WWASSERT
  49. #ifndef fw_assert
  50. #define fw_assert WWASSERT
  51. #endif //fw_assert
  52. #else //WWASSERT
  53. #define fw_assert assert
  54. #endif //WWASSERT
  55. #ifdef errno
  56. #undef errno
  57. #endif //errno
  58. #define errno (WSAGetLastError())
  59. #define LAST_ERROR errno
  60. #define TIMER_SECOND 1000
  61. /*
  62. ** Length of winsocks internal buffer.
  63. */
  64. #define SOCKET_BUFFER_SIZE 1024*512
  65. /*
  66. ** Length of our temporary receive buffer. Needs to be more that the max packet size which is about 550 bytes.
  67. */
  68. #define RECEIVE_BUFFER_LEN 640
  69. /*
  70. ** Number of statically allocated packet buffers for the class
  71. */
  72. #define MAX_STATIC_BUFFERS 32
  73. //#define PACKET_LOSS_PERCENTAGE 15
  74. /*
  75. ** Class to manage low level comms for talking to Mangler Servers.
  76. **
  77. ** Can't use the Renegade packet comms since the packet format is different - Mangler servers expect C&C packet format.
  78. */
  79. class SocketHandlerClass
  80. {
  81. public:
  82. /*
  83. ** Constructor, destructor.
  84. */
  85. SocketHandlerClass(void);
  86. ~SocketHandlerClass(void);
  87. /*
  88. ** Startup, shutdown.
  89. */
  90. bool Open(int inport, int outport);
  91. void Close(void);
  92. void Discard_In_Buffers(void);
  93. void Discard_Out_Buffers(void);
  94. /*
  95. ** Read, write.
  96. */
  97. int Peek(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
  98. int Read(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
  99. void Write(void *buffer, int buffer_len, void *address, unsigned short port = 0);
  100. /*
  101. ** Service.
  102. */
  103. void Service(void);
  104. static void Service_All(void);
  105. inline void Service_Never(void) {CanService = false;};
  106. /*
  107. ** Error handling.
  108. */
  109. void Clear_Socket_Error(void);
  110. /*
  111. ** Query functions.
  112. */
  113. int Get_Num_Queued_Receive_Packets(void) {return(InBuffers.Count());};
  114. int Get_Num_Queued_Outgoing_Packets(void) {return(OutBuffers.Count());};
  115. int Get_Num_Local_Addresses(void) {return (LocalAddresses.Count());};
  116. unsigned char * Get_Local_Address (int a) {return (LocalAddresses[a]);};
  117. int Get_Incoming_Port(void) {return(IncomingPort);};
  118. SOCKET Get_Socket(void) {return(Socket);};
  119. private:
  120. /*
  121. ** The socket associated with this class.
  122. */
  123. SOCKET Socket;
  124. /*
  125. ** The port that this class listens on.
  126. */
  127. int IncomingPort;
  128. /*
  129. ** The port that the class writes to.
  130. */
  131. int OutgoingPort;
  132. /*
  133. ** List of local addresses.
  134. */
  135. DynamicVectorClass <unsigned char *> LocalAddresses;
  136. /*
  137. ** This struct contains the information needed for each incoming and outgoing packet.
  138. ** It acts as a temporary control for these packets.
  139. */
  140. struct WinsockBufferType {
  141. unsigned char Address[4]; // Address. IN_ADDR
  142. int BufferLen; // Length of data in buffer
  143. bool IsBroadcast; // Flag to broadcast this packet
  144. bool InUse; // Useage state of buffer
  145. bool IsAllocated; // false means statically allocated.
  146. unsigned short Port; // Override port. Send to this port if not 0. Save incoming port number.
  147. unsigned long CRC; // CRC of packet for extra sanity.
  148. unsigned char Buffer[RECEIVE_BUFFER_LEN]; // Buffer to store packet in.
  149. };
  150. /*
  151. ** Packet buffer allocation.
  152. */
  153. void *Get_New_Out_Buffer(void);
  154. void *Get_New_In_Buffer(void);
  155. /*
  156. ** Packet CRCs.
  157. */
  158. void Add_CRC(unsigned long *crc, unsigned long val);
  159. virtual void Build_Packet_CRC(WinsockBufferType *packet);
  160. virtual bool Passes_CRC_Check(WinsockBufferType *packet);
  161. /*
  162. ** Array of buffers to temporarily store incoming and outgoing packets.
  163. */
  164. DynamicVectorClass<WinsockBufferType*> InBuffers;
  165. DynamicVectorClass<WinsockBufferType*> OutBuffers;
  166. /*
  167. ** Array of buffers that are always available for incoming packets.
  168. */
  169. WinsockBufferType StaticInBuffers[MAX_STATIC_BUFFERS];
  170. WinsockBufferType StaticOutBuffers[MAX_STATIC_BUFFERS];
  171. /*
  172. ** Pointers to allow circular use of the buffer arrays.
  173. */
  174. int InBufferArrayPos;
  175. int OutBufferArrayPos;
  176. /*
  177. ** Usage count for each array.
  178. */
  179. int InBuffersUsed;
  180. int OutBuffersUsed;
  181. /*
  182. ** Temporary receive buffer to use when querying Winsock for incoming packets.
  183. */
  184. unsigned char ReceiveBuffer[RECEIVE_BUFFER_LEN];
  185. /*
  186. ** All instances of this class.
  187. */
  188. static DynamicVectorClass<SocketHandlerClass*> AllSocketHandlers;
  189. /*
  190. ** Can the regular service code be called for this class?
  191. */
  192. bool CanService;
  193. };
  194. #endif //NATSOCK_H