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): make three commands ask for the room they actually use

Olcay Seygan126fd374c58d
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp
index 9f2d36a65..ff36da0e4 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp
@@ -429,9 +429,11 @@ UnsignedInt NetPacket::GetRunAheadMetricsCommandSize(NetCommandMsg *msg) {
msglen += sizeof(UnsignedByte);
msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte);
- ++msglen;
- msglen += sizeof(UnsignedByte);
- msglen += sizeof(Real);
+ ++msglen; // the 'D'
+ msglen += sizeof(Real); // average latency
+ msglen += sizeof(UnsignedShort); // average fps - the writer sends a short, and
+ // this counted a byte, so the buffer was a
+ // byte short of what got written into it
return msglen;
}
@@ -623,6 +625,9 @@ UnsignedInt NetPacket::GetLoadCompleteMessageSize(NetCommandMsg *msg) {
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
++msglen;
msglen += sizeof(UnsignedByte);
+ msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte); // the 'C' and the command id, which
+ // the writer always emits and this
+ // never counted
++msglen; // For the 'D'
@@ -637,6 +642,7 @@ UnsignedInt NetPacket::GetTimeOutGameStartMessageSize(NetCommandMsg *msg) {
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte);
++msglen;
msglen += sizeof(UnsignedByte);
+ msglen += sizeof(UnsignedShort) + sizeof(UnsignedByte); // the 'C' and the command id, as above
++msglen; // For the 'D'
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 76c17d3df..92d514d43 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -4242,8 +4242,69 @@ struct NetPacketFileReader : public NetPacket
using NetPacket::readFileMessage;
using NetPacket::readFileAnnounceMessage;
using NetPacket::GetBufferSizeNeededForCommand;
+ using NetPacket::FillBufferWithCommand;
};
+/* Sending a command out of band asks how many bytes it needs, allocates that, and then lets the
+ writer fill it. Three of those size functions disagreed with their own writer: the run-ahead
+ metrics one counted the average frame rate as a byte where the writer sends a short, and the
+ load-complete and timeout-start ones did not count the command id at all, though the writer
+ always emits it. Each of those is a write past the end of a freshly sized heap block.
+
+ So measure it: fill a canary buffer, find how far the writer really went, and hold the
+ advertised size against it. */
+static Int bytesActuallyWritten( NetCommandRef *ref, Int *advertised )
+{
+ static const Int CANARY_LEN = 512;
+ static const UnsignedByte CANARY = 0xCD;
+ UnsignedByte buffer[CANARY_LEN];
+ memset( buffer, CANARY, sizeof(buffer) );
+
+ *advertised = (Int)NetPacketFileReader::GetBufferSizeNeededForCommand( ref->getCommand() );
+ NetPacketFileReader::FillBufferWithCommand( buffer, ref );
+
+ Int last = -1;
+ Int i;
+ for( i = 0; i < CANARY_LEN; ++i )
+ if( buffer[i] != CANARY )
+ last = i;
+ return last + 1;
+}
+
+static NetCommandRef *wrapForSend( NetCommandMsg *msg, NetCommandType type )
+{
+ msg->setNetCommandType( type );
+ msg->setPlayerID( 3 );
+ msg->setID( 0x1234 );
+ msg->setExecutionFrame( 99 );
+ NetCommandRef *ref = newInstance( NetCommandRef )( msg );
+ ref->setRelay( 0 );
+ msg->detach();
+ return ref;
+}
+
+TEST(a_commands_advertised_size_covers_what_its_writer_puts_in_the_buffer)
+{
+ Int advertised;
+
+ NetRunAheadMetricsCommandMsg *metrics = newInstance( NetRunAheadMetricsCommandMsg );
+ metrics->setAverageLatency( 0.125f );
+ metrics->setAverageFps( 300 ); // wider than a byte, which is the point
+ NetCommandRef *metricsRef = wrapForSend( metrics, NETCOMMANDTYPE_RUNAHEADMETRICS );
+ CHECK( bytesActuallyWritten( metricsRef, &advertised ) <= advertised );
+ metricsRef->deleteInstance();
+
+ NetCommandMsg *loadDone = newInstance( NetCommandMsg );
+ NetCommandRef *loadRef = wrapForSend( loadDone, NETCOMMANDTYPE_LOADCOMPLETE );
+ CHECK( bytesActuallyWritten( loadRef, &advertised ) <= advertised );
+ loadRef->deleteInstance();
+
+ NetCommandMsg *timeout = newInstance( NetCommandMsg );
+ NetCommandRef *timeoutRef = wrapForSend( timeout, NETCOMMANDTYPE_TIMEOUTSTART );
+ CHECK( bytesActuallyWritten( timeoutRef, &advertised ) <= advertised );
+ timeoutRef->deleteInstance();
+}
+
/* The size of nothing is nothing. This one hands back a byte count, but its guard against a null
command returned TRUE - so a caller sizing a buffer reserved one byte for a command that is not
there. */