Skip to content

Commit 960e7a4

Browse files
committed
TEMPORARY(TESTING): add static_assert tests and debug functions for packet size verification
1 parent d54e7dc commit 960e7a4

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacket.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ class NetPacket : public MemoryPoolObject
105105
static UnsignedInt GetDisconnectScreenOffCommandSize(NetCommandMsg *msg);
106106
static UnsignedInt GetFrameResendRequestCommandSize(NetCommandMsg *msg);
107107

108+
#ifdef _DEBUG
109+
// Testing function - remove before merging to main
110+
static void TestPacketSizes();
111+
#endif
112+
108113
static void FillBufferWithGameCommand(UnsignedByte *buffer, NetCommandRef *msg);
109114
static void FillBufferWithAckCommand(UnsignedByte *buffer, NetCommandRef *msg);
110115
static void FillBufferWithFrameCommand(UnsignedByte *buffer, NetCommandRef *msg);

GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,85 @@ struct NetPacketFrameResendRequestCommand {
397397
// Restore normal struct packing
398398
#pragma pack(pop)
399399

400+
////////////////////////////////////////////////////////////////////////////////
401+
// Static Assert Tests - Verify struct sizes match original manual calculations
402+
// These tests ensure the refactor maintains exact compatibility
403+
////////////////////////////////////////////////////////////////////////////////
404+
405+
// Test function for Frame Resend Request Command
406+
constexpr UnsignedInt GetFrameResendRequestCommandSize()
407+
{
408+
UnsignedInt msglen = 0;
409+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::CommandType and command type
410+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::PlayerId and player ID
411+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // NetPacketFieldTypes::CommandId and command ID
412+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::Relay and relay
413+
414+
++msglen; // NetPacketFieldTypes::Data
415+
msglen += sizeof(UnsignedInt); // frame to resend
416+
417+
return msglen;
418+
}
419+
420+
// Test function for ACK Command
421+
constexpr UnsignedInt GetAckCommandSize()
422+
{
423+
UnsignedInt msglen = 0;
424+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::CommandType and command type
425+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::PlayerId and player ID
426+
++msglen; // NetPacketFieldTypes::Data
427+
msglen += sizeof(UnsignedShort); // command ID being acknowledged
428+
msglen += sizeof(UnsignedByte); // original player ID
429+
430+
return msglen;
431+
}
432+
433+
// Test function for Frame Command
434+
constexpr UnsignedInt GetFrameCommandSize()
435+
{
436+
UnsignedInt msglen = 0;
437+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::CommandType and command type
438+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::Relay and relay
439+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedInt); // NetPacketFieldTypes::Frame and frame
440+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::PlayerId and player ID
441+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // NetPacketFieldTypes::CommandId and command ID
442+
++msglen; // NetPacketFieldTypes::Data
443+
msglen += sizeof(UnsignedShort); // command count
444+
445+
return msglen;
446+
}
447+
448+
// Test function for Player Leave Command
449+
constexpr UnsignedInt GetPlayerLeaveCommandSize()
450+
{
451+
UnsignedInt msglen = 0;
452+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::CommandType and command type
453+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::Relay and relay
454+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedInt); // NetPacketFieldTypes::Frame and frame
455+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::PlayerId and player ID
456+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedShort); // NetPacketFieldTypes::CommandId and command ID
457+
++msglen; // NetPacketFieldTypes::Data
458+
msglen += sizeof(UnsignedByte); // leaving player ID
459+
460+
return msglen;
461+
}
462+
463+
// Test function for Keep Alive Command
464+
constexpr UnsignedInt GetKeepAliveCommandSize()
465+
{
466+
UnsignedInt msglen = 0;
467+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::CommandType and command type
468+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::Relay and relay
469+
msglen += sizeof(UnsignedByte) + sizeof(UnsignedByte); // NetPacketFieldTypes::PlayerId and player ID
470+
++msglen; // NetPacketFieldTypes::Data
471+
472+
return msglen;
473+
}
474+
475+
// Static assertions to verify sizes match
476+
static_assert(GetFrameResendRequestCommandSize() == sizeof(NetPacketFrameResendRequestCommand), "FrameResendRequestCommand size mismatch");
477+
static_assert(GetAckCommandSize() == sizeof(NetPacketAckCommand), "AckCommand size mismatch");
478+
static_assert(GetFrameCommandSize() == sizeof(NetPacketFrameCommand), "FrameCommand size mismatch");
479+
static_assert(GetPlayerLeaveCommandSize() == sizeof(NetPacketPlayerLeaveCommand), "PlayerLeaveCommand size mismatch");
480+
static_assert(GetKeepAliveCommandSize() == sizeof(NetPacketKeepAliveCommand), "KeepAliveCommand size mismatch");
481+

GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,30 @@ UnsignedInt NetPacket::GetFrameResendRequestCommandSize(NetCommandMsg *msg) {
509509
return sizeof(NetPacketFrameResendRequestCommand);
510510
}
511511

512+
////////////////////////////////////////////////////////////////////////////////
513+
// TESTING FUNCTIONS - Remove these before merging to main
514+
// These functions help verify packet serialization works correctly
515+
////////////////////////////////////////////////////////////////////////////////
516+
517+
#ifdef _DEBUG
518+
// Test function to verify packet struct sizes match expectations
519+
void NetPacket::TestPacketSizes() {
520+
// This function will be called during debug builds to verify sizes
521+
// Set a breakpoint here to inspect the values
522+
523+
UnsignedInt frameResendSize = sizeof(NetPacketFrameResendRequestCommand);
524+
UnsignedInt ackSize = sizeof(NetPacketAckCommand);
525+
UnsignedInt frameSize = sizeof(NetPacketFrameCommand);
526+
UnsignedInt playerLeaveSize = sizeof(NetPacketPlayerLeaveCommand);
527+
UnsignedInt keepAliveSize = sizeof(NetPacketKeepAliveCommand);
528+
529+
// These should match the original manual calculations
530+
// Set breakpoints here to verify the values are correct
531+
DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("TestPacketSizes: FrameResend=%d, Ack=%d, Frame=%d, PlayerLeave=%d, KeepAlive=%d",
532+
frameResendSize, ackSize, frameSize, playerLeaveSize, keepAliveSize));
533+
}
534+
#endif
535+
512536
// this function assumes that buffer is already the correct size.
513537
void NetPacket::FillBufferWithCommand(UnsignedByte *buffer, NetCommandRef *ref) {
514538
NetCommandMsg *msg = ref->getCommand();

0 commit comments

Comments
 (0)