netstats.cpp 4.5 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. // Filename: netstats.cpp
  20. // Project: wwnet
  21. // Author: Tom Spencer-Smith
  22. // Date: Oct 1998
  23. // Description:
  24. //
  25. //------------------------------------------------------------------------------------
  26. #include "netstats.h" // I WANNA BE FIRST!
  27. #include "win.h"
  28. #include "systimer.h"
  29. #include "miscutil.h"
  30. #include "netutil.h"
  31. #include "wwdebug.h"
  32. //
  33. // class defines
  34. //
  35. //------------------------------------------------------------------------------------
  36. cNetStats::cNetStats() :
  37. LastUnreliablePacketId(-1),
  38. FreezePacketId(-1)
  39. {
  40. Init_Net_Stats();
  41. }
  42. //------------------------------------------------------------------------------------
  43. void cNetStats::Init_Net_Stats()
  44. {
  45. StartTime = TIMEGETTIME();
  46. SampleStartTime = StartTime;
  47. for (int statistic = 0; statistic < STAT_COUNT; statistic++) {
  48. StatTotal[statistic] = 0;
  49. StatAverage[statistic] = 0;
  50. StatSnapshot[statistic] = 0;
  51. StatMacroSnapshot[statistic] = 0;
  52. StatSample[statistic] = 0;
  53. StatMacroSample[statistic] = 0;
  54. }
  55. RemotePacketloss = 0;
  56. UnreliableCount = 0;
  57. FreezePacketId = LastUnreliablePacketId;
  58. RemoteServiceCount = 0;
  59. }
  60. //------------------------------------------------------------------------------------
  61. double cNetStats::Get_Pc_Packetloss_Received() const
  62. {
  63. double packetloss_pc = 0;
  64. int packet_count = LastUnreliablePacketId - FreezePacketId;
  65. WWASSERT(packet_count >= 0);
  66. if (packet_count > 0) {
  67. //
  68. // Must add 1 to PrevLastUnreliable because packet id starts at 0
  69. //
  70. packetloss_pc = 100 * (1 - UnreliableCount / (double) packet_count);
  71. }
  72. return packetloss_pc;
  73. }
  74. //------------------------------------------------------------------------------------
  75. void cNetStats::Set_Pc_Packetloss_Sent(double packetloss_pc)
  76. {
  77. /*TSS102901
  78. WWASSERT(packetloss_pc > -MISCUTIL_EPSILON && packetloss_pc < 100 + MISCUTIL_EPSILON);
  79. RemotePacketloss = packetloss_pc;
  80. */
  81. }
  82. //------------------------------------------------------------------------------------
  83. void cNetStats::Set_Remote_Service_Count(int remote_service_count)
  84. {
  85. //WWASSERT(remote_service_count >= 0);
  86. RemoteServiceCount = remote_service_count;
  87. }
  88. //------------------------------------------------------------------------------------
  89. bool cNetStats::Update_If_Sample_Done(int this_frame_time, bool force_update)
  90. {
  91. bool is_updated = false;
  92. static int update_count = 0;
  93. if (force_update || this_frame_time - SampleStartTime > cNetUtil::NETSTATS_SAMPLE_TIME_MS) {
  94. update_count++;
  95. double total_time = (this_frame_time - StartTime) / 1000.0;
  96. if (total_time < MISCUTIL_EPSILON) {
  97. total_time = MISCUTIL_EPSILON;
  98. }
  99. for (int statistic = 0; statistic < STAT_COUNT; statistic++) {
  100. StatTotal[statistic] += StatSample[statistic];
  101. StatAverage[statistic] = (UINT) (StatTotal[statistic] / total_time);
  102. StatSnapshot[statistic] = StatSample[statistic];
  103. StatMacroSample[statistic] += StatSample[statistic];
  104. StatSample[statistic] = 0;
  105. if (update_count % 4 == 0) {
  106. StatMacroSnapshot[statistic] = StatMacroSample[statistic];
  107. StatMacroSample[statistic] = 0;
  108. }
  109. }
  110. SampleStartTime = this_frame_time;
  111. is_updated = true;
  112. }
  113. return is_updated;
  114. }
  115. //const USHORT cNetStats::SAMPLE_TIME = 500;
  116. // crash here when exit server thread before client
  117. //WWASSERT(packetloss_pc >= 0 && packetloss_pc <= 100);
  118. /*
  119. //
  120. // These 2 stats are computed from others
  121. //
  122. if (StatSample[STAT_AppByteSent] > 0) {
  123. StatSample[STAT_AppDataSentPc] = (UINT) (100 * StatSample[STAT_AppByteSent] /
  124. (double) (StatSample[STAT_AppByteSent] + StatSample[STAT_HdrByteSent]));
  125. }
  126. if (StatSample[STAT_AppByteRcv] > 0) {
  127. StatSample[STAT_AppDataRcvPc] = (UINT) (100 * StatSample[STAT_AppByteRcv] /
  128. (double) (StatSample[STAT_AppByteRcv] + StatSample[STAT_HdrByteRcv]));
  129. }
  130. */