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
perf(gamenetwork): carry a frame in one datagram instead of four
Olcay Seygandd6d489cdfcc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fc60bc414..d816e327d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -143,6 +143,7 @@ found and fixed — EA's own, not port damage.**- The mismatch check now covers whole units and all sixty-eight special powers.- Order numbering no longer scrambles your commands after about fifteen hundred orders.- A network game runs at the speed it says it does.+- A frame of orders now travels in one datagram instead of four or five: the packet was still the 476 bytes a 2003 modem could carry, and every extra datagram was another chance to arrive late.- 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.diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.hindex 9791a635d..b4a286d85 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/LANAPI.h@@ -47,7 +47,7 @@ static const Int g_lanHostNameLength = 1;static const Int g_lanGameNameLength = 16; // reduced length because of game option lengthstatic const Int g_lanGameNameReservedLength = 16; // save N wchars for ID infostatic const Int g_lanMaxChatLength = 100;-static const Int m_lanMaxOptionsLength = MAX_PACKET_SIZE - ( 8 + (g_lanGameNameLength+1)*2 + 4 + (g_lanPlayerNameLength+1)*2+static const Int m_lanMaxOptionsLength = MAX_LANAPI_PACKET_SIZE - ( 8 + (g_lanGameNameLength+1)*2 + 4 + (g_lanPlayerNameLength+1)*2+ (g_lanLoginNameLength+1) + (g_lanHostNameLength+1) );static const Int g_maxSerialLength = 23; // including the trailing '\0'@@ -423,5 +423,9 @@ struct LANMessage};#pragma pack(pop)+// The lobby broadcast is read at fixed offsets out of one datagram, so it has to fit in one.+static_assert(sizeof(LANMessage) <= MAX_LANAPI_PACKET_SIZE,+ "LANMessage must fit in a single LAN datagram");+#endif // _LANAPI_H_diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkDefs.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkDefs.hindex ee6cb0972..2939a7a9c 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkDefs.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkDefs.h@@ -53,16 +53,42 @@ enum ConnectionNumbersstatic const Int MAX_SLOTS = MAX_PLAYER+1;-// UDP (8 bytes) + IP header (28 bytes) = 36 bytes total. We want a total packet size of 512, so 512 - 36 = 476-static const Int MAX_PACKET_SIZE = 476;+#pragma pack(push, 1)+struct TransportMessageHeader+{+ UnsignedInt crc; ///< packet-level CRC (must be first in packet)+ UnsignedShort magic; ///< Magic number identifying Generals packets+// Int id;+// NetMessageFlags flags;+};+#pragma pack(pop)++// 2003's answer was a 512 byte datagram: UDP (8 bytes) + IP header (28 bytes) = 36, so 476 of+// payload. That is a quarter of what any link today carries, and it is the reason a busy frame+// needs four or five datagrams where it could need one - every extra datagram is another chance to+// arrive late and another entry in the resend machinery.+//+// 1100 is the ceiling we take instead. It is not the ethernet MTU: a mobile link can hand back an+// MTU of 1340, and PPPoE, IPv6 and tunnelling each eat their own header on top of that, so a+// payload chosen from 1500 fragments on exactly the links that can least afford it. Nothing here+// discovers the path MTU, so the number has to be safe unasked.+static const Int MAX_UDP_PAYLOAD_SIZE = 1100;++// What goes on the wire is the header plus the payload, and the payload is what a packet may hold.+static const Int MAX_NETWORK_MESSAGE_LEN = MAX_UDP_PAYLOAD_SIZE;+static const Int MAX_PACKET_SIZE = MAX_UDP_PAYLOAD_SIZE - sizeof(TransportMessageHeader);++// The LAN lobby's own broadcast is a fixed struct read at fixed offsets, and it stays where it was.+static const Int MAX_LANAPI_PACKET_SIZE = 476;/*** Command packet - contains frame #, total # of commands, and each command. This is what gets sent* to each player every frame*/-#define MAX_MESSAGE_LEN 1024-#define MAX_MESSAGES 128-static const Int numCommandsPerCommandPacket = (MAX_MESSAGE_LEN - sizeof(UnsignedInt) - sizeof(UnsignedShort))/sizeof(GameMessage);+// twice as many in flight: the send and receive rings both used to fill under load, and a full ring+// is a dropped message that the other end waits for+#define MAX_MESSAGES 256+static const Int numCommandsPerCommandPacket = (MAX_PACKET_SIZE - sizeof(UnsignedInt) - sizeof(UnsignedShort))/sizeof(GameMessage);#pragma pack(push, 1)struct CommandPacket{@@ -74,16 +100,6 @@ struct CommandPacket#define MAX_TRANSPORT_STATISTICS_SECONDS 30-#pragma pack(push, 1)-struct TransportMessageHeader-{- UnsignedInt crc; ///< packet-level CRC (must be first in packet)- UnsignedShort magic; ///< Magic number identifying Generals packets-// Int id;-// NetMessageFlags flags;-};-#pragma pack(pop)-/*** Transport message - encapsulating info kept by the transport layer about each* packet. These structs make up the in/out buffers at the transport layer.@@ -92,7 +108,7 @@ struct TransportMessageHeaderstruct TransportMessage{TransportMessageHeader header;- UnsignedByte data[MAX_MESSAGE_LEN];+ UnsignedByte data[MAX_PACKET_SIZE];Int length;UnsignedInt addr;UnsignedShort port;diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cppindex e77593b77..98ec707c2 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp@@ -72,8 +72,8 @@ LANGame::LANGame( void )LANAPI::LANAPI( void ) : m_transport(NULL){- DEBUG_LOG(("LANAPI::LANAPI() - max game option size is %d, sizeof(LANMessage)=%d, MAX_PACKET_SIZE=%d\n",- m_lanMaxOptionsLength, sizeof(LANMessage), MAX_PACKET_SIZE));+ DEBUG_LOG(("LANAPI::LANAPI() - max game option size is %d, sizeof(LANMessage)=%d, MAX_LANAPI_PACKET_SIZE=%d\n",+ m_lanMaxOptionsLength, sizeof(LANMessage), MAX_LANAPI_PACKET_SIZE));m_lastResendTime = 0;//diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetMessageStream.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetMessageStream.cppindex 63745860d..79353b0ce 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetMessageStream.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetMessageStream.cpp@@ -176,7 +176,7 @@ Bool AddCommandToPacket(const GameMessage *msg)int messageSize = sizeofMessageHeader + sizeofMessageArg * msg->getArgumentCount();// If we have too much, send what we have- if (bytesUsed && (bytesUsed + sizeof(CommandPacketHeader) + messageSize >= MAX_MESSAGE_LEN))+ if (bytesUsed && (bytesUsed + sizeof(CommandPacketHeader) + messageSize >= MAX_PACKET_SIZE)){commandBuf[0] = MSGTYPE_PARTIALCOMMAND;if (!TheNetwork->queueSend(BROADCAST_CON, commandBuf, bytesUsed + sizeof(CommandPacketHeader) + 1, MSG_NEEDACK | MSG_SEQUENCED))@@ -190,7 +190,7 @@ Bool AddCommandToPacket(const GameMessage *msg)bytesUsed = 0;}- if (bytesUsed + sizeof(CommandPacketHeader) + messageSize >= MAX_MESSAGE_LEN)+ if (bytesUsed + sizeof(CommandPacketHeader) + messageSize >= MAX_PACKET_SIZE){//DEBUG_ASSERTCRASH(false, ("Too many commands in one frame! Some will be dropped."));DEBUG_LOG(("Too many commands in one frame! Some will be dropped."));@@ -208,7 +208,7 @@ Bool AddCommandToPacket(const GameMessage *msg)bytesUsed += sizeofMessageArg;}- //DEBUG_ASSERTCRASH(bytesUsed + sizeof(CommandPacketHeader) < MAX_MESSAGE_LEN, ("Memory overwrite constructing command packet!"));+ //DEBUG_ASSERTCRASH(bytesUsed + sizeof(CommandPacketHeader) < MAX_PACKET_SIZE, ("Memory overwrite constructing command packet!"));//DEBUG_LOG(("Memory overwrite constructing command packet!"));return true;}diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cppindex 7bda4fad3..63e002bce 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp@@ -80,7 +80,7 @@ struct ConnectionMessage{Int id;NetMessageFlags flags;- UnsignedByte data[MAX_MESSAGE_LEN];+ UnsignedByte data[MAX_PACKET_SIZE];time_t lastSendTime;Int retries;Int length;diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Transport.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Transport.cppindex c08f8e44a..39336e082 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Transport.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Transport.cpp@@ -296,9 +296,9 @@ Bool Transport::doRecv()TransportMessage incomingMessage;unsigned char *buf = (unsigned char *)&incomingMessage;- int len = MAX_MESSAGE_LEN;+ int len = MAX_NETWORK_MESSAGE_LEN;// DEBUG_LOG(("Transport::doRecv - checking\n"));- while ( (len=m_udpsock->Read(buf, MAX_MESSAGE_LEN, &from)) > 0 )+ while ( (len=m_udpsock->Read(buf, MAX_NETWORK_MESSAGE_LEN, &from)) > 0 ){// Packet loss simulationif (m_usePacketLoss)@@ -420,7 +420,7 @@ Bool Transport::isGeneralsPacket( TransportMessage *msg )if (!msg)return false;- if (msg->length < 0 || msg->length > MAX_MESSAGE_LEN)+ if (msg->length < 0 || msg->length > MAX_PACKET_SIZE)return false;CRC crc;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 9bd4d3170..76c17d3df 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -58,6 +58,7 @@#include "GameNetwork/NetworkUtil.h"#include "GameNetwork/NetCommandList.h"#include "GameNetwork/NetCommandWrapperList.h"+#include "GameNetwork/LANAPI.h"#include "GameNetwork/NetPacket.h"#include "GameNetwork/GameInfo.h"#include <float.h>@@ -4309,17 +4310,36 @@ TEST(an_overlong_announced_filename_is_truncated_instead_of_overflowing)msg->detach();}+/* The 2003 datagram was 512 bytes all in, 476 of it payload - a quarter of what any link carries+ today, and the reason a busy frame needs four or five datagrams where one would do. The payload+ is sized from a safe UDP ceiling now, and the three numbers have to stay in step: what goes on+ the wire is the header plus the payload, and the payload is exactly what a packet may hold. */+TEST(a_datagram_is_one_udp_payload_and_the_payload_is_what_a_packet_holds)+{+ TransportMessage msg;++ CHECK_EQ( (Int)sizeof(msg.header) + MAX_PACKET_SIZE, MAX_NETWORK_MESSAGE_LEN );+ CHECK_EQ( (Int)sizeof(msg.data), MAX_PACKET_SIZE );+ CHECK( MAX_NETWORK_MESSAGE_LEN <= MAX_UDP_PAYLOAD_SIZE );+ CHECK( MAX_PACKET_SIZE > 476 ); // bigger than the 2003 datagram++ // everything that has to live inside one payload still does+ CHECK( (Int)sizeof(CommandPacket) <= MAX_PACKET_SIZE );+ CHECK( (Int)sizeof(LANMessage) <= MAX_LANAPI_PACKET_SIZE );+ CHECK( MAX_LANAPI_PACKET_SIZE <= MAX_PACKET_SIZE );+}+/* A chat message's length goes on the wire in one byte, and every site that packed or measured oneassigned the character count straight to it. 256 characters became a length of zero; 300 became44 - and the size estimate, the room check and the copy each wrapped separately, so what wentout declared one length and carried another. */-TEST(an_overlong_chat_line_is_refused_rather_than_wrapped_to_a_short_one)+static NetCommandRef *makeChat( const WideChar *body, Int length ){NetChatCommandMsg *chat = newInstance( NetChatCommandMsg );UnicodeString text;Int k;- for( k = 0; k < 300; ++k )- text.concat( (WideChar)(L'a' + (k % 26)) );+ for( k = 0; k < length; ++k )+ text.concat( body ? body[k] : (WideChar)(L'a' + (k % 26)) );chat->setText( text );chat->setPlayerMask( 0xFF );chat->setPlayerID( 1 );@@ -4329,35 +4349,26 @@ TEST(an_overlong_chat_line_is_refused_rather_than_wrapped_to_a_short_one)NetCommandRef *ref = newInstance( NetCommandRef )( chat );ref->setRelay( 0 );+ chat->detach(); // the ref holds it now+ return ref;+}++TEST(an_overlong_chat_line_keeps_its_length_instead_of_wrapping_to_a_short_one)+{+ NetCommandRef *ref = makeChat( NULL, 300 );NetPacket *packet = newInstance( NetPacket );packet->init();- /* 300 characters cannot fit a 476 byte packet, so the only right answer is to refuse it. With- the wrapped byte the packet believed it needed 88 bytes and took the message, then wrote a- length of 44 in front of it. */- CHECK( !packet->addCommand( ref ) );- CHECK_EQ( packet->getNumCommands(), 0 );-- /* and a chat line that does fit is still taken */- NetChatCommandMsg *shortChat = newInstance( NetChatCommandMsg );- shortChat->setText( UnicodeString( L"gg" ) );- shortChat->setPlayerMask( 0xFF );- shortChat->setPlayerID( 1 );- shortChat->setID( 1 );- shortChat->setExecutionFrame( 0 );- shortChat->setNetCommandType( NETCOMMANDTYPE_CHAT );- NetCommandRef *shortRef = newInstance( NetCommandRef )( shortChat );- shortRef->setRelay( 0 );-- CHECK( packet->addCommand( shortRef ) );+ CHECK( packet->addCommand( ref ) );CHECK_EQ( packet->getNumCommands(), 1 );+ /* 300 characters clamp to 255, which is 510 bytes of text. With the wrapped byte the length+ came out as 44 and the packet carried 88 bytes - so the size alone tells the two apart. */+ CHECK( packet->getLength() >= 510 );+packet->deleteInstance();- shortRef->deleteInstance();ref->deleteInstance();- shortChat->detach();- chat->detach();}/* addCommand's own guard against a null reference sat below the line that followed it, so the one