clientpingmanager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/clientpingmanager.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 12/09/01 6:40p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "clientpingmanager.h"
  36. #include <windows.h>
  37. #include "systimer.h"
  38. #include "gamemode.h"
  39. #include "cspingrequestevent.h"
  40. #include "cnetwork.h"
  41. //
  42. // Class statics
  43. //
  44. int cClientPingManager::PingNumber = 0;
  45. DWORD cClientPingManager::TimeSentMs = 0;
  46. DWORD cClientPingManager::LastRoundTripPingMs = 0;
  47. DWORD cClientPingManager::AvgRoundTripPingMs = 0;
  48. bool cClientPingManager::IsAwaitingResponse = false;
  49. DWORD cClientPingManager::RoundTripPingSamplesMs[];
  50. //-----------------------------------------------------------------------------
  51. void
  52. cClientPingManager::Init
  53. (
  54. void
  55. )
  56. {
  57. PingNumber = 0;
  58. TimeSentMs = 0;
  59. LastRoundTripPingMs = 0;
  60. AvgRoundTripPingMs = 0;
  61. IsAwaitingResponse = false;
  62. for (int i = 0; i < MAX_SAMPLES; i++)
  63. {
  64. RoundTripPingSamplesMs[i] = 0;
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. void
  69. cClientPingManager::Think
  70. (
  71. void
  72. )
  73. {
  74. if (GameModeManager::Find("Combat")->Is_Active() && cNetwork::I_Am_Only_Client())
  75. {
  76. if (!IsAwaitingResponse)
  77. {
  78. DWORD time_now_ms = TIMEGETTIME();
  79. if (time_now_ms - TimeSentMs >= MIN_PING_DELAY_MS)
  80. {
  81. PingNumber++;
  82. TimeSentMs = time_now_ms;
  83. IsAwaitingResponse = true;
  84. cCsPingRequestEvent * p_event = new cCsPingRequestEvent;
  85. p_event->Init(PingNumber);
  86. }
  87. }
  88. //
  89. // Propagate latency data to combat
  90. //
  91. CombatManager::Set_Last_Round_Trip_Ping_Ms(LastRoundTripPingMs);
  92. CombatManager::Set_Avg_Round_Trip_Ping_Ms(AvgRoundTripPingMs);
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. DWORD
  97. cClientPingManager::Get_Last_Round_Trip_Ping_Ms
  98. (
  99. void
  100. )
  101. {
  102. return LastRoundTripPingMs;
  103. }
  104. //-----------------------------------------------------------------------------
  105. DWORD
  106. cClientPingManager::Get_Avg_Round_Trip_Ping_Ms
  107. (
  108. void
  109. )
  110. {
  111. return AvgRoundTripPingMs;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void
  115. cClientPingManager::Compute_Average_Round_Trip_Ping_Ms
  116. (
  117. void
  118. )
  119. {
  120. //
  121. // This is the average of MAX_SAMPLES non-zero pings.
  122. //
  123. AvgRoundTripPingMs = 0;
  124. DWORD num_pings = 0;
  125. DWORD total_ping = 0;
  126. for (int i = 0; i < MAX_SAMPLES; i++)
  127. {
  128. if (RoundTripPingSamplesMs[i] != 0)
  129. {
  130. num_pings++;
  131. total_ping += RoundTripPingSamplesMs[i];
  132. }
  133. }
  134. if (num_pings > 0)
  135. {
  136. AvgRoundTripPingMs = (DWORD)(total_ping / (float) num_pings);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. void
  141. cClientPingManager::Response_Received
  142. (
  143. int ping_number
  144. )
  145. {
  146. if (ping_number == PingNumber)
  147. {
  148. LastRoundTripPingMs = TIMEGETTIME() - TimeSentMs;
  149. RoundTripPingSamplesMs[PingNumber % MAX_SAMPLES] = LastRoundTripPingMs;
  150. Compute_Average_Round_Trip_Ping_Ms();
  151. IsAwaitingResponse = false;
  152. }
  153. else
  154. {
  155. WWDEBUG_SAY(("WARNING: cClientPingManager::Response_Received ping number mismatch (%d, %d)\n",
  156. ping_number, PingNumber));
  157. }
  158. }