I'm working on a network program that will deal with packets having a header portion and a payload portion. The header is variable-length, with extension segments added or removed depending on network conditions. The user data portion is a plain byte buffer, although it may be treated as some sort of structure or other during packet construction or use.
I would like to have a base class that builds & manipulates the header, with the header bytes (possibly) shifted forwards or backwards if necessary to insert an extension field, and a derived class containing the user data. For example:
class hdr {
public:
const void *start( void ) { return uint8_t(pBuf) + sizeof(pBuf) - SIZEOF_HEADER - pExtBytes; }
private:
size_t pExtBytes;
uint32_t pBuf[16]; // header data goes here, shifted to the END of the buffer, adjacent the beginning of the derived class
// NO PADDING HERE!
} ;
class payload1 : public hdr {
public:
private:
uint8_t pData[128];
} ;
class payload2 : public hdr {
public:
private:
uint16_t pData[12];
} ;
Is there a standard way to guarantee that there's no padding between hdr
and payload1
or payload2
, so that I can pass start()
to write()
and have the header and contiguous payload transmitted out the network? For example, if (sizeof(hdr) % BIG_STANDARD_ALIGNMENT) == 0)
then derived classes of hdr
will start without any extra padding?
An alternative is to use scatter/gather I/O, but that seems like a complicated setup, and I'm not sure it works to gather packet-fragments together for transmission in a single UDP packet.