connect.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // Filename: connect.h
  20. // Project: wwnet
  21. // Author: Tom Spencer-Smith
  22. // Date: June 1998
  23. // Description:
  24. //
  25. //-----------------------------------------------------------------------------
  26. #if defined(_MSV_VER)
  27. #pragma once
  28. #endif
  29. #ifndef CONNECT_H
  30. #define CONNECT_H
  31. #include "rhost.h"
  32. #include "netstats.h"
  33. #include "bittype.h"
  34. #include "netutil.h"
  35. #include "slist.h"
  36. #include "wwpacket.h"
  37. #include "packettype.h"
  38. //
  39. // A server can have this many clients (a client has only 1 rhost: the server)
  40. // -1 is used as ID_UNKNOWN
  41. // This is used as an array bound and is not insulated,
  42. // but using a namespace is ugly...
  43. //
  44. //const int MAX_RHOSTS = 256;
  45. //
  46. // These defines are useful at the app level
  47. //
  48. const int SERVER_RHOST_ID = 0;
  49. //
  50. // This is the default dummy ID used by a client until he receives a real id
  51. // from the server
  52. //
  53. const int ID_UNKNOWN = -1;
  54. typedef enum {
  55. REFUSAL_CLIENT_ACCEPTED = 0,
  56. REFUSAL_GAME_FULL,
  57. REFUSAL_BAD_PASSWORD,
  58. REFUSAL_VERSION_MISMATCH,
  59. REFUSAL_PLAYER_EXISTS,
  60. REFUSAL_BY_APPLICATION
  61. } REFUSAL_CODE;
  62. //
  63. // Send flags. Normal send is individual, single.
  64. // Strictly speaking SEND_UNRELIABLE could be deduced by absense
  65. // of SEND_RELIABLE, but it's inclusion makes a clearer API.
  66. //
  67. const BYTE SEND_RELIABLE = 0x01; // you must specify this or SEND_UNRELIABLE
  68. const BYTE SEND_UNRELIABLE = 0x02; // you must specify this or SEND_RELIABLE
  69. const BYTE SEND_MULTI = 0x04; // For SEND_UNRELIABLE only. Default is single send.
  70. class cMsgStatList;
  71. typedef void (*Accept_Handler)(void);
  72. typedef void (*Refusal_Handler)(REFUSAL_CODE refusal_code);
  73. typedef void (*Server_Broken_Connection_Handler)(int broken_rhost_id);
  74. typedef void (*Client_Broken_Connection_Handler)(void);
  75. typedef void (*Eviction_Handler)(int evicted_rhost_id);
  76. typedef void (*Conn_Handler)(int new_rhost_id);
  77. typedef REFUSAL_CODE (*Application_Acceptance_Handler)(cPacket & packet);
  78. typedef void (*Server_Packet_Handler)(cPacket & packet, int rhost_id);
  79. typedef void (*Client_Packet_Handler)(cPacket & packet);
  80. extern char * Addr_As_String(sockaddr_in *addr);
  81. //-----------------------------------------------------------------------------
  82. //
  83. // This class represents the link between C & S.
  84. // The 1-many S-C relationship is expressed in the cRemoteHost component.
  85. //
  86. class cConnection
  87. {
  88. public:
  89. cConnection();
  90. ~cConnection();
  91. void Init_As_Client(ULONG server_ip, USHORT server_port, unsigned short my_port = 0);
  92. void Init_As_Server(USHORT server_port, int max_players,
  93. bool is_dedicated_server, ULONG addr = 0);
  94. void Connect_Cs(cPacket & app_data);
  95. void Send_Packet_To_Individual(cPacket & packet, int addressee, BYTE send_flags);
  96. bool Have_Id() const {return LocalId != ID_UNKNOWN;}
  97. bool Is_Established() const;
  98. void Service_Read();
  99. void Service_Send(bool is_urgent = false);
  100. ULONG Get_Bandwidth_Budget_Out() const {return BandwidthBudgetOut;}
  101. void Set_Bandwidth_Budget_Out(ULONG bw_budget);
  102. void Destroy_Connection(int rhost_id);
  103. void Init_Stats();
  104. double Get_Threshold_Priority(int rhost_id);
  105. void Set_Packet_Loss(double percent_lost);
  106. void Set_Packet_Duplication(double percent_duplicated);
  107. void Set_Packet_Latency_Range(int minimum_latency_ms, int maximum_latency_ms);
  108. void Set_Max_Acceptable_Packetloss_Pc(double max_packetloss_pc);
  109. void Enable_Flow_Control(BOOL is_enabled) {IsFlowControlEnabled = is_enabled;} // This should be called at startup.
  110. SList<cPacket> * Get_Packet_List() {return &PacketList;}
  111. void Clear_Resend_Counts();
  112. int Get_Min_RHost() {return MinRHost;}
  113. int Get_Max_RHost() {return MaxRHost;}
  114. int Get_Num_RHosts() {return NumRHosts;}
  115. cRemoteHost * Get_Remote_Host(int rhost);
  116. bool Is_Destroy() {return IsDestroy;}
  117. int Get_Local_Id() const {return LocalId;}
  118. double Get_Max_Acceptable_Packetloss_Pc() const {return MaxAcceptablePacketlossPc;}
  119. cNetStats & Get_Combined_Stats() {return CombinedStats;}
  120. cNetStats & Get_Averaged_Stats() {return AveragedStats;}
  121. static BOOL Is_Flow_Control_Enabled() {return IsFlowControlEnabled;}
  122. static UINT Get_Total_Compressed_Bytes_Sent(void)
  123. {return TotalCompressedBytesSent;}
  124. static UINT Get_Total_Uncompressed_Bytes_Sent(void)
  125. {return TotalUncompressedBytesSent;}
  126. cMsgStatList * Get_Stat_List(void) {return PStatList;}
  127. bool Is_Bad_Connection(void) {return(IsBadConnection);};
  128. void Set_Rhost_Is_In_Game(int id, bool state);
  129. void Set_Rhost_Expect_Packet_Flood(int id, bool state);
  130. void Allow_Extra_Timeout_For_Loading(void);
  131. void Allow_Packet_Processing(bool set) {CanProcess = set;}
  132. void Install_Accept_Handler(Accept_Handler handler);
  133. void Install_Refusal_Handler(Refusal_Handler handler);
  134. void Install_Server_Broken_Connection_Handler(Server_Broken_Connection_Handler handler);
  135. void Install_Client_Broken_Connection_Handler(Client_Broken_Connection_Handler handler);
  136. void Install_Eviction_Handler(Eviction_Handler handler);
  137. void Install_Conn_Handler(Conn_Handler handler);
  138. void Install_Application_Acceptance_Handler(Application_Acceptance_Handler handler);
  139. void Install_Server_Packet_Handler(Server_Packet_Handler handler);
  140. void Install_Client_Packet_Handler(Client_Packet_Handler handler);
  141. // Need to use this. ST - 8/10/2001 11:52AM
  142. void Send_Packet_To_Address(cPacket & packet, LPSOCKADDR_IN p_address);
  143. #ifdef WWDEBUG
  144. // Debug support for latency testing.
  145. static void Set_Latency(int low, int high);
  146. static void Get_Latency(int &low, int &high, int &current);
  147. #endif //WWDEBUG
  148. private:
  149. cConnection(const cConnection& rhs); // Disallow copy (compile/link time)
  150. cConnection& operator=(const cConnection& rhs); // Disallow assignment (compile/link time)
  151. void Init_As_Client(LPSOCKADDR_IN p_server_address, unsigned short my_port = 0);
  152. bool Demultiplex_R_Or_U_Packet(cPacket * p_packet, int rhost_id);
  153. void Send_Accept_Sc(int new_rhost_id);
  154. bool Bind(USHORT port, ULONG addr = 0);
  155. bool Receive_Packet();
  156. int Low_Level_Send_Wrapper(cPacket & packet, LPSOCKADDR_IN p_address);
  157. int Send_Wrapper(cPacket & packet, LPSOCKADDR_IN p_address);
  158. int Send_Wrapper(cPacket & packet, int addressee);
  159. int Low_Level_Receive_Wrapper(cPacket & packet);
  160. int Receive_Wrapper(cPacket & packet);
  161. void Set_R_And_U_Packet_Id(cPacket & packet, int addressee, BYTE send_type);
  162. void R_And_U_Send(cPacket & packet, int addressee);
  163. void Send_Ack(LPSOCKADDR_IN p_address, int reliable_packet_id);
  164. void Send_Refusal_Sc(LPSOCKADDR_IN p_address, REFUSAL_CODE refusal_code);
  165. void Process_Connection_Request(cPacket & packet);
  166. void Send_Keepalives();
  167. static LPCSTR Type_Translation(int type);
  168. bool Sender_Id_Tests(cPacket & packet);
  169. USHORT Calculate_Packet_Bits(USHORT app_bytes);
  170. int Single_Player_sendto(cPacket & packet);
  171. int Single_Player_recvfrom(char * data);
  172. int Address_To_Rhostid(const SOCKADDR_IN* p_address);
  173. bool Is_Time_To_Resend_Packet_To_Remote_Host(const cPacket *packet, cRemoteHost *rhost);
  174. bool Is_Packet_Too_Old(const cPacket *packet, cRemoteHost *rhost);
  175. int LocalId; // Each client has a unique id
  176. USHORT LocalPort; // port we are bound to.
  177. double MaxAcceptablePacketlossPc;
  178. cNetStats CombinedStats;
  179. cNetStats AveragedStats;
  180. unsigned long ThisFrameTimeMs; // to avoid excess timeGetTime clls
  181. bool IsServer; // for C/S specific-code
  182. bool IsDedicatedServer; // for C/S specific-code
  183. static BOOL IsFlowControlEnabled;
  184. bool InitDone; // Used to ensure certain API's are used at the right time.
  185. SOCKET Sock;
  186. USHORT SimulatedPacketLossPerRANDMAX;
  187. USHORT SimulatedPacketDuplicationPerRANDMAX;
  188. int MinimumLatencyMs;
  189. int MaximumLatencyMs;
  190. int RefusalPacketSendId; // server
  191. int HighestRefusalPacketRcvId; // client
  192. ULONG BandwidthBudgetOut;
  193. SList<cPacket> PacketList;
  194. int ServiceCount;
  195. bool IsBadConnection;
  196. cRemoteHost ** PRHost;
  197. int MinRHost;
  198. int MaxRHost;
  199. int NumRHosts;
  200. bool IsDestroy;
  201. static UINT TotalCompressedBytesSent;
  202. static UINT TotalUncompressedBytesSent;
  203. cMsgStatList * PStatList;
  204. unsigned long ExtraTimeoutTime;
  205. unsigned long ExtraTimeoutTimeStarted;
  206. bool CanProcess;
  207. Accept_Handler AcceptHandler;
  208. Refusal_Handler RefusalHandler;
  209. Server_Broken_Connection_Handler ServerBrokenConnectionHandler;
  210. Client_Broken_Connection_Handler ClientBrokenConnectionHandler;
  211. Eviction_Handler EvictionHandler;
  212. Conn_Handler ConnHandler;
  213. Application_Acceptance_Handler ApplicationAcceptanceHandler;
  214. Server_Packet_Handler ServerPacketHandler;
  215. Client_Packet_Handler ClientPacketHandler;
  216. #ifdef WWDEBUG
  217. // Testing support for high latency connections.
  218. // Dynamic vector is ineffecient here but it doesn't matter since this is a debug testing only kinda thing.
  219. static int LatencyAddLow;
  220. static int LatencyAddHigh;
  221. static int CurrentLatencyAdd;
  222. static unsigned long LastLatencyChange;
  223. DynamicVectorClass<cPacket*> LaggedPackets;
  224. DynamicVectorClass<unsigned long> LaggedPacketTimes;
  225. DynamicVectorClass<int> LaggedPacketRetCodes;
  226. #endif //WWDEBUG
  227. };
  228. #endif // CONNECT_H
  229. //void Init_As_Client(LPCSTR server_ip, USHORT server_port);
  230. //void Send_Packet_To_All(cPacket & packet, BYTE send_flags); // send to all rhosts
  231. //cRemoteHost * PRHost[MAX_RHOSTS];
  232. //virtual void Server_Broken_Connection_Handler(int rhost_id);
  233. //virtual void Client_Broken_Connection_Handler();
  234. //virtual void Accept_Handler();
  235. //virtual void Refusal_Handler(int refusal_code);
  236. //virtual void Connection_Handler(int new_rhost_id);
  237. //virtual bool Application_Acceptance_Handler(cPacket & packet);
  238. //virtual void Eviction_Handler(int evicted_rhost_id);
  239. //virtual void Server_Packet_Handler(cPacket & packet, int rhost_id);
  240. //virtual void Client_Packet_Handler(cPacket & packet);