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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gamenetwork): ask for a frame whose announcement was lost
Olcay Seygan11035cbc0ee7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d8fce5316..048abcac0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -83,6 +83,9 @@ found and fixed — EA's own, not port damage.**- Input delay is measured from simulation speed now, not your graphics card.- One lost packet costs a round trip instead of a flat two seconds.- A desynced match stops at once instead of playing on as two games.+- 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 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/Connection.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.hindex 7ca907ee4..f7e5ae42e 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h@@ -116,6 +116,10 @@ public:void setQuitting( void );Bool isQuitting( void ) { return m_isQuitting; }+ /** This connection's measured retry timeout, in milliseconds. How long a packet on this link is+ given before it is worth sending again - see FrameResendPolicy.h. */+ UnsignedInt getRetryTimeMS( void ) const { return (UnsignedInt)m_retryTime; }+#if defined(_DEBUG) || defined(_INTERNAL)void debugPrintCommands();#endifdiff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.hindex e8892f00c..19c49504a 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h@@ -199,6 +199,12 @@ private:DisconnectManager *m_disconnectManager; ///< Controls the disconnect dialog.+ /* The frame the logic is currently stuck on, and how long it has been stuck, so a frame whose+ FRAMEINFO was lost can be asked for again instead of waited out - see FrameResendPolicy.h. */+ UnsignedInt m_resendWatchFrame; ///< frame allCommandsReady last blocked on+ time_t m_resendWatchSince; ///< when it started blocking+ time_t m_resendLastRequest; ///< when we last asked for it; 0 = not asked+FrameDataManager *m_frameData[MAX_SLOTS];NetCommandList *m_pendingCommands;diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameResendPolicy.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameResendPolicy.hnew file mode 100644index 000000000..c4f508f1c--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameResendPolicy.h@@ -0,0 +1,75 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// FILE: FrameResendPolicy.h //////////////////////////////////////////////////////////////////////+// Desc: When to ask a player again for a frame that never arrived.+///////////////////////////////////////////////////////////////////////////////////////////////////++#pragma once++#ifndef _FRAME_RESEND_POLICY_H_+#define _FRAME_RESEND_POLICY_H_++#include "Lib/BaseType.h"++/* A frame's command count arrives exactly once, in a FRAMEINFO packet. Until it does, FrameData+ has nothing to compare its command list against and reports FRAMEDATA_NOTREADY - and the logic+ waits. That is correct while the packet is in flight and wrong the moment it is lost, because+ FrameData only ever escalates to FRAMEDATA_RESEND when it holds *more* commands than were+ announced. The case where the announcement itself never came has no exit, and+ ConnectionManager::requestFrameDataResend - which exists, and which the other machine answers -+ was reachable from nowhere else.++ A real match: the host sat on execution frame 30 for twenty seconds with frames 31 through 59+ already in hand, on a link measuring 51 ms round trip and 227 ms retry timeout, and sent zero+ resend requests in the whole game. What finally freed it was the disconnect screen's own+ handshake, which by design waits out NetworkStallCeilingTime - so a single dropped packet cost+ twenty seconds and an accusing vote screen for a player who was never gone.++ Asking is cheap; asking too eagerly is not. Every frame in the run-ahead window is legitimately+ "not ready" for a while, so a request before the packet has had time to arrive is pure traffic,+ and traffic is what loses packets. The wait is therefore sized on the link the game measured+ for itself, not on a constant: give the packet two of that connection's retry timeouts to show+ up, then ask, and keep asking no more often than that. */++enum+{+ /* The measured retry timeout is derived from a smoothed round trip and can read absurdly low on+ a LAN or high after a hitch. These bound what it is allowed to buy. */+ FRAME_RESEND_MIN_TIMEOUT_MS = 100,+ FRAME_RESEND_MAX_TIMEOUT_MS = 1000,++ /* How many retry timeouts a missing frame gets before we speak up. */+ FRAME_RESEND_TIMEOUTS_TO_WAIT = 2,+};++/**+ * Should we ask this player to send the blocking frame again?+ *+ * @param stalledMS how long the logic has been unable to execute this frame+ * @param sinceLastRequestMS how long since we last asked for it; pass stalledMS when we have+ * not asked at all, which is what "no request yet" means here+ * @param retryTimeoutMS the connection's own measured retry timeout+ */+Bool shouldRequestFrameResend( UnsignedInt stalledMS, UnsignedInt sinceLastRequestMS,+ UnsignedInt retryTimeoutMS );++/** The wait the rule above is built on, exposed so a caller can log what it is waiting for. */+UnsignedInt frameResendWaitMS( UnsignedInt retryTimeoutMS );++#endif // _FRAME_RESEND_POLICY_H_diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex 9e3449215..f1db41b06 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -42,6 +42,7 @@#include "GameClient/GameText.h"#include "GameClient/MessageBox.h"#include "GameNetwork/ConnectionManager.h"+#include "GameNetwork/FrameResendPolicy.h"#include "GameNetwork/StallJudgement.h"#include "GameNetwork/KeepAliveSchedule.h"#include "GameNetwork/CushionMetrics.h"@@ -147,6 +148,9 @@ ConnectionManager::ConnectionManager(void)m_netCommandWrapperList = NULL;m_localUser = NULL;m_localUser = newInstance(User);+ m_resendWatchFrame = 0;+ m_resendWatchSince = 0;+ m_resendLastRequest = 0;}/**@@ -165,6 +169,12 @@ void ConnectionManager::init()m_connections[i] = NULL;}+ /* A fresh game means nothing is stuck yet; leaving the old stamps behind would make the+ first blocking frame of the new match look like it had been waiting since the last one. */+ m_resendWatchFrame = 0;+ m_resendWatchSince = 0;+ m_resendLastRequest = 0;+if (m_pendingCommands == NULL) {m_pendingCommands = newInstance(NetCommandList);m_pendingCommands->init();@@ -1587,7 +1597,9 @@ Int commandsReadyDebugSpewage = 0;*/Bool ConnectionManager::allCommandsReady(UnsignedInt frame, Bool justTesting /* = FALSE */) {Bool retval = TRUE;- FrameDataReturnType frameRetVal;+ /* Read after the loop, and the loop can exit before ever assigning it. */+ FrameDataReturnType frameRetVal = FRAMEDATA_READY;+ Int notReadyPlayer = -1;// retval = FALSE; // ****for testing purposes only!!!!!!****// i is used after the loop; VC6 for-scope let it escape.Int i;@@ -1607,6 +1619,7 @@ Bool ConnectionManager::allCommandsReady(UnsignedInt frame, Bool justTesting /*frameRetVal = m_frameData[i]->allCommandsReady(frame, (frame != commandsReadyDebugSpewage) && (justTesting == FALSE));if (frameRetVal == FRAMEDATA_NOTREADY) {+ notReadyPlayer = i;retval = FALSE;} else if (frameRetVal == FRAMEDATA_RESEND) {requestFrameDataResend(i, frame);@@ -1624,6 +1637,37 @@ Bool ConnectionManager::allCommandsReady(UnsignedInt frame, Bool justTesting /*}}+ /* A frame that never got its FRAMEINFO stays NOTREADY for ever on its own - FrameData escalates+ to FRAMEDATA_RESEND only when it holds more commands than were announced, never when the+ announcement is the thing that was lost. Ask for it. See FrameResendPolicy.h for why the+ wait is the connection's own retry timeout rather than a constant. */+ if ((notReadyPlayer >= 0) && (justTesting == FALSE)) {+ time_t now = timeGetTime();++ if (frame != m_resendWatchFrame) {+ m_resendWatchFrame = frame;+ m_resendWatchSince = now;+ m_resendLastRequest = 0; // nothing asked for this frame yet+ }++ UnsignedInt stalledMS = (UnsignedInt)(now - m_resendWatchSince);+ UnsignedInt sinceLastRequestMS = (m_resendLastRequest == 0)+ ? stalledMS+ : (UnsignedInt)(now - m_resendLastRequest);++ UnsignedInt retryTimeoutMS = FRAME_RESEND_MIN_TIMEOUT_MS;+ if (m_connections[notReadyPlayer] != NULL) {+ retryTimeoutMS = m_connections[notReadyPlayer]->getRetryTimeMS();+ }++ if (shouldRequestFrameResend(stalledMS, sinceLastRequestMS, retryTimeoutMS)) {+ DEBUG_LOG(("ConnectionManager::allCommandsReady - frame %d still missing from player %d after %d ms, asking again (retry timeout %d ms)\n",+ frame, notReadyPlayer, stalledMS, retryTimeoutMS));+ requestFrameDataResend(notReadyPlayer, frame);+ m_resendLastRequest = now;+ }+ }+if ((retval == TRUE) && (justTesting == FALSE)) {m_disconnectManager->allCommandsReady(TheGameLogic->getFrame(), this);retval = m_disconnectManager->allowedToContinue(); // allow the disconnect manager to keep us on this framediff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameResendPolicy.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameResendPolicy.cppnew file mode 100644index 000000000..876f757b1--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameResendPolicy.cpp@@ -0,0 +1,51 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// FILE: FrameResendPolicy.cpp ////////////////////////////////////////////////////////////////////++#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine++#include "GameNetwork/FrameResendPolicy.h"++UnsignedInt frameResendWaitMS( UnsignedInt retryTimeoutMS )+{+ if( retryTimeoutMS < FRAME_RESEND_MIN_TIMEOUT_MS )+ retryTimeoutMS = FRAME_RESEND_MIN_TIMEOUT_MS;++ if( retryTimeoutMS > FRAME_RESEND_MAX_TIMEOUT_MS )+ retryTimeoutMS = FRAME_RESEND_MAX_TIMEOUT_MS;++ return retryTimeoutMS * FRAME_RESEND_TIMEOUTS_TO_WAIT;+}++Bool shouldRequestFrameResend( UnsignedInt stalledMS, UnsignedInt sinceLastRequestMS,+ UnsignedInt retryTimeoutMS )+{+ UnsignedInt wait = frameResendWaitMS( retryTimeoutMS );++ /* The packet has not had time to arrive yet. Everything in the run-ahead window passes through+ this state on its way to being executed. */+ if( stalledMS < wait )+ return FALSE;++ /* We have already asked, recently. The answer may well be on the wire. */+ if( sinceLastRequestMS < wait )+ return FALSE;++ return TRUE;+}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 2c19fb22b..15f2ff413 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -33,6 +33,7 @@#include "GameNetwork/Connection.h"#include "GameLogic/CRCSnapshotRing.h"#include "GameNetwork/GameDataMatch.h"+#include "GameNetwork/FrameResendPolicy.h"#include "GameLogic/FPUControl.h"#include "GameNetwork/StallJudgement.h"#include "GameNetwork/KeepAliveSchedule.h"@@ -3505,6 +3506,41 @@ TEST(the_run_ahead_covers_the_trip_it_is_sized_for)}}+/* 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+ was the disconnect screen's twenty-second handshake. */+TEST(a_missing_frame_is_asked_for_again_once_the_wire_has_had_its_chance)+{+ const UnsignedInt retry = 227; // the retry timeout that match measured+ const UnsignedInt wait = frameResendWaitMS( retry );++ /* Every frame in the run-ahead window is legitimately not ready for a while. Asking inside that+ window is pure traffic, and traffic is what loses packets in the first place. */+ CHECK( shouldRequestFrameResend( 0, 0, retry ) == FALSE );+ CHECK( shouldRequestFrameResend( wait - 1, wait - 1, retry ) == FALSE );++ /* past it, ask - "not asked yet" is sinceLastRequestMS == stalledMS */+ CHECK( shouldRequestFrameResend( wait, wait, retry ) == TRUE );+ CHECK( shouldRequestFrameResend( 20000, 20000, retry ) == TRUE );++ /* having asked, do not ask again until the answer has had time to arrive */+ CHECK( shouldRequestFrameResend( 20000, 0, retry ) == FALSE );+ CHECK( shouldRequestFrameResend( 20000, wait - 1, retry ) == FALSE );+ CHECK( shouldRequestFrameResend( 20000, wait, retry ) == TRUE );++ /* the wait comes from the link, not from a constant, and a wild measurement cannot run away+ with it in either direction */+ CHECK( frameResendWaitMS( 500 ) > frameResendWaitMS( 120 ) );+ CHECK_EQ( (Int)frameResendWaitMS( 0 ),+ (Int)(FRAME_RESEND_MIN_TIMEOUT_MS * FRAME_RESEND_TIMEOUTS_TO_WAIT) );+ CHECK_EQ( (Int)frameResendWaitMS( 999999 ),+ (Int)(FRAME_RESEND_MAX_TIMEOUT_MS * FRAME_RESEND_TIMEOUTS_TO_WAIT) );++ /* and the whole point: the twenty seconds that match lost become one wait */+ CHECK( wait < 1000 );+}+TEST(the_run_ahead_carries_a_margin_the_percentage_alone_cannot_give){/* An average round trip is exceeded half the time, so a window sized to exactly the average is