rhost.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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: rhost.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 RHOST_H
  30. #define RHOST_H
  31. #include "netstats.h"
  32. #include "wwpacket.h"
  33. #include "bittype.h"
  34. #include "slist.h"
  35. #include "wwdebug.h"
  36. #include "win.h"
  37. #include <winsock.h>
  38. //const USHORT MAX_MESSAGE_TYPES = 256;
  39. //
  40. // ResendTimeoutMs is dynamic, and works like this: It starts out very low,
  41. // say 10ms, as given by DEFAULT_RESEND_TIMEOUT_MS. Then it is kept 10%
  42. // higher than the highest ping. Every 60s the stats are reset, and
  43. // at that time ResendTimeoutMs is set to be min(3 * the average
  44. // ping for that sample, max ping for that sample). This means that
  45. // rare outlandishly large pings don't cause our ResendTimeoutMs to
  46. // be too conservative.
  47. // It's all very ad hoc of course and will need refinement...
  48. //
  49. enum {
  50. RELIABLE_SEND_LIST = 0,
  51. RELIABLE_RCV_LIST,
  52. UNRELIABLE_SEND_LIST,
  53. UNRELIABLE_RCV_LIST
  54. };
  55. class cRemoteHost
  56. {
  57. public:
  58. cRemoteHost();
  59. ~cRemoteHost();
  60. void Add_Packet(cPacket & packet, BYTE list_type);
  61. void Remove_Packet(int reliable_packet_id, BYTE list_type);
  62. void Toggle_Flow_Control();
  63. void Init_Stats();
  64. int Get_Last_Service_Count() {return LastServiceCount;}
  65. void Set_Last_Service_Count(int service_count);
  66. void Compute_List_Max(int list_type);
  67. int Get_List_Max(int list_type);
  68. void Set_List_Processing_Time(int list_type, int processing_time_ms);
  69. int Get_List_Processing_Time(int list_type);
  70. int Get_Last_Contact_Time() {return LastContactTime;}
  71. void Set_Last_Contact_Time(int time) {LastContactTime = time;}
  72. SOCKADDR_IN & Get_Address();
  73. void Set_Address(SOCKADDR_IN & address) {Address = address;}
  74. int Get_Target_Bps(void) const {return TargetBps;}
  75. //void Set_Target_Bps(int bps) {WWASSERT(bps > 0); TargetBps = bps;}
  76. void Set_Target_Bps(int bps) {TargetBps = bps;}
  77. int Get_Maximum_Bps(void) const {return MaximumBps;}
  78. //void Set_Maximum_Bps(int bps) {WWASSERT(bps > 0); MaximumBps = bps;}
  79. void Set_Maximum_Bps(int bps) {MaximumBps = bps;}
  80. cNetStats & Get_Stats() {return Stats;}
  81. double Get_Threshold_Priority() const {return ThresholdPriority;}
  82. float Get_Bandwidth_Multiplier(void) const {return BandwidthMultiplier;}
  83. void Set_Average_Priority(float ave) {AverageObjectPriority = ave;}
  84. float Get_Average_Priority(void) {return(AverageObjectPriority);}
  85. USHORT Get_Resend_Timeout_Ms() const {return ResendTimeoutMs;}
  86. //
  87. // Ping
  88. //
  89. int Get_Num_Internal_Pings() const {return NumInternalPings;}
  90. int Get_Total_Internal_Pingtime_Ms() const {return TotalInternalPingtimeMs;}
  91. int Get_Average_Internal_Pingtime_Ms() const {return AverageInternalPingtimeMs;}
  92. int Get_Min_Internal_Pingtime_Ms() const {return MinInternalPingtimeMs;}
  93. int Get_Max_Internal_Pingtime_Ms() const {return MaxInternalPingtimeMs;}
  94. int Get_Reliable_Packet_Send_Id() const {return ReliablePacketSendId;}
  95. int Get_Unreliable_Packet_Send_Id() const {return UnreliablePacketSendId;}
  96. int Get_Reliable_Packet_Rcv_Id() const {return ReliablePacketRcvId;}
  97. int Get_Unreliable_Packet_Rcv_Id() const {return UnreliablePacketRcvId;}
  98. void Set_Reliable_Packet_Send_Id(int id) {ReliablePacketSendId = id;}
  99. void Set_Unreliable_Packet_Send_Id(int id) {UnreliablePacketSendId = id;}
  100. void Set_Reliable_Packet_Rcv_Id(int id) {ReliablePacketRcvId = id;}
  101. void Set_Unreliable_Packet_Rcv_Id(int id) {UnreliablePacketRcvId = id;}
  102. void Increment_Reliable_Packet_Send_Id() {ReliablePacketSendId++;}
  103. void Increment_Unreliable_Packet_Send_Id() {UnreliablePacketSendId++;}
  104. void Increment_Reliable_Packet_Rcv_Id() {ReliablePacketRcvId++;}
  105. void Increment_Unreliable_Packet_Rcv_Id() {UnreliablePacketRcvId++;}
  106. unsigned long Get_Last_Keepalive_Time_Ms() const {return LastKeepaliveTimeMs;}
  107. void Set_Last_Keepalive_Time_Ms(unsigned long time_ms) {LastKeepaliveTimeMs = time_ms;}
  108. SList<cPacket> & Get_Packet_List(int index) {WWASSERT(index >= 0 && index < 4); return PacketList[index];}
  109. bool Must_Evict() const {return MustEvict;}
  110. void Set_Must_Evict(bool flag) {MustEvict = flag;}
  111. void Adjust_Flow_If_Necessary(float sample_time_ms);
  112. void Adjust_Resend_Timeout(void);
  113. void Set_Id(int id) {WWASSERT(id >= 0); Id = id;}
  114. int Get_Id(void) {WWASSERT(Id >= 0); return Id;}
  115. void Set_Is_Loading(bool state);
  116. bool Get_Is_Loading(void) {return(IsLoading);}
  117. bool Was_Recently_Loading(unsigned long time = 0);
  118. void Set_Flood(bool state);
  119. bool Get_Flood(void) {return(ExpectPacketFlood);}
  120. unsigned long Get_Creation_Time(void) {return(CreationTime);}
  121. unsigned long Get_Total_Resends(void) {return(TotalResends);}
  122. void Increment_Resends(void) {TotalResends++;}
  123. void Set_Total_Resent_Packets_In_Queue (int resent_packets) {TotalResentPacketsInQueue = resent_packets;}
  124. inline int Get_Priority_Update_Counter(void) {return(PriorityUpdateCounter);}
  125. inline void Increment_Priority_Count(void) {PriorityUpdateCounter++; if (PriorityUpdateCounter > PriorityUpdateRate) PriorityUpdateCounter = 0;}
  126. static void Set_Priority_Update_Rate(int rate) {PriorityUpdateRate = rate;}
  127. static inline void Set_Allow_Extra_Modem_Bandwidth_Throttling(bool set) {AllowExtraModemBandwidthThrottling = set;}
  128. private:
  129. cRemoteHost(const cRemoteHost& rhs); // Disallow copy (compile/link time)
  130. cRemoteHost& operator=(const cRemoteHost& rhs); // Disallow assignment (compile/link time)
  131. void Dam_The_Flood(void);
  132. bool Is_Outgoing_Flooded(void);
  133. cNetStats Stats;
  134. double ThresholdPriority;
  135. double TPIncrement;
  136. int LastReliableSendId;
  137. int LastUnreliableSendId;
  138. USHORT ResendTimeoutMs;
  139. int NumInternalPings;
  140. int TotalInternalPingtimeMs;
  141. int AverageInternalPingtimeMs;
  142. int MinInternalPingtimeMs;
  143. int MaxInternalPingtimeMs;
  144. SOCKADDR_IN Address;
  145. int ReliablePacketSendId;
  146. int UnreliablePacketSendId;
  147. int ReliablePacketRcvId;
  148. int UnreliablePacketRcvId;
  149. SList<cPacket> PacketList[4]; // list of all player objects
  150. int ListMax[4];
  151. int ListProcessingTime[4];
  152. unsigned long LastKeepaliveTimeMs;
  153. bool MustEvict;
  154. BOOL IsFlowControlEnabled;
  155. int LastServiceCount;
  156. int LastContactTime;
  157. int TargetBps;
  158. int MaximumBps;
  159. int Id;
  160. float BandwidthMultiplier;
  161. float AverageObjectPriority;
  162. bool IsLoading;
  163. bool ExpectPacketFlood;
  164. unsigned long FloodTimer;
  165. unsigned long WasLoading;
  166. unsigned long TotalResends;
  167. unsigned long CreationTime;
  168. int PriorityUpdateCounter;
  169. //
  170. // Variables for detecting outgoing packet floods.
  171. //
  172. unsigned long ExtendedAveragePingTime;
  173. int ExtendedAverageCount;
  174. int LastAveragePingTime;
  175. bool IsOutgoingFlooded;
  176. int TotalResentPacketsInQueue;
  177. unsigned long NextOutgoingFloodActionTime;
  178. int NumOutgoingFloods;
  179. static bool AllowExtraModemBandwidthThrottling;
  180. static int PriorityUpdateRate;
  181. };
  182. #endif // RHOST_H
  183. //
  184. // When this is set to true, sends to ALL will include this rhost.
  185. // This is needed to allow a slot to be reserved for a rhost before
  186. // using it extensively. Generally, when a client is accepted into a
  187. // game he wil need to do an extensive data load during which there
  188. // will be no network servicing. When that is finished he should signal
  189. // readyness.
  190. //
  191. //bool IsReadyForAllData;
  192. //int NumFailsInSampleTime;
  193. //int NumConsecutiveTPCorrectionsDownwards;
  194. //int NumConsecutiveTPCorrectionsUpwards;