I'm writing a client for a server program written in C++. As is not unusual, all the networking protocol is in a format where packets can be easily memcopied into/out of a C++ structure (1 byte packet code, then different arrangements per packet type).
I could do the same thing in C#, but is there an easier way, especially considering lots of the data is fixed-length char arrays that I want to play with as strings? Or should I just suck it up and convert types as needed? I've looked at using the ISerializable interface, but it doesnt look as low level as is required.
I wrote a post on this in 2004 which covers some of the options available for converting a binary stream to a .NET memory structure. I reposted it on my new blog since the old blog site no longer exists.
http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html
Basically you have three options
When you consider the options you should also take into account how the byte ordering might affect you.
As an example I will use the IP header as an example, since at the time of the post I was working with Raw TCP packets.
You need to define your .NET structure that the binary data will be mapped to. For example the IP Header looks like the following.
Note that the StructLayout attribute is only required for first two options, and of course you will need to set the packing as appropriate for the structure that is being serialized from the server.
So in C/C++, given a pointer to a block of memory that contains the data bytes that map to the C/C++ structure you can use the following bit of code to view the block of data as a structure piece of memory, where packet is a byte* to the memory.
Doing the same is C# using /unsafe option and the struct defined above you count use the following code.
The marshaling option would look like the following
And finally you can use the BinaryReader to do this entirely in managed code.
As I mentioned earlier, you might need to consider byte ordering. For example, the above code is not quite correct because the IpHeader does not use the same byte order as is assumed by ReadInt16. ReadInt32 etc. Resolving the issue with the above solution is as simple as using IPAddress.NetworkToHostOrder.