Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamenetwork): stop a stall being measured as network latency

Olcay Seygan96ef5f18f0e2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 048abcac0..b87d431fc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -86,6 +86,8 @@ found and fixed — EA's own, not port damage.**
- A single lost packet no longer freezes the match for twenty seconds. The game now asks for the
missing frame back after a fifth of a second instead of waiting out the disconnect timer, so
nobody sits in front of a vote screen for a player who never went anywhere.
+- A freeze no longer costs everyone two seconds of input delay for the rest of the match: time
+ spent stalled is not mistaken for how slow the connection is.
- A player leaving is reported as a player leaving, not a desync.
- A desync now writes a per-object report on both machines for comparison.
- LAN refuses to start between two machines whose game files differ.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index 9160f591e..e95f31b63 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -93,6 +93,29 @@ enum
Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,
Int minRunAhead, Int maxRunAhead );
+/* A latency sample is the wall time between sending a frame's commands and the ack coming back, so
+ it measures the link only while both machines are running. Let the logic stop - a lost packet,
+ a player alt-tabbing, a map that took a moment - and the same stopwatch keeps running, and the
+ stall is filed as round trip time. It is not: it is the answer to a question the run-ahead is
+ not asking. A real match recorded 1.79 s and then 7.64 s on a link whose measured srtt was
+ 51 ms; the run-ahead sized on them went from its usual 5 frames to 29 and then 64 - 2.1 seconds
+ of input delay - and stayed there while the sample sat in the average.
+
+ So a sample past any plausible round trip is discarded down to the ceiling rather than trusted.
+ A link genuinely that slow is not playable anyway, and clamping costs nothing there; the point
+ is that a stall can no longer buy a permanent delay for everyone.
+
+ Half a second of round trip is past where this game is playable, and it is what the clamp
+ has to be worth: two players pinned at the ceiling still only buy 18 frames of
+ run-ahead - 0.6 s - against the 64 frames the raw samples bought. A link genuinely slower
+ than this loses at most 33 ms of one-way window, which RUNAHEAD_JITTER_FRAMES already covers.
+ ponytail: one ceiling for every link. The self-calibrating version is the connection's own
+ srtt + 4*rttvar (Connection.h has both), if a real link is ever found that this undersizes. */
+const Real MAX_PLAUSIBLE_LATENCY_SECONDS = 0.5f;
+
+/** Fold a raw round-trip measurement into the range a round trip can actually occupy. */
+Real sanitizeLatencySample( Real seconds, Real ceilingSeconds );
+
/* The room's logic rate is set by the packet router to the slowest rate any player reports, and
every machine then paces its own logic on it (Network::timeForNewFrame). The trap is that the
rate a player reports is the rate they *achieved*, and a player pinned at the room's rate
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index 7f5df4d33..91e4fba05 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -112,6 +112,20 @@ Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,
return frames;
}
+Real sanitizeLatencySample( Real seconds, Real ceilingSeconds )
+{
+ if( ceilingSeconds < 0.0f )
+ ceilingSeconds = 0.0f;
+
+ if( seconds < 0.0f )
+ return 0.0f; // the clock went backwards; no information, not a fast link
+
+ if( seconds > ceilingSeconds )
+ return ceilingSeconds;
+
+ return seconds;
+}
+
Int settleRoomFrameRate( Int minFps, Int fpsLimit )
{
if( minFps < ROOM_FRAME_RATE_FLOOR )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
index 4c744345b..8f3041d20 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
@@ -28,6 +28,7 @@
#include "GameNetwork/FrameMetrics.h"
#include "GameNetwork/NetworkUtil.h"
+#include "GameNetwork/CushionMetrics.h"
/** -----------------------------------------------------------------------------------------------
* The fps number the run-ahead equation wants: logic frames per second, from the logic frame the
@@ -148,7 +149,10 @@ void FrameMetrics::processLatencyResponse(UnsignedInt frame) {
Int latencyListIndex = frame % TheGlobalData->m_networkLatencyHistoryLength;
m_averageLatency -= m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
- m_latencyList[latencyListIndex] = (Real)timeDiff / (Real)1000; // convert to seconds from milliseconds.
+ /* A stalled frame keeps this stopwatch running long after the packet landed, and the number
+ comes back as round trip time. See sanitizeLatencySample in CushionMetrics.h. */
+ m_latencyList[latencyListIndex] = sanitizeLatencySample( (Real)timeDiff / (Real)1000,
+ MAX_PLAUSIBLE_LATENCY_SECONDS );
m_averageLatency += m_latencyList[latencyListIndex] / TheGlobalData->m_networkLatencyHistoryLength;
if (frame % 16 == 0) {
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 15f2ff413..607110313 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -3506,6 +3506,43 @@ TEST(the_run_ahead_covers_the_trip_it_is_sized_for)
}
}
+/* Measured in a real LAN match: the host lost one FRAMEINFO packet, sat on the frame for twenty
+ seconds, and its own latency samples came back as 1.79 s and then 7.64 s on a link whose srtt was
+ 51 ms. The run-ahead sized on them went 5 -> 29 -> 64 frames, which is 2.1 seconds of input
+ delay, and it stayed there while the samples sat in the average. */
+TEST(a_stalled_frame_is_not_filed_as_round_trip_time)
+{
+ /* an ordinary link passes through untouched - the clamp must not be a tax on healthy games */
+ CHECK_NEAR( sanitizeLatencySample( 0.051f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.051f, 0.0001f );
+ CHECK_NEAR( sanitizeLatencySample( 0.300f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.300f, 0.0001f );
+ CHECK( MAX_PLAUSIBLE_LATENCY_SECONDS <= 0.5f ); // a slower link than this is not a game
+ CHECK_NEAR( sanitizeLatencySample( MAX_PLAUSIBLE_LATENCY_SECONDS, MAX_PLAUSIBLE_LATENCY_SECONDS ),
+ MAX_PLAUSIBLE_LATENCY_SECONDS, 0.0001f );
+
+ /* the two the match actually recorded */
+ CHECK_NEAR( sanitizeLatencySample( 1.785224f, MAX_PLAUSIBLE_LATENCY_SECONDS ),
+ MAX_PLAUSIBLE_LATENCY_SECONDS, 0.0001f );
+ CHECK_NEAR( sanitizeLatencySample( 7.636834f, MAX_PLAUSIBLE_LATENCY_SECONDS ),
+ MAX_PLAUSIBLE_LATENCY_SECONDS, 0.0001f );
+
+ /* a clock that ran backwards says nothing; it must not read as a zero-latency link */
+ CHECK_NEAR( sanitizeLatencySample( -3.0f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.0f, 0.0001f );
+
+ /* and the point of all of it: what the run-ahead does with the clamped numbers. Two players at
+ the ceiling still cost far less than the 64 frames the raw samples bought. */
+ Real clamped = sanitizeLatencySample( 7.636834f, MAX_PLAUSIBLE_LATENCY_SECONDS )
+ + sanitizeLatencySample( 1.785224f, MAX_PLAUSIBLE_LATENCY_SECONDS );
+ Int runAhead = computeRunAhead( clamped, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 );
+ CHECK( runAhead < 64 );
+ CHECK( runAhead <= 18 ); // 0.6 s at 30 Hz, and only with both players at the ceiling
+ CHECK( runAhead <= (Int)(MAX_PLAUSIBLE_LATENCY_SECONDS * 30.0f * 1.2f) + RUNAHEAD_JITTER_FRAMES );
+
+ /* a LAN is untouched by any of this */
+ CHECK_EQ( computeRunAhead( sanitizeLatencySample( 0.051f, MAX_PLAUSIBLE_LATENCY_SECONDS ) * 2.0f,
+ 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 ),
+ computeRunAhead( 0.102f, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 ) );
+}
+
/* The other half of that match: nothing ever asked for the lost frame. FrameData escalates to
FRAMEDATA_RESEND only when it holds more commands than were announced, so a missing announcement
waits for ever - zero FRAMERESENDREQUEST commands went out in the whole game, and what freed it