Skip to content

Conversation

bobtista
Copy link

@bobtista bobtista commented Oct 7, 2025

Created NetPacketStructs.h with packed struct definitions for network packets
Replaced 9 manual size calculation functions with sizeof(struct) calls

In the next PR we could use these structs for actual serialization, replacing manual memcpy calls

TODO:
Replicate in Generals

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot compile this branch. There are 700 linker errors.

@Mauller
Copy link

Mauller commented Oct 7, 2025

You can still do this for the messages that are variable in length, just make the fixed portion of the message a struct.
Then you just calculate the message size based on the size of the fixed portion and the variable data appended to it.

@bobtista bobtista force-pushed the bobtista/packedstructpackets branch 2 times, most recently from dbb01a8 to 9d9e804 Compare October 8, 2025 17:25
@bobtista bobtista force-pushed the bobtista/packedstructpackets branch from 9d9e804 to db5c043 Compare October 8, 2025 22:08

++msglen; // the NetPacketFieldTypes::Data
msglen += sizeof(UnsignedByte); // string msglength
UnsignedByte textmsglen = cmdMsg->getText().getLength();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: This truncates 4 bytes to 1 byte 👀

@xezon xezon added Major Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Refactor Edits the code with insignificant behavior changes, is never user facing labels Oct 13, 2025
@bobtista bobtista force-pushed the bobtista/packedstructpackets branch from 34ab595 to ba6335d Compare October 13, 2025 19:27
@bobtista bobtista force-pushed the bobtista/packedstructpackets branch from 35cac52 to d54e7dc Compare October 13, 2025 21:09
@bobtista bobtista force-pushed the bobtista/packedstructpackets branch from 960e7a4 to ad27a6c Compare October 14, 2025 03:29
*/
size_t NetDisconnectChatCommandMsg::getByteCount() const
{
return m_text.getLength() * sizeof(UnsignedShort);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think UnicodeString also has a getByteCount function by now. Use it.

}

UnsignedInt NetPacket::GetGameCommandSize(NetCommandMsg *msg) {
NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)msg;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static cast

GameMessage *gmsg = cmdMsg->constructGameMessage();
GameMessageParser *parser = newInstance(GameMessageParser)(gmsg);

msglen += sizeof(GameMessage::Type);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can all this junk also be shoved into a getByteCount function?

*/
size_t NetChatCommandMsg::getByteCount() const
{
return m_text.getLength() * sizeof(UnsignedShort) + sizeof(m_playerMask);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_text.getByteCount()

*/
size_t NetDisconnectChatCommandMsg::getByteCount() const
{
return m_text.getByteCount();
Copy link

@xezon xezon Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the reader recognize the length of this text? Surely this must be either null terminated or bundled with a count? I wonder if this is an original bug.

Copy link
Author

@bobtista bobtista Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be addressed in another PR or here?
A potential fix:

{
    return sizeof(UnsignedByte) + m_text.getByteCount(); 
}


GameMessageParserArgumentType *arg = parser->getFirstArgumentType();
while (arg != NULL) {
msglen += 2 * sizeof(UnsignedByte); // for the type and number of args of that type declaration.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be written in 2 lines. Does "type" have a typedef?

Copy link
Author

@bobtista bobtista Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like:

constexpr size_t ARG_TYPE_SIZE = sizeof(UnsignedByte);
constexpr size_t ARG_COUNT_SIZE = sizeof(UnsignedByte);
...
msglen += ARG_TYPE_SIZE + ARG_COUNT_SIZE;

Or

msglen += sizeof(UnsignedByte); // argument type
msglen += sizeof(UnsignedByte); // argument count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Major Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Refactor Edits the code with insignificant behavior changes, is never user facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants