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 refusing every resend in a match's first 65 frames

Olcay Seygan5c3ad2b4e3eb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1e4420f10..5b7ea2bec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -260,6 +260,15 @@ map. Ctrl+A is still everything at once.
on its own — from twelve frames a second to thirty in about six seconds. A room held down by a
machine that really is that slow settles just above it and stays there instead of surging and
falling back.
+- **The repair for a corrupted order was turned off for the first two seconds of every game.** When
+ a player's orders for a moment arrive damaged rather than late, the game throws them away and asks
+ that player to send them again — and that request is answered only if the moment in question is
+ still recent enough to be worth resending. The check for "recent enough" counted backwards from
+ the start of the match, which for the first couple of seconds gave an answer of roughly four
+ billion, so every request was turned down as ancient history. Those first seconds are precisely
+ when it matters: connections are still settling and everybody is giving their opening orders at
+ once. A stall there had to sit through the retry and then the disconnect screen instead of being
+ repaired on the spot. It counts properly now.
## Sharper textures, for free
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index 79f3c11d1..f06e956eb 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -38,6 +38,15 @@
negative: a command whose frame has already gone by is no margin at all, not a negative one. */
Int frameCushion( UnsignedInt executionFrame, UnsignedInt currentFrame );
+/* Frame numbers are UnsignedInt, so every "how far apart are these two frames" question in the
+ network layer is one subtraction away from four billion. The helpers here take the difference
+ as a signed Int, which is the same bit pattern read the way the question meant it. */
+
+/** Whether a frame is too far in the past to still be resendable. A frame we have not reached
+ yet is not old - we simply have nothing for it. "Past" means within half the frame counter's
+ range, which at 30 Hz is over four hundred days of play. */
+Bool frameIsTooOldToResend( UnsignedInt currentFrame, UnsignedInt requestedFrame, Int framesToKeep );
+
/** Whether to slow our own logic rate. A negative minimumCushion means no sample has been taken
yet, which is not the same as no margin - see the sentinel note in CushionMetrics.cpp. */
Bool shouldSelfSlug( Int minimumCushion, Int runAhead, UnsignedInt slackPercent );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index 124f1c812..39e97b3c0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -2457,7 +2457,7 @@ void ConnectionManager::sendFrameDataToPlayer(UnsignedInt playerID, UnsignedInt
}
void ConnectionManager::sendSingleFrameToPlayer(UnsignedInt playerID, UnsignedInt frame) {
- if ((TheGameLogic->getFrame() - FRAMES_TO_KEEP) > frame) {
+ if (frameIsTooOldToResend(TheGameLogic->getFrame(), frame, FRAMES_TO_KEEP)) {
DEBUG_LOG(("ConnectionManager::sendSingleFrameToPlayer - player %d requested frame %d when we are on frame %d, this is too far in the past.\n", playerID, frame, TheGameLogic->getFrame()));
return;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index ee0bbcb12..eb86a2849 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -38,6 +38,21 @@ Int frameCushion( UnsignedInt executionFrame, UnsignedInt currentFrame )
return diff;
}
+Bool frameIsTooOldToResend( UnsignedInt currentFrame, UnsignedInt requestedFrame, Int framesToKeep )
+{
+ /* EA wrote this as `if ((TheGameLogic->getFrame() - FRAMES_TO_KEEP) > frame)`, an UnsignedInt
+ subtraction with nothing under it: for the first FRAMES_TO_KEEP frames of a match it wraps to
+ around four billion, which is greater than any frame anyone can ask for, so every resend
+ request in the opening seconds was answered with "this is too far in the past" and dropped.
+ A stall at the start of a game therefore could not be repaired by the mechanism that exists
+ to repair it, and had to wait for the retry and then the disconnect screen instead. */
+ Int age = (Int)(currentFrame - requestedFrame); // signed difference, wraparound-safe
+ if( age < 0 )
+ return FALSE;
+
+ return age > framesToKeep;
+}
+
Bool shouldSelfSlug( Int minimumCushion, Int runAhead, UnsignedInt slackPercent )
{
/* Negative is FrameMetrics' "nothing measured yet", set by init() and never produced by a real
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 011bf5327..23bd18326 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2572,6 +2572,42 @@ TEST(room_frame_rate_still_pins_to_a_genuinely_slow_player_without_oscillating)
probeRoomFrameRate( ROOM_FRAME_RATE_FLOOR, fpsLimit ) );
}
+TEST(resend_window_is_measured_from_the_start_of_the_match_not_four_billion)
+{
+ /* ConnectionManager::sendSingleFrameToPlayer refuses to resend a frame older than
+ FRAMES_TO_KEEP, which is (MAX_FRAMES_AHEAD / 2) + 1 = 65 - about two seconds at 30 Hz, and
+ exactly the window a stalled player can be behind by, since nobody advances while the room
+ is waiting for them. */
+ const Int KEEP = 65;
+
+ // the frame we are on, and everything inside the window, is resendable
+ CHECK( !frameIsTooOldToResend( 1000, 1000, KEEP ) );
+ CHECK( !frameIsTooOldToResend( 1000, 999, KEEP ) );
+ CHECK( !frameIsTooOldToResend( 1000, 1000 - KEEP, KEEP ) );
+
+ // one frame past the window is not
+ CHECK( frameIsTooOldToResend( 1000, 1000 - KEEP - 1, KEEP ) );
+ CHECK( frameIsTooOldToResend( 1000, 0, KEEP ) );
+
+ /* The defect: EA computed `(currentFrame - FRAMES_TO_KEEP) > requestedFrame` on UnsignedInts,
+ so for the first 65 frames of every match the left side wrapped to about four billion and
+ every request was refused. A stall in the opening two seconds could not be repaired by the
+ resend at all - it had to wait for the retry and then the disconnect screen. */
+ CHECK( !frameIsTooOldToResend( 0, 0, KEEP ) );
+ CHECK( !frameIsTooOldToResend( 1, 0, KEEP ) );
+ CHECK( !frameIsTooOldToResend( 10, 3, KEEP ) );
+ CHECK( !frameIsTooOldToResend( KEEP, 0, KEEP ) );
+ CHECK( frameIsTooOldToResend( KEEP + 1, 0, KEEP ) );
+
+ // a frame we have not reached is not old; we simply have nothing for it yet
+ CHECK( !frameIsTooOldToResend( 100, 101, KEEP ) );
+ CHECK( !frameIsTooOldToResend( 100, 1000000, KEEP ) );
+
+ // and the frame counter itself wrapping does not reopen or close the window by accident
+ CHECK( !frameIsTooOldToResend( 5, 0xFFFFFFFEu, KEEP ) );
+ CHECK( frameIsTooOldToResend( 5, 0xFFFFFF00u, KEEP ) );
+}
+
TEST(selfslug_is_monotonic_in_the_cushion)
{
// once there is enough margin the answer must stay no, or the frame rate would oscillate