I have a black box. I connect to it via TCP. The black box helpfully provides an Interface Control Document which tells me the format of the data I'll be recieving. It may be something like:
- Bytes 0-3 UINT32: id of thing
- Bytes 4-8 UINT32: type of thing
- [and, you, know, like 100 other elements of varying data types]
In C#, how can I get the data I have taken off the TCP socket and get it into the format I know it to be in?
In C, I could just memcpy from a buffer into a struct. Or I could recast the buffer to become the struct I know it to be. I've been casting around for how to best do this in C# and so far find the answers to be somewhere between baffling and way more complicated than I was expecting (C# lacks typedefs and seems to frown on structs in general). So far my best attempt is to make the message as a class:
class MessageThing
{
public uint thingId;
public uint thingType;
[...insert loads of other data fields here...]
}
...and then painfully use BitConverter.ToUint32(buffer, offset) to copy, piece by piece, into my class, from my message buffer. Surely there is a more efficient way of parsing than this. (I found this sort of thing to be super easy in Python and Java, and actually easy enough in C, so I'm clearly missing something for C#?)