samedi 26 décembre 2015

Design pattern for message packet header

I am writing my own messaging API using libevent. The data is wrapped by a packet structure which contains a packet body of data byte array and a packet header that contains the entire packet size information (for the reason to invoke callback at a complete stream).

class PacketHeader {
  size_t packet_size;
};

class Packet {
  PacketHeader header;
  uint8_t* body;
};

However, I wanna users be able to customize their own PacketHeader, while I can handle the underlying size information for messaging. What is the best practice to do this? Should I use inheritance or pure virtual abstraction (interface class). I was thinking about the following but not sure if it is the best practice.

template <class Derived>
class PacketHeader{
  size_t packet_size;
  size_t header_size() const {return sizeof(Derived);}
}

Aucun commentaire:

Enregistrer un commentaire