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): keep network waits out of the reported logic rate

Olcay Seygan66eaaebf9616
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 06f0289f6..5af650ed6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -270,6 +270,7 @@ found and fixed — EA's own, not port damage.**
- Input delay is measured from simulation speed now, not your graphics card.
- One player's slow graphics card no longer slows the whole match. A network game runs at the pace of its slowest machine, and a machine that could only draw 16 pictures a second also only advanced the game 16 times a second, so everybody played in slow motion. It now skips pictures to keep up with the game, the way single player already did. Two machines on one desk: with one of them held to 16 pictures a second the match used to run at half speed, and with it held to 10 the match now runs at 28 of its 30 frames a second, the two machines agreeing on every frame.
- One lost packet costs a round trip instead of a flat two seconds.
+- An online match no longer settles at 20 frames a second. Every time a machine sat waiting for the other player's orders, the wait was counted as that machine being slow, the whole room was slowed down to match, and the slower room waited just as often. A two-player match on 2026-09-29 played its whole six minutes at 21. Waiting now counts as waiting: two machines at a 260 ms ping with 3% loss were told to run at 27 before and at 30 now, and play at 27.6 instead of 25.7.
- A link that drops the odd packet no longer drags the whole room into slow motion. The game held its outgoing packets back for up to half its input delay, replies included, so it measured every connection as far slower than it was and waited longer and longer for anything lost. Three players on a 50 ms link losing one packet in fifty used to sink from 30 frames a second to 13; they hold 21 now, and two players losing one in a hundred hold 27.
- A bad connection no longer turns the match into a slideshow. The orders each player sends now go out three times, 30 ms apart, so a lost packet is covered by the next one instead of by a resend that came most of a second later while everyone waited. Three players on 120 ms links losing one packet in twenty went from 6 frames a second to 19; two on 200 ms links losing one in ten went from 6 to 23, and the 50 ms room above from 21 to 26. The copies cost a few bytes and nothing on a clean line.
- Your clicks are answered sooner online. The game worked out how far ahead to schedule orders from the two slowest connections in the room and counted the host as one of them, so a two-player game waited out a whole round trip where half of one is the wire. At a 200 ms ping the delay between a click and the units moving drops from about 310 ms to 215; three players at 250 ms go from 500 to 370. Pings up to 300 ms and more are measured as they are now instead of being cut off at half a second, which left those links a window too short for them.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h
index f5f41d8c8..c883f3dfa 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h
@@ -119,6 +119,7 @@ public:
UnsignedInt getPacketArrivalCushion( void );
Int getMinimumCushion();
+ void addNetworkStall(time_t ms) { m_frameMetrics.addNetworkStall(ms); }
void flushConnections();
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h
index 898224701..f18faeb79 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h
@@ -43,6 +43,7 @@ public:
void doPerFrameMetrics(UnsignedInt frame);
void processLatencyResponse(UnsignedInt frame);
void addCushion(Int cushion);
+ void addNetworkStall(time_t ms); ///< wall time spent waiting on another machine's commands, not computing
Real getAverageLatency();
Int getAverageFPS();
@@ -53,6 +54,7 @@ protected:
// frames per second history variables.
Real *m_fpsList; ///< A record of how many game logic frames per second there were for the last 60 seconds.
time_t m_lastFpsTimeThing; ///< The time when the last fps entry started being recorded.
+ time_t m_fpsStallMS; ///< Network stall inside the current fps window; see doPerFrameMetrics.
UnsignedInt m_fpsStartingFrame; ///< The logic frame the window above opened on, so the sample is a logic rate.
Int m_fpsListIndex; ///< Index into the array of the current fps list entry being measured.
Real m_averageFps; ///< The current average logic fps, computed just like m_averageLatency below but with the fps numbers.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index 05be96b6a..c87f95d0b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -1411,12 +1411,14 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
}
msg->setAverageLatency(m_frameMetrics.getAverageLatency());
- // see above for explanation.
-// if (didSelfSlug) {
-// msg->setAverageFps(frameRate);
-// } else {
+ /* See above. EA left this half commented out, so only the packet router's own slug was
+ forgiven: the other seat's voluntary 10 % slowdown came back as its capacity and the
+ room was commanded down to it, a frame or two under 30 on every lossy link. */
+ if (didSelfSlug) {
+ msg->setAverageFps(frameRate);
+ } else {
msg->setAverageFps(m_frameMetrics.getAverageFPS());
-// }
+ }
if (didSelfSlug) {
//DEBUG_LOG(("ConnectionManager::updateRunAhead - average latency = %f, average fps = %d, actual fps = %d, didSelfSlug = true\n", m_frameMetrics.getAverageLatency(), m_frameMetrics.getAverageFPS(), m_frameMetrics.getAverageFPS()));
} else {
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
index 8f3041d20..cdc03f64d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp
@@ -52,6 +52,7 @@ FrameMetrics::FrameMetrics()
m_fpsListIndex = 0;
m_lastFpsTimeThing = 0;
m_fpsStartingFrame = 0;
+ m_fpsStallMS = 0;
m_minimumCushion = 0;
m_pendingLatencies = NEW time_t[MAX_FRAMES_AHEAD];
@@ -97,6 +98,7 @@ void FrameMetrics::init() {
// down for the first NetworkFPSHistoryLength seconds of the game.
m_lastFpsTimeThing = timeGetTime();
m_fpsStartingFrame = 0;
+ m_fpsStallMS = 0;
for (i = 0; i < TheGlobalData->m_networkLatencyHistoryLength; ++i) {
m_latencyList[i] = (Real)0.2;
}
@@ -126,7 +128,22 @@ void FrameMetrics::doPerFrameMetrics(UnsignedInt frame) {
time_t curTime = timeGetTime();
time_t windowMS = curTime - m_lastFpsTimeThing;
if (windowMS >= 1000) {
- Real logicFps = FrameMetrics_logicFpsSample(frame, m_fpsStartingFrame, windowMS);
+ /* Time spent waiting for another machine's commands is not this machine being slow, and
+ counting it latched the room: a stall on a real internet link read as a low logic rate,
+ the room was commanded down to it, the slower room stalled just as often (the run-ahead is
+ sized in frames of that same rate, so it covers the same wall time) and reported lower
+ again. A two player match on 2026-09-29 played 7835 frames in 371 seconds, 21 a
+ second, from start to end. The machine that is really slow never waits on anybody, so its
+ own rate still reaches minFps untouched. A quarter of the window is kept so a window
+ that was almost all stall cannot divide by nearly nothing, and the catch-up frames run
+ after a stall are capped at the game speed rather than reported as a faster machine. */
+ time_t busyMS = windowMS - m_fpsStallMS;
+ if (busyMS < windowMS / 4)
+ busyMS = windowMS / 4;
+ Real logicFps = FrameMetrics_logicFpsSample(frame, m_fpsStartingFrame, busyMS);
+ if (logicFps > (Real)TheGlobalData->m_framesPerSecondLimit)
+ logicFps = (Real)TheGlobalData->m_framesPerSecondLimit;
+ m_fpsStallMS = 0;
m_averageFps -= ((m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // subtract out the old value from the average.
m_fpsList[m_fpsListIndex] = logicFps;
m_averageFps += ((Real)(m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // add the new value to the average.
@@ -179,6 +196,11 @@ Real FrameMetrics::getAverageLatency() {
return m_averageLatency;
}
+void FrameMetrics::addNetworkStall(time_t ms) {
+ if (ms > 0)
+ m_fpsStallMS += ms;
+}
+
Int FrameMetrics::getMinimumCushion() {
return m_minimumCushion;
}
\ No newline at end of file
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
index f004d9c27..277aaf901 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
@@ -211,6 +211,7 @@ protected:
Int m_lastExecutionFrame; ///< The highest frame number that a command could have been executed on.
Int m_lastFrameCompleted;
Bool m_didSelfSlug;
+ time_t m_stallStart; ///< When the frame we are waiting on stopped being ready; 0 when not waiting.
__int64 m_perfCountFreq; ///< The frequency of the performance counter.
__int64 m_nextFrameTime; ///< When did we execute the last frame? For slugging the GameLogic...
@@ -277,6 +278,7 @@ Network::Network()
//Initializations inserted
m_checkCRCsThisFrame = FALSE;
m_didSelfSlug = FALSE;
+ m_stallStart = 0;
m_frameDataReady = FALSE;
m_sawCRCMismatch = FALSE;
//
@@ -344,6 +346,7 @@ void Network::init()
m_lastFrameCompleted = m_runAhead - 1; // subtract 1 since we're starting on frame 0
m_frameDataReady = FALSE;
m_didSelfSlug = FALSE;
+ m_stallStart = 0;
m_localStatus = NETLOCALSTATUS_PREGAME;
@@ -745,7 +748,19 @@ void Network::update( void )
endOfGameCheck();
}
- if (AllCommandsReady(TheGameLogic->getFrame())) { // If all the commands are ready for the next frame...
+ /* Wall time spent here waiting on somebody else's commands. FrameMetrics takes it out of the
+ logic rate this machine reports, which is a capacity, not a count of frames that happened. */
+ Bool ready = AllCommandsReady(TheGameLogic->getFrame());
+ if (m_conMgr != NULL && m_localStatus == NETLOCALSTATUS_INGAME) {
+ if (!ready && m_stallStart == 0) {
+ m_stallStart = timeGetTime();
+ } else if (ready && m_stallStart != 0) {
+ m_conMgr->addNetworkStall(timeGetTime() - m_stallStart);
+ m_stallStart = 0;
+ }
+ }
+
+ if (ready) { // If all the commands are ready for the next frame...
m_conMgr->handleAllCommandsReady();
// DEBUG_LOG(("Network::update - frame %d is ready\n", TheGameLogic->getFrame()));
if (timeForNewFrame()) { // This needs to come after any other pre-frame execution checks as this changes the timing variables.