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): order network commands across the id wrap

Olcay Seygan1aa94b4db6a7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4e0c6ac6a..c32383fab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -203,6 +203,14 @@ map. Ctrl+A is still everything at once.
thirty-six of the game's sixty-eight special powers never counted at all. The summary covers the
whole of it now. This does not stop two machines drifting apart; it stops them drifting apart
quietly, and moves the report back to the frame it actually started on.
+- **Your orders stop arriving in the wrong order a few minutes into a network game.** Every order
+ you give is numbered so the other machines can put your orders back in the order you gave them.
+ The counter was sixteen bits wide and started near the top of its range, so it rolled over to
+ zero after about fifteen hundred orders - a few minutes of ordinary play - and from that moment
+ the game read your newest orders as your oldest ones. Your own commands could execute back to
+ front, and because sorting them depended on the order the packets happened to arrive in, two
+ machines could end up executing the same commands in different orders and drift apart. The
+ numbering still rolls over; the game now reads it correctly when it does.
- **A network game runs at the speed it says it does.** Playing against other people, the
simulation was gated by two separate clocks at once — the network's and the game's own — and a
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
index ee7ec5e3c..b4bdce55b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
@@ -38,6 +38,16 @@ Bool CommandRequiresDirectSend(NetCommandMsg *msg);
Bool IsCommandSynchronized(NetCommandType type);
AsciiString GetAsciiNetCommandType(NetCommandType type);
+// Command ids are sixteen bits wide and wrap. Ordering them with < or > puts every command
+// issued after a wrap in front of every command issued before it, so the answer comes from the
+// distance between them instead: newVal is newer when it sits within half the id space ahead of
+// oldVal. Equal ids are not newer.
+inline Bool IsCommandIdNewer( UnsignedShort newVal, UnsignedShort oldVal )
+{
+ const UnsignedShort diff = newVal - oldVal;
+ return diff != 0 && diff < 0x8000;
+}
+
#ifdef DEBUG_LOGGING
extern "C" {
void dumpBufferToLog(const void *vBuf, Int len, const char *fname, Int line);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
index 4e1f415ab..3eb407d0a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
@@ -151,10 +151,10 @@ NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) {
NetCommandRef *theNext = m_lastMessageInserted->getNext();
if ((m_lastMessageInserted->getCommand()->getNetCommandType() == msg->getCommand()->getNetCommandType()) &&
(m_lastMessageInserted->getCommand()->getPlayerID() == msg->getCommand()->getPlayerID()) &&
- (m_lastMessageInserted->getCommand()->getID() < msg->getCommand()->getID()) &&
+ IsCommandIdNewer(msg->getCommand()->getID(), m_lastMessageInserted->getCommand()->getID()) &&
((theNext == NULL) || ((theNext->getCommand()->getNetCommandType() > msg->getCommand()->getNetCommandType()) ||
(theNext->getCommand()->getPlayerID() > msg->getCommand()->getPlayerID()) ||
- (theNext->getCommand()->getID() > msg->getCommand()->getID())))) {
+ IsCommandIdNewer(theNext->getCommand()->getID(), msg->getCommand()->getID())))) {
// Make sure this command isn't already in the list.
if (isEqualCommandMsg(m_lastMessageInserted->getCommand(), msg->getCommand())) {
@@ -275,7 +275,7 @@ NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) {
// Find the position within the player's section based on the command ID.
// If the command type doesn't require a command ID, sort by whatever it should be sorted by.
- while ((tempmsg != NULL) && (msg->getCommand()->getNetCommandType() == tempmsg->getCommand()->getNetCommandType()) && (msg->getCommand()->getPlayerID() == tempmsg->getCommand()->getPlayerID()) && (msg->getCommand()->getSortNumber() > tempmsg->getCommand()->getSortNumber())) {
+ while ((tempmsg != NULL) && (msg->getCommand()->getNetCommandType() == tempmsg->getCommand()->getNetCommandType()) && (msg->getCommand()->getPlayerID() == tempmsg->getCommand()->getPlayerID()) && IsCommandIdNewer((UnsignedShort)msg->getCommand()->getSortNumber(), (UnsignedShort)tempmsg->getCommand()->getSortNumber())) {
tempmsg = tempmsg->getNext();
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
index 64d8273c4..ec2175b93 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
@@ -108,9 +108,11 @@ UnsignedInt ResolveIP(AsciiString host)
* Returns the next network command ID.
*/
UnsignedShort GenerateNextCommandID() {
- static UnsignedShort commandID = 64000;
- ++commandID;
- return commandID;
+ // The counter used to start at 64000, so the first wrap arrived about 1500 commands into the
+ // very first match. It still wraps - it is sixteen bits wide - but IsCommandIdNewer, which is
+ // what NetCommandList sorts by, survives the wrap.
+ static UnsignedShort commandID = 0;
+ return commandID++;
}
/**
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 28974d77f..8e21bc592 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -39,6 +39,7 @@
#include "Common/RandomValue.h"
#include "GameLogic/LogicRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
+#include "GameNetwork/NetworkUtil.h"
#include "GameNetwork/GameInfo.h"
#include <float.h>
#include "GameClient/Water.h"
@@ -3507,3 +3508,40 @@ TEST(bitflags_crc_covers_every_word_of_the_flag_set)
}
CHECK_NE( crcOfStatusMask( full ), crcOfStatusMask( fullMinusOne ) );
}
+
+// ---------------------------------------------------------------------------------------------
+// Network command ids are sixteen bits and wrap. NetCommandList insert-sorts by them, so the
+// comparison has to survive the wrap or a player's own orders come out of order - and, because a
+// broken comparison makes the sorted insert depend on arrival order, come out differently on two
+// machines that received the same commands in different orders.
+// ---------------------------------------------------------------------------------------------
+TEST(network_command_ids_are_ordered_across_the_sixteen_bit_wrap)
+{
+ // away from the wrap, this is plain ordering
+ CHECK( IsCommandIdNewer( 1, 0 ) );
+ CHECK( !IsCommandIdNewer( 0, 1 ) );
+ CHECK( !IsCommandIdNewer( 7, 7 ) ); // the same id is not newer than itself
+
+ // across the wrap - the raw > comparison gets both of these backwards
+ CHECK( IsCommandIdNewer( 0, 65535 ) );
+ CHECK( !IsCommandIdNewer( 65535, 0 ) );
+ CHECK( IsCommandIdNewer( 3, 65530 ) );
+ CHECK( !IsCommandIdNewer( 65530, 3 ) );
+
+ // half the id space ahead is still ahead; one past that is read as behind
+ CHECK( IsCommandIdNewer( 0x7FFF, 0 ) );
+ CHECK( !IsCommandIdNewer( 0x8000, 0 ) );
+
+ // every consecutive pair across a run that crosses 65535 -> 0 is ordered, both ways round
+ Bool allOrdered = TRUE;
+ UnsignedShort id = 65500;
+ for( Int i = 0; i < 100; ++i )
+ {
+ UnsignedShort next = (UnsignedShort)(id + 1);
+ if( !IsCommandIdNewer( next, id ) || IsCommandIdNewer( id, next ) )
+ allOrdered = FALSE;
+ id = next;
+ }
+ CHECK( allOrdered );
+ CHECK_EQ( (Int)id, 64 ); // the run really did cross the wrap
+}