4 Answers
4

I would create a native C# class to represent the packet and its data (not one that attempts to match the wire format, necessarily), and pass it a BinaryReader in the constructor. Have it read its data in appropriate chunks from the data stream:

If you have multiple packet types that share a common header or other leading fields, then you can use inheritance to avoid repetition. The BasePacket class might read and populate the header fields, and the LogonChallenge class would inherit from BasePacket and begin reading the challenge fields after calling the base constructor.

If there's a lot of unsafe code I'd probably look at writing it in C++ instead. Possibly as a C++ COM DLL that can then be called fairly easily from C# if needed, just making sure that the COM interface is easy to match to .Net types. Though maybe there's some better way using Managed C++, which I've never used.

Agree with ho1, I would write small C++/CLI class that wraps this structure. This class possibly needs an interface, which can populate the structure from byte array, and property for every structure member. C# client can construct this class instance from byte array received from a socket, and read every structure member from it as managed property. All durty work may be done in unmanaged code.