views:

124

answers:

4

I have the following C struct from the source code of a server, and many similar:

// preprocessing magic: 1-byte alignment

typedef struct AUTH_LOGON_CHALLENGE_C
{
    // 4 byte header
    uint8   cmd;
    uint8   error;      
    uint16  size;       

    // 30 bytes
    uint8   gamename[4];
    uint8   version1;
    uint8   version2;
    uint8   version3;
    uint16  build;
    uint8   platform[4];
    uint8   os[4];
    uint8   country[4];
    uint32  timezone_bias;
    uint32  ip;
    uint8   I_len;

    // I_len bytes
    uint8   I[1];
} sAuthLogonChallenge_C;

// usage (the actual code that will read my packets): 
sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0]; // where buf is a raw byte array

These are TCP packets, and I need to implement something that emits and reads them in C#. What's the cleanest way to do this?

My current approach involves

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct foo { ... }

and a lot of fixed statements to read and write it, but it feels really clunky, and since the packet itself is not fixed length, I don't feel comfortable using it. Also, it's a lot of work.

However, it does describe the data structure nicely, and the protocol may change over time, so this may be ideal for maintenance.

What are my options? Would it be easier to just write it in C++ and use some .NET magic to use that?

Clarification: I also need to handle endian issues and null-padded strings.

+1  A: 

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.

ho1
+6  A: 

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:

public class LogonChallenge
{
    public LogonChallenge(BinaryReader data)
    {
        // header
        this.Cmd = data.ReadByte();
        this.Error = data.ReadByte();
        this.Size = data.ReadUInt16();

        // etc
    }
}

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.

JSBangs
+1  A: 

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.

Alex Farber
A: 

Okay, here's what I've come up with:

abstract class Packet
{
    protected enum T
    {
        Byte,
        UInt16,
        UInt32,
        NullPaddedAsciiString,
        Whatever
    }
    protected struct Offset
    {
        public int offset;
        public T type;                      // included only for readability
        public Offset(int i, T type)
        {
            this.type = type;
            offset = i;
        }
    }

    protected byte[] data;

    byte[] RawData { get { return data; } }

    // getters and setters will be implemented using something like this
    protected UInt16 GetUInt16(Offset o)
    {
        // magic
    }

    protected void Write(Offset o, string s)
    { 
        // magic
    }
}

class cAuthLogonChallenge : Packet
{
    // still not perfect, but at least communicates the intent
    static Offset cmd = new Offset(0, T.Byte);
    static Offset error = new Offset(1, T.Byte);
    static Offset size = new Offset(2, T.UInt16);
    // etc.

    public cAuthLogonChallenge(string username)
    {
        var size = 30 + username.Length
        data = new byte[size];
        Write(cmd, 0x00);
        // etc.
    }
}
Jurily