Skip to content

Conversation

@bobtista
Copy link

@bobtista bobtista commented Oct 8, 2025

Building on #1675, this PR uses the packed struct definitions for packet serialization

Changes

Serialization Refactor

  • Replaced manual buffer manipulation with packed struct assignments in all FillBufferWith* functions
  • Eliminated manual offset tracking - structs handle memory layout automatically
  • Used NetPacketFieldTypes constants instead of magic character literals throughout

What Changed

Before (manual byte manipulation):

UnsignedShort offset = 0;
buffer[offset] = NetPacketFieldTypes::CommandType;
++offset;
buffer[offset] = cmdMsg->getNetCommandType();
offset += sizeof(UnsignedByte);
// ... 20+ lines of manual offset tracking and memcpy calls

After (clean struct assignment):

NetPacketFrameCommand* packet = (NetPacketFrameCommand*)buffer;
packet->commandType.header = NetPacketFieldTypes::CommandType;
packet->commandType.commandType = cmdMsg->getNetCommandType();
packet->frame.header = NetPacketFieldTypes::Frame;
packet->frame.frame = cmdMsg->getExecutionFrame();
// ... readable struct field assignments

TODO

  • Replicate in Generals
  • Consider similar refactor for deserialization code

@bobtista bobtista marked this pull request as draft October 8, 2025 21:40
@bobtista bobtista force-pushed the bobtista/packedstruct-serialization branch from 6f54a4b to 28f6188 Compare October 8, 2025 22:12
@Mauller
Copy link

Mauller commented Oct 10, 2025

You need to make sure to memcopy your packed structs into the buffer once they are filled otherwise the messages won't be going anywhere :D

@bobtista
Copy link
Author

You need to make sure to memcopy your packed structs into the buffer once they are filled otherwise the messages won't be going anywhere :D

Are you sure? As I read it, when we cast buffer to NetPacketAckCommand*, we aren't creating a local struct - we're directly treating the buffer memory as a packed struct. Every assignment like packet->commandType.header = 'T' is directly writing into the buffer at the correct offset, so no memcpy is needed. Is this not the case?

@Mauller
Copy link

Mauller commented Oct 10, 2025

Are you sure? As I read it, when we cast buffer to NetPacketAckCommand*, we aren't creating a local struct - we're directly treating the buffer memory as a packed struct. Every assignment like packet->commandType.header = 'T' is directly writing into the buffer at the correct offset, so no memcpy is needed. Is this not the case?

Ah yes, apologies, i missed that you cast a pointer to the buffer.

@bobtista bobtista force-pushed the bobtista/packedstruct-serialization branch from 28f6188 to 87b4bf2 Compare October 12, 2025 18:35
@bobtista bobtista marked this pull request as ready for review October 12, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants