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): make NetworkKeepAliveDelay a live, bounded knob
Olcay Seygan2e6e61c90ab2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 766668a18..ded3f2973 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -218,6 +218,12 @@ map. Ctrl+A is still everything at once.has actually stopped talking before it does that. If everybody is still sending, it waits andcatches up. If a player really has gone quiet, the screen comes up as before, and there is ahard limit so a genuinely wedged game still gives you the screen rather than hanging forever.+- **The setting that keeps your connection alive through a router was never being read.** Every+ online game sends a small packet to each player now and then purely so the router in between does+ not quietly close the door it opened for them. How often that happens was a configurable number+ that the game printed at startup and then ignored. It is a real setting now, and it is kept inside+ the range that actually keeps the door open — a game can ask for these packets more often on a+ fussy connection, but nothing can set it so far apart that the connection lapses between them.## Sharper textures, for freediff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.hindex 32a63b9a7..145805740 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h@@ -215,6 +215,11 @@ private:/* Wall clock of the last thing we heard from each player, zero for "nothing yet". */time_t m_lastHeardFrom[MAX_SLOTS];++ /* Where the keep-alive round has got to. These were function statics in doKeepAlive, so they+ survived across games and across ConnectionManager instances. */+ time_t m_keepAliveRoundStart;+ Int m_keepAliveNextSlot;Int m_minFpsPlayer;Int m_minFps;UnsignedInt m_smallestPacketArrivalCushion;diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/KeepAliveSchedule.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/KeepAliveSchedule.hnew file mode 100644index 000000000..5aa7f8e81--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/KeepAliveSchedule.h@@ -0,0 +1,64 @@+/*+** 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: KeepAliveSchedule.h //////////////////////////////////////////////////////////////////////+// Desc: When each player's keep-alive packet is due.+///////////////////////////////////////////////////////////////////////////////////////////////////++#pragma once++#ifndef _KEEP_ALIVE_SCHEDULE_H_+#define _KEEP_ALIVE_SCHEDULE_H_++#include "Lib/BaseType.h"++/* A keep-alive is a packet with nothing in it, sent to a player we have nothing to say to. Its+ only job is to keep the hole open: a NAT drops the UDP mapping it made for our connection after+ an idle timeout, and once that is gone the other machine's packets have nowhere to land. The+ comment in NetworkDefs.h says the interval "should be less than 30 just to keep firewall ports+ open", and consumer routers of the era were commonly at 20 s or less - which is why the round+ is bounded below that whatever the INI asks for.++ They are staggered rather than sent together: one slot's worth of the round each, so eight+ keep-alives do not leave the machine in the same tick as each other and the frame's commands.++ NetworkKeepAliveDelay names the round in seconds. EA parsed it, logged it, and then never read+ it - ConnectionManager::doKeepAlive counted whole seconds against MAX_SLOTS in two function+ statics instead, which made the real interval a hardcoded 7-8 s and the knob dead. These+ functions are what doKeepAlive now runs on, and they are here rather than in the middle of+ ConnectionManager so a test can drive them without a socket. */++/** The round length in milliseconds, from the configured NetworkKeepAliveDelay in seconds. */+UnsignedInt keepAliveRoundMS( UnsignedInt configuredSeconds );++/** How many of maxSlots slots are due to have been sent by elapsedMS into a round of roundMS.+ Slot 0 is due immediately, and the last slot one slot-width before the round ends. */+Int keepAliveSlotsDue( UnsignedInt elapsedMS, UnsignedInt roundMS, Int maxSlots );++/** Whether the round is over and the next one should start. */+Bool keepAliveRoundIsOver( UnsignedInt elapsedMS, UnsignedInt roundMS );++/** The bounds keepAliveRoundMS clamps to, in seconds. */+enum+{+ KEEPALIVE_MIN_ROUND_SECONDS = 2, ///< below this we are spending bandwidth on nothing+ KEEPALIVE_MAX_ROUND_SECONDS = 8, ///< the interval EA's counting loop actually produced, and+ ///< comfortably under the shortest NAT timeouts in the wild+};++#endif // _KEEP_ALIVE_SCHEDULE_H_diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex d53a99db2..e5bd4f91a 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -43,6 +43,7 @@#include "GameClient/MessageBox.h"#include "GameNetwork/ConnectionManager.h"#include "GameNetwork/StallJudgement.h"+#include "GameNetwork/KeepAliveSchedule.h"#include "GameNetwork/LANAPICallbacks.h"#include "GameNetwork/NAT.h"#include "GameNetwork/NetCommandWrapperList.h"@@ -134,6 +135,8 @@ ConnectionManager::ConnectionManager(void)m_frameData[i] = NULL;m_lastHeardFrom[i] = 0;}+ m_keepAliveRoundStart = 0;+ m_keepAliveNextSlot = 0;m_transport = NULL;m_disconnectManager = NULL;m_pendingCommands = NULL;@@ -178,6 +181,8 @@ void ConnectionManager::init()for (i = 0; i < MAX_SLOTS; ++i) {m_lastHeardFrom[i] = 0;}+ m_keepAliveRoundStart = 0;+ m_keepAliveNextSlot = 0;m_localSlot = -1;#ifdef MEMORYPOOL_DEBUG@@ -1689,35 +1694,37 @@ void ConnectionManager::determineRouterFallbackPlan() {*/void ConnectionManager::doKeepAlive() {- static Int nextIndex = 0;- static time_t startTime = 0;-time_t curTime = timeGetTime();- if (startTime == 0) {- startTime = curTime;- return;+ if (m_keepAliveRoundStart == 0) {+ m_keepAliveRoundStart = curTime;+ m_keepAliveNextSlot = 0;}- time_t numSeconds = (curTime - startTime) / 1000;+ /* EA counted whole seconds against MAX_SLOTS here, in two function statics, which made the+ interval a hardcoded 7-8 s and left NetworkKeepAliveDelay parsed, logged and never read.+ The round is the knob now, bounded - see KeepAliveSchedule.h. */+ UnsignedInt roundMS = keepAliveRoundMS( TheGlobalData->m_networkKeepAliveDelay );+ UnsignedInt elapsedMS = (UnsignedInt)(curTime - m_keepAliveRoundStart);+ Int due = keepAliveSlotsDue( elapsedMS, roundMS, MAX_SLOTS );- while ((nextIndex <= numSeconds) && (nextIndex < MAX_SLOTS)) {-// DEBUG_LOG(("ConnectionManager::doKeepAlive - trying to send keep alive message to player %d\n", nextIndex));- if (m_connections[nextIndex] != NULL) {+ while (m_keepAliveNextSlot < due) {+ if (m_connections[m_keepAliveNextSlot] != NULL) {NetKeepAliveCommandMsg *msg = newInstance(NetKeepAliveCommandMsg);msg->setPlayerID(m_localSlot);if (DoesCommandRequireACommandID(msg->getNetCommandType()) == TRUE) {msg->setID(GenerateNextCommandID());}-// DEBUG_LOG(("ConnectionManager::doKeepAlive - sending keep alive message to player %d\n", nextIndex));- sendLocalCommandDirect(msg, 1 << nextIndex);+// DEBUG_LOG(("ConnectionManager::doKeepAlive - sending keep alive message to player %d\n", m_keepAliveNextSlot));+ sendLocalCommandDirect(msg, 1 << m_keepAliveNextSlot);msg->detach();}- ++nextIndex;+ ++m_keepAliveNextSlot;}- if (nextIndex == MAX_SLOTS) {- nextIndex = 0;- startTime = curTime;++ if (keepAliveRoundIsOver( elapsedMS, roundMS )) {+ m_keepAliveNextSlot = 0;+ m_keepAliveRoundStart = curTime;}}diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/KeepAliveSchedule.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/KeepAliveSchedule.cppnew file mode 100644index 000000000..9613631e1--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/KeepAliveSchedule.cpp@@ -0,0 +1,59 @@+/*+** 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: KeepAliveSchedule.cpp ////////////////////////////////////////////////////////////////////+// Desc: When each player's keep-alive packet is due.+///////////////////////////////////////////////////////////////////////////////////////////////////++#include "PreRTS.h"+#include "GameNetwork/KeepAliveSchedule.h"++UnsignedInt keepAliveRoundMS( UnsignedInt configuredSeconds )+{+ /* The knob comes out of an INI file, so it can be anything, including zero. Clamping rather+ than trusting it is the point of having the round in one place: too short wastes bandwidth+ on a full lobby, too long and the NAT mapping is gone before we refresh it, which is the+ failure this packet exists to prevent. */+ if( configuredSeconds < KEEPALIVE_MIN_ROUND_SECONDS )+ configuredSeconds = KEEPALIVE_MIN_ROUND_SECONDS;+ if( configuredSeconds > KEEPALIVE_MAX_ROUND_SECONDS )+ configuredSeconds = KEEPALIVE_MAX_ROUND_SECONDS;++ return configuredSeconds * 1000;+}++Int keepAliveSlotsDue( UnsignedInt elapsedMS, UnsignedInt roundMS, Int maxSlots )+{+ if( maxSlots <= 0 )+ return 0;++ UnsignedInt perSlot = roundMS / (UnsignedInt)maxSlots;+ if( perSlot == 0 )+ return maxSlots; // a round shorter than one slot: everybody is due at once++ Int due = (Int)(elapsedMS / perSlot) + 1; // slot 0 is due at elapsed zero+ if( due > maxSlots )+ due = maxSlots;++ return due;+}++Bool keepAliveRoundIsOver( UnsignedInt elapsedMS, UnsignedInt roundMS )+{+ return elapsedMS >= roundMS;+}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 882773409..30781cd06 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -32,6 +32,7 @@#include "GameNetwork/GameDataMatch.h"#include "GameLogic/FPUControl.h"#include "GameNetwork/StallJudgement.h"+#include "GameNetwork/KeepAliveSchedule.h"#include <float.h>#include "GameClient/Water.h"#include "GameLogic/Module/PhysicsUpdate.h"@@ -2232,3 +2233,80 @@ TEST(judgestall_the_verdict_is_monotonic_in_the_stall_time)CHECK( strlen( stallVerdictName( STALL_SILENT ) ) > 0 );CHECK( strlen( stallVerdictName( STALL_WEDGED ) ) > 0 );}++// ---------------------------------------------------------------------------------------------+// The keep-alive round (MULTIPLAYER 2.4). NetworkKeepAliveDelay was parsed, logged and never+// read; doKeepAlive counted whole seconds against MAX_SLOTS in two function statics instead.+// ---------------------------------------------------------------------------------------------++TEST(keepalive_the_round_is_bounded_whatever_the_ini_says)+{+ // the shipped GameData.ini asks for 20 s, which is exactly where consumer NAT mappings of the+ // era expired - refreshing the hole at the moment it closes is no refresh at all+ CHECK_EQ( keepAliveRoundMS( 20 ), (UnsignedInt)KEEPALIVE_MAX_ROUND_SECONDS * 1000 );+ CHECK_EQ( keepAliveRoundMS( 3600 ), (UnsignedInt)KEEPALIVE_MAX_ROUND_SECONDS * 1000 );++ // and a zero would be a divide by zero one function along+ CHECK_EQ( keepAliveRoundMS( 0 ), (UnsignedInt)KEEPALIVE_MIN_ROUND_SECONDS * 1000 );+ CHECK_EQ( keepAliveRoundMS( 1 ), (UnsignedInt)KEEPALIVE_MIN_ROUND_SECONDS * 1000 );++ // in between, the knob is the knob+ CHECK_EQ( keepAliveRoundMS( 4 ), (UnsignedInt)4000 );+ CHECK_EQ( keepAliveRoundMS( 8 ), (UnsignedInt)8000 );++ // the ceiling is what EA's counting loop actually produced, so the default behaviour is the+ // behaviour the game shipped with+ CHECK_EQ( (int)KEEPALIVE_MAX_ROUND_SECONDS, 8 );+ CHECK( KEEPALIVE_MIN_ROUND_SECONDS < KEEPALIVE_MAX_ROUND_SECONDS );+}++TEST(keepalive_slots_are_staggered_across_the_round)+{+ // eight slots over an eight second round: one per second, which is what EA's loop did+ CHECK_EQ( keepAliveSlotsDue( 0, 8000, 8 ), 1 );+ CHECK_EQ( keepAliveSlotsDue( 999, 8000, 8 ), 1 );+ CHECK_EQ( keepAliveSlotsDue( 1000, 8000, 8 ), 2 );+ CHECK_EQ( keepAliveSlotsDue( 6999, 8000, 8 ), 7 );+ CHECK_EQ( keepAliveSlotsDue( 7000, 8000, 8 ), 8 );++ // past the end of the round nothing more comes due - the round restarts instead+ CHECK_EQ( keepAliveSlotsDue( 8000, 8000, 8 ), 8 );+ CHECK_EQ( keepAliveSlotsDue( 100000, 8000, 8 ), 8 );++ // a shorter round packs the same eight into less time rather than dropping any+ CHECK_EQ( keepAliveSlotsDue( 0, 2000, 8 ), 1 );+ CHECK_EQ( keepAliveSlotsDue( 250, 2000, 8 ), 2 );+ CHECK_EQ( keepAliveSlotsDue( 1750, 2000, 8 ), 8 );+}++TEST(keepalive_the_count_never_goes_backwards_and_never_overruns)+{+ // doKeepAlive walks m_keepAliveNextSlot up to this number and indexes m_connections with it,+ // so a value outside [0, MAX_SLOTS] would be an out of bounds write in a network path+ Int last = 0;+ for( UnsignedInt ms = 0; ms <= 20000; ms += 17 )+ {+ Int due = keepAliveSlotsDue( ms, 8000, 8 );+ CHECK( due >= last );+ CHECK( due >= 0 && due <= 8 );+ last = due;+ }+ CHECK_EQ( last, 8 );++ // degenerate inputs cannot produce an index either+ CHECK_EQ( keepAliveSlotsDue( 0, 8000, 0 ), 0 );+ CHECK_EQ( keepAliveSlotsDue( 0, 0, 8 ), 8 ); // round shorter than a slot: all at once+ CHECK_EQ( keepAliveSlotsDue( 0, 4, 8 ), 8 );+}++TEST(keepalive_every_player_gets_one_within_the_round)+{+ // the property that matters to a NAT: no slot waits longer than the round for its packet+ UnsignedInt roundMS = keepAliveRoundMS( 20 ); // what the shipped INI produces+ CHECK( !keepAliveRoundIsOver( roundMS - 1, roundMS ) );+ CHECK( keepAliveRoundIsOver( roundMS, roundMS ) );++ // the last slot is due strictly before the round ends, so it is never skipped by the restart+ CHECK_EQ( keepAliveSlotsDue( roundMS - 1, roundMS, 8 ), 8 );+ CHECK( roundMS <= 15000 ); // under the shortest NAT UDP timeouts seen in the wild+}