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 2 times, most recently from 960e7a4 to ad27a6c Compare October 14, 2025 03:29
…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
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.

This looks very promising.

// Frame Info Command Packet
////////////////////////////////////////////////////////////////////////////////

struct NetPacketFrameCommand {
Copy link

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.

Copy link
Author

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

Copy link

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.

Copy link
Author

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?

Copy link

Choose a reason for hiding this comment

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

In a future Pull.

@bobtista
Copy link
Author

@xezon any changes you want still?

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.

Did you push all changes? (Some Review Comments were closed without response or action)

@bobtista
Copy link
Author

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.

@xezon
Copy link

xezon commented Oct 21, 2025

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.

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 think it is good. Needs replication in Generals.

@xezon xezon added the Approved Pull Request was approved label Oct 22, 2025
@bobtista
Copy link
Author

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.

@xezon
Copy link

xezon commented Oct 22, 2025

Copying files from ZH to Generals is risky, unless they are identical. Better use script to replicate:

Batch:

FOR /F "delims=" %%b IN ('git merge-base --fork-point main') DO git diff %%b > changes.patch
git apply -p2 --directory=Generals --reject --whitespace=fix changes.patch
del changes.patch

@bobtista
Copy link
Author

Copying files from ZH to Generals is risky, unless they are identical. Better use script to replicate:

Batch:

FOR /F "delims=" %%b IN ('git merge-base --fork-point main') DO git diff %%b > changes.patch
git apply -p2 --directory=Generals --reject --whitespace=fix changes.patch
del changes.patch

Ah ok. 118 rejected hunks. I'll try going through them like it's a merge conflict.

@xezon
Copy link

xezon commented Oct 22, 2025

It drops *.rej files that you can inspect.

@bobtista
Copy link
Author

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

@xezon
Copy link

xezon commented Oct 22, 2025

Yes. At some places Generals code is a bit different and then needs attention to sort out.

@bobtista
Copy link
Author

Ok so using this method to compare the original files from ZH to Generals on main:

diff <(git show main:Generals/Code/GameEngine/Source/GameNetwork/NetPacket.cpp) <(git show main:GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp)
2c2
< **    Command & Conquer Generals(tm)
---
> **    Command & Conquer Generals Zero Hour(tm)

The changes should be exactly the same for NetPacket.cpp, NetCommandMsg.h, and NetCommandMsg.cpp
The differences were in cmakefiles and one difference in ConnectionManager.cpp which was preserved. Ah. I made a mistake with the patch command, used the wrong commit. I'll do it again, should be easier and cleaner

Yes. At some places Generals code is a bit different and then needs attention to sort out.

@bobtista
Copy link
Author

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) {
Copy link

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?

Copy link
Author

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?

Copy link

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.

Copy link

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.

Copy link
Author

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.

Copy link

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?

Copy link
Author

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

@bobtista bobtista force-pushed the bobtista/packedstructpackets branch from c97bc8c to 1486fca Compare October 22, 2025 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved Pull Request was approved 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