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
feat(gamenetwork): send synchronized commands redundantly
Olcay Seyganc8e0450f5ee7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 513750cc5..45050cfd8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -227,6 +227,7 @@ found and fixed — EA's own, not port damage.**- 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.- 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.- Two players who each lost the other's last packet no longer freeze until the disconnect screen. Each asked the other to send it again, and each only resent what it had already played, which was nothing, so on a link with a tenth of a second of delay and one packet in twenty lost the match stopped for good one second in. Now the packet goes again and the match carries on.- 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 thediff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.hindex f7e5ae42e..9e05842f9 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h@@ -82,6 +82,28 @@ void Connection_updateRetryTimeout( Real sampleMS, Real &srtt, Real &rttvar, tim*/time_t Connection_retryDelayFor( time_t baseRetryMS, Int numTimesSent );+/**+ * Redundant sends. A synchronized command (orders, frame info, run-ahead) that is still unacked+ * goes out again in the next packets this many times, each copy at least the spacing after the+ * last time it was on the wire, before any retry timer runs out. On a lossy link the retry is+ * what the room waits for: the timeout is a full round trip through the packet router and more,+ * 600 to 800ms at 120ms and 5% loss, and the run-ahead covers only the one-way trip, so every lost+ * packet was a stall. A copy 30ms behind the original costs a few bytes and gets there before the+ * run-ahead runs out. Only synchronized commands are copied because their receiving end drops a+ * duplicate by itself (FrameData::addCommand, and the old-frame test in processNetCommand); chat,+ * disconnect and file traffic keep the plain retry. A copy does not count as a send for Karn's+ * rule, so the first ack still times the link.+ */+#define CONNECTION_REDUNDANT_COPIES 2+#define CONNECTION_REDUNDANT_SPACING_MS 30++/**+ * Whether a command last put on the wire at timeLastOnWire, already copied copiesSent times, is+ * due another redundant copy at curTime. -1 is a command that has never gone out.+ * Free function (not a member, not static) so test_gameengine can link straight to it.+ */+Bool Connection_isRedundantCopyDue( time_t curTime, time_t timeLastOnWire, Int copiesSent );+class Connection : public MemoryPoolObject{MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(Connection, "Connection")@@ -126,6 +148,7 @@ public:protected:void doRetryMetrics();+ void addRedundantCopies(NetPacket *packet, time_t curTime);Bool m_isQuitting;UnsignedInt m_quitTime;diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandRef.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandRef.hindex 90671e05c..93642cf44 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandRef.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandRef.h@@ -68,6 +68,10 @@ public:Int getNumTimesSent() const;void markSent(time_t when);+ time_t getTimeLastOnWire() const;+ Int getNumCopiesSent() const;+ void markCopied(time_t when);+protected:NetCommandMsg *m_msg;NetCommandRef *m_next;@@ -75,6 +79,8 @@ protected:UnsignedByte m_relay; ///< Need this in the command reference since the relay value will be different depending on where this particular reference is being sent.time_t m_timeLastSent;Int m_numTimesSent; ///< How many times this reference has gone out. An ack for a command sent more than once cannot be timed (which send is it acking?), so it is not used as a latency sample - Karn's rule.+ time_t m_timeLastOnWire; ///< The last send or redundant copy. Spaces the copies; the retry timer keeps m_timeLastSent.+ Int m_numCopiesSent; ///< Redundant copies since the last real send. See CONNECTION_REDUNDANT_COPIES.#ifdef DEBUG_NETCOMMANDREFUnsignedInt m_id;@@ -149,12 +155,40 @@ inline Int NetCommandRef::getNumTimesSent() const/*** Record a send: stamp the time and count it.*/-inline void NetCommandRef::markSent(time_t when)+inline void NetCommandRef::markSent(time_t when){m_timeLastSent = when;+ m_timeLastOnWire = when;+ m_numCopiesSent = 0;++m_numTimesSent;}+/**+ * The last time this reference went out at all, real send or redundant copy. -1 before the first.+ */+inline time_t NetCommandRef::getTimeLastOnWire() const+{+ return m_timeLastOnWire;+}++/**+ * How many redundant copies have followed the last real send.+ */+inline Int NetCommandRef::getNumCopiesSent() const+{+ return m_numCopiesSent;+}++/**+ * Record a redundant copy. Neither the retry timer nor Karn's count moves: the copy exists so the+ * retry is never needed, and the first ack still times the original send.+ */+inline void NetCommandRef::markCopied(time_t when)+{+ m_timeLastOnWire = when;+ ++m_numCopiesSent;+}+/*** Set the send relay for this reference of the command.*/diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cppindex 042e17246..393021d10 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp@@ -98,6 +98,17 @@ time_t Connection_retryDelayFor( time_t baseRetryMS, Int numTimesSent )return delay;}+Bool Connection_isRedundantCopyDue( time_t curTime, time_t timeLastOnWire, Int copiesSent )+{+ if (timeLastOnWire == -1) {+ return FALSE;+ }+ if (copiesSent >= CONNECTION_REDUNDANT_COPIES) {+ return FALSE;+ }+ return (curTime - timeLastOnWire) >= CONNECTION_REDUNDANT_SPACING_MS;+}+/*** The constructor.*/@@ -394,6 +405,8 @@ UnsignedInt Connection::doSend() {if (msg != NULL) {DEBUG_LOG(("didn't finish sending all commands in connection\n"));+ } else {+ addRedundantCopies(packet, curtime);}++numpackets;@@ -413,6 +426,26 @@ UnsignedInt Connection::doSend() {return numpackets;}+/**+ * Fill what is left of the last packet of a send with copies of the synchronized commands still+ * waiting for their ack. They only ever ride behind everything that was due, so a copy never+ * pushes a first send into the next packet. See CONNECTION_REDUNDANT_COPIES.+ */+void Connection::addRedundantCopies(NetPacket *packet, time_t curTime) {+ for (NetCommandRef *ref = m_netCommandList->getFirstMessage(); ref != NULL; ref = ref->getNext()) {+ if (!IsCommandSynchronized(ref->getCommand()->getNetCommandType())) {+ continue;+ }+ if (!Connection_isRedundantCopyDue(curTime, ref->getTimeLastOnWire(), ref->getNumCopiesSent())) {+ continue;+ }+ if (!packet->addCommand(ref)) {+ return;+ }+ ref->markCopied(curTime);+ }+}+NetCommandRef * Connection::processAck(NetAckStage1CommandMsg *msg) {return processAck(msg->getCommandID(), msg->getOriginalPlayerID());}diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandRef.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandRef.cppindex abd1e80d9..876d91ff4 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandRef.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandRef.cpp@@ -46,6 +46,8 @@ NetCommandRef::NetCommandRef(NetCommandMsg *msg)m_msg->attach();m_timeLastSent = -1;m_numTimesSent = 0;+ m_timeLastOnWire = -1;+ m_numCopiesSent = 0;#ifdef DEBUG_NETCOMMANDREFm_id = ++refNum;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 0b61984de..2c8b71d53 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3128,6 +3128,25 @@ TEST(connection_retry_backs_off_when_a_command_keeps_going_unacked)CHECK_EQ( (Int)Connection_retryDelayFor( 200, 0 ), 200 );}+extern Bool Connection_isRedundantCopyDue( time_t curTime, time_t timeLastOnWire, Int copiesSent );++TEST(connection_redundant_copies_follow_a_send_spaced_and_counted)+{+ /* Nothing to copy before the first real send; that one goes out through the ordinary path. */+ CHECK( !Connection_isRedundantCopyDue( 1000, -1, 0 ) );++ /* Not in the packet the command just went out in, nor one right behind it: two copies inside+ the same few milliseconds die in the same burst. */+ CHECK( !Connection_isRedundantCopyDue( 1000, 1000, 0 ) );+ CHECK( !Connection_isRedundantCopyDue( 1000 + CONNECTION_REDUNDANT_SPACING_MS - 1, 1000, 0 ) );+ CHECK( Connection_isRedundantCopyDue( 1000 + CONNECTION_REDUNDANT_SPACING_MS, 1000, 0 ) );++ /* The count is the bandwidth bound: every copy past it is refused however long the ack takes,+ and the retry timer takes over from there. */+ CHECK( Connection_isRedundantCopyDue( 5000, 1000, CONNECTION_REDUNDANT_COPIES - 1 ) );+ CHECK( !Connection_isRedundantCopyDue( 5000, 1000, CONNECTION_REDUNDANT_COPIES ) );+}+// ------------------------------------------------------------------------------------------------// CRCSnapshotRing - the evidence a mismatch report is missing. A mismatch is detected several// frames after the frame it happened on, so the ring has to still hold that frame when asked, and