-
Notifications
You must be signed in to change notification settings - Fork 105
refactor(network): replace manual size calculations with packed structs #1675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
refactor(network): replace manual size calculations with packed structs #1675
Conversation
There was a problem hiding this 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.
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
You can still do this for the messages that are variable in length, just make the fixed portion of the message a struct. |
dbb01a8
to
9d9e804
Compare
9d9e804
to
db5c043
Compare
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
|
||
++msglen; // the NetPacketFieldTypes::Data | ||
msglen += sizeof(UnsignedByte); // string msglength | ||
UnsignedByte textmsglen = cmdMsg->getText().getLength(); |
There was a problem hiding this comment.
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 👀
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
34ab595
to
ba6335d
Compare
35cac52
to
d54e7dc
Compare
…dType type, and add struct constructors
960e7a4
to
ad27a6c
Compare
*/ | ||
size_t NetDisconnectChatCommandMsg::getByteCount() const | ||
{ | ||
return m_text.getLength() * sizeof(UnsignedShort); |
There was a problem hiding this comment.
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.
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
} | ||
|
||
UnsignedInt NetPacket::GetGameCommandSize(NetCommandMsg *msg) { | ||
NetGameCommandMsg *cmdMsg = (NetGameCommandMsg *)msg; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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