-
Notifications
You must be signed in to change notification settings - Fork 118
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
xezon
left a comment
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
960e7a4 to
ad27a6c
Compare
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Include/GameNetwork/NetPacketStructs.h
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
…ments more consistent
…dType type, and add struct constructors
…classes - Add NetLoadCompleteCommandMsg and NetTimeOutGameStartCommandMsg classes - Update ConnectionManager.cpp to use specific classes instead of base NetCommandMsg - Remove special case handling from NetPacket.cpp since all types now have classes - Eliminate the switch statement entirely - now just calls virtual function
xezon
left a comment
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.
This looks very promising.
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
Outdated
Show resolved
Hide resolved
| // Frame Info Command Packet | ||
| //////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| struct NetPacketFrameCommand { |
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.
Would it perhaps make sense to put these packed structs into the non packed message classes?
Such as:
class NetGameCommandMsg : public NetCommandMsg
{
...
public:
#pragma pack(push, 1)
struct Packed
{
...
};
#pragma pack(pop)
...
};This way they would sit together and would perhaps be easier to maintain? Not sure.
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.
Yes we could do that, we'd get to use return sizeof(Packed) instead of return sizeof(NetPacketGameCommand), and we could remove some imports, and it keeps the related code together visually. On the other hand, the packed structs are really about network serialization, not about the message logic. Are there places where we only want to import one vs both? Any file that includes NetCommandMsg.h would now also pull in all the packed struct definitions. Whatever you think is best
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 move in later changes when more refactor work is done and we have a clearer picture. Right now seems to be ok.
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.
You mean later in another PR or now in this one?
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.
In a future Pull.
|
@xezon any changes you want still? |
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.
Did you push all changes? (Some Review Comments were closed without response or action)
I just pushed the latest. I tried to only resolve comments if I addressed them in a commit. |
|
For example you marked resolved the virtual function comment without action. Or the obsolete CppMacros.h comment without action. So I wonder what was the resolution for it. Edit: I see the edits now. Strangely GitHub did not show the changes before. I will look over tomorrow. |
xezon
left a comment
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 it is good. Needs replication in Generals.
I just pushed the replication commit. Note, I just copied NetCommandMsg and NetPacket.cpp and corrected comments that mentioned Zero Hour. |
|
Copying files from ZH to Generals is risky, unless they are identical. Better use script to replicate: Batch: |
Ah ok. 118 rejected hunks. I'll try going through them like it's a merge conflict. |
|
It drops *.rej files that you can inspect. |
Ok I did it and kept the commit where I'd just copied the files - there are indeed some differences when using the patch approach for those two files |
|
Yes. At some places Generals code is a bit different and then needs attention to sort out. |
|
Ok so using this method to compare the original files from ZH to Generals on main: The changes should be exactly the same for NetPacket.cpp, NetCommandMsg.h, and NetCommandMsg.cpp
|
|
Ok re-doing the patch method with the correct commit, there were zero rejections. Everything applied cleanly, and it should be good to go :) |
| return 0; | ||
| } | ||
|
|
||
| UnsignedInt NetPacket::GetGameCommandSize(NetCommandMsg *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.
When I tried to locally replicate to Generals, I saw that the removed code blocks in this file conflicted. Are we certain that the packets are the exact same in both games? No discrepancies?
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.
Hmm...
git apply -p2 --directory=Generals --reject --whitespace=fix changes.patch 2>&1
Checking patch Generals/Code/GameEngine/Include/GameNetwork/NetCommandMsg.h...
Checking patch Generals/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp...
Hunk #1 succeeded at 2236 (offset 14 lines).
Hunk #2 succeeded at 2249 (offset 14 lines).
Checking patch Generals/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp...
changes.patch:842: new blank line at EOF.
+
Checking patch Generals/Code/GameEngine/Source/GameNetwork/NetPacket.cpp...
Applied patch Generals/Code/GameEngine/Include/GameNetwork/NetCommandMsg.h cleanly.
Applied patch Generals/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp cleanly.
Applied patch Generals/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp cleanly.
Applied patch Generals/Code/GameEngine/Source/GameNetwork/NetPacket.cpp cleanly.
warning: 1 line adds whitespace errors.
find Generals -name "*.rej" -> no output
git status --short
MM Generals/Code/GameEngine/Include/GameNetwork/NetCommandMsg.h
MM Generals/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
MM Generals/Code/GameEngine/Source/GameNetwork/NetCommandMsg.cpp
MM Generals/Code/GameEngine/Source/GameNetwork/NetPacket.cpp
?? changes.patch
All 4 files applied cleanly - zero rejections
Preserved Generals-specific code (14 lines of disconnect stats logic in ConnectionManager)
Have our mains diverged somehow? Should I pull the latest and rebase?
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 did test by removing the top 3 commits where you did replicate work, and then perform a clean replicate with the Script. It then showed conflict.
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 took a look at WinMerge and NetPacket.cpp are identical in both games (before this change). So it should not conflict. I will check again tomorrow.
I did also compare NetPacket.cpp in this branch and it is not identical in both games.
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.
Ok I think I found the issue - in commit 0630ab5 on this branch, I changed a comment in both Generals and GeneralsMD, which caused the patch application to complain. So, I did a hard reset to before I made the attempts at duplicating to Generals, then added a commit that reverts the premature change to the Generals file, then the replicate in Generals with the patch went smoothly. I'm force pushing now which just removes the two botched commits where I tried replicating before.
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.
Nice. Yes that explains it.
Btw, seeing major parts of Network are identical between Generals and Zero Hour, how about we unify the Network code first, before applying the new refactors?
I think Rebase after Unify will work, otherwise just copy paste the ZH files over Core files will also work.
What do you think? Would you need help with that?
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.
Unifying then rebasing was giving me headaches, so I started a new branch, unified, then copied the contents of the files to their new homes, and made a cleaner commit history. I think we can close this PR and use this fresh one instead #1733
c97bc8c to
1486fca
Compare
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