Skip to content

Commit 831b810

Browse files
authored
fix(network): Fix slightly unstable latency calculation in FrameMetrics::processLatencyResponse() (TheSuperHackers#2200)
1 parent d3b62e8 commit 831b810

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Core/GameEngine/Source/GameNetwork/FrameMetrics.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
2828

29+
#include <numeric>
30+
2931
#include "GameNetwork/FrameMetrics.h"
3032
#include "GameClient/Display.h"
3133
#include "GameNetwork/networkutil.h"
@@ -105,9 +107,9 @@ void FrameMetrics::processLatencyResponse(UnsignedInt frame) {
105107
time_t timeDiff = curTime - m_pendingLatencies[pendingIndex];
106108

107109
Int latencyListIndex = frame % TheGlobalData->m_networkLatencyHistoryLength;
108-
m_averageLatency -= m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
109110
m_latencyList[latencyListIndex] = (Real)timeDiff / (Real)1000; // convert to seconds from milliseconds.
110-
m_averageLatency += m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
111+
const Real latencySum = std::accumulate(m_latencyList, m_latencyList + TheGlobalData->m_networkLatencyHistoryLength, 0.0f);
112+
m_averageLatency = latencySum / (Real)TheGlobalData->m_networkLatencyHistoryLength;
111113

112114
if (frame % 16 == 0) {
113115
// DEBUG_LOG(("ConnectionManager::processFrameInfoAck - average latency = %f", m_averageLatency));

0 commit comments

Comments
 (0)