I am receiving packets of data over the network from a C#/Java/dlang/etc. client.

I'm writing the data directly to a socket, one primitive value at a time in little endian format. I would like to receive this super easily in d. Here is an example of roughly what I want to do.

class MoveCommand
{
        byte serialNumber;
        int x;
        int y;
}

public void processMoveCommand(byte[] buffer, offset)
{
MoveCommand command = *(cast(MoveCommand *)(buffer.ptr + offset));
}

When I do MoveCommand.sizeof, it returns 4. When I changed MoveCommand to a struct, it returns 12. Is that going to be reliable on every machine and with every compiler? It seems like I had to use __attributed_packed__ (something like this, it's been 10 years) to guarantee the actual byte layout of a struct in C when working with files. Do I have to even worry about this in D? If so, is there something comparable to attribute packed in D?

The alternative that I'm fearing would be that some compiler would choose word-based boundaries, it would pack that data or something else (I would hope not these). I would rather know how this works now and not have it come back to bite me when I least suspect it on some other machine.

I am happy to read about this stuff, but I don't know where to start. I have the book "The D Programming Language" if there is something in there that I missed.

Reply via email to