3 ms·
Your binary blobs are structures with fixed-size primitives and a known layout, on ABIs with manageable structure packing rules: struct my_data { u
by klodolph 5y ago
Your binary blobs are structures with fixed-size primitives and a known layout, on ABIs with manageable structure packing rules:
struct my_data {
uint32_t x;
uint16_t y;
};
You can transmit data structures like this over the network with write(), if you want. You can say, “sizeof(my_data) == 8” and just refuse to support systems where this is not true. On the other end, you can
obj.x = swap32(obj.x);
obj.y = swap16(obj.y);
where necessary, or avoid the swap if both peers have the same endian. Keep in mind that this is illustrative. Not trying to describe the details of how you would design a system from a higher level.
Note that you’re not making any attempt to support all architectures, here. You can restrict yourself to byte-oriented architectures with the normal 8, 16, 32, and 64 bit integer types that have no stricter than natural alignment. That covers a useful, broad range of architectures. Depending on whether you care about architectures like M68k where uint32_t does not have natural alignment, you can manually pad out structures.