Preface: Serialization libraries cannot be used due to restrictions in the development environment, and the latest version usable is C++ 11.
I have various struct that need to be serialized and deserialized so that they may be broadcast and received over UDP.
I would like to define an abstract class for serializable types that has serialization and deserialization methods to be implemented by various message types. This is what I've been working with, but it "smells" to me. Ideally it would be nice to make Deserialize static and have it return a concrete implementation of Serializable, but virtual methods cannot be static:
class Serializable {
public:
virtual std::size_t Serialize(char* buffer, const unsigned int max_message_size) = 0;
virtual bool Deserialize(const char* buffer, const unsigned int max_message_size) = 0;
};
An abstract UDP specific subclass of this may be as follows:
template<typename T>
class UdpMessageBase : public Serializable {
protected:
UDPHeader header; // struct that contains primitive types for metadata about message
T data; // struct that contains message data, subclasses will specialize, see below
bool populated = false;
public:
virtual ~UdpMessageBase() = 0; //abstract class
T getData() {return data;}
UDPHeader getHeader() {return header;}
};
And finally a specific implementation of this for a specific message:
class MySpecificUdpMessage : public UdpMessageBase<structForMySpecificUdpMessage> {
public:
MySpecificUdpMessage() {
initHeader();
}
MySpecificUdpMessage(structForMySpecificUdpMessage data) : data(data) {
initHeader();
populated = true;
}
std::size_t Serialize(char* buffer, const unsigned int max_message_size) {
if (populated) {
// serialize the header and data into buffer to be used by caller
// return total size of serialized data
}
else { // throw error };
}
bool Deserialize(const char* buffer, const unsigned int max_message_size) {
if (populated) {
// throw error
}
else {
// deserialize buffer into header and data of this instance of the object
populated = true;
// return true or false based on success
}
}
private:
void initHeader() {
//set header values specific to this message type
}
};
Example usage:
// for outgoing udp message where we have the data
dataForMySpecificUdpMessage someDataToSend; // assume this was passed into this method
char* buffer; // buffer we want to serialize data into
MySpecificUdpMessage messageToSend = MySpecificUdpMessage(someDataToSend);
messageToSend.Serialize(buffer);
SendUDPMessage(buffer); //arbitrary interface that sends the buffer over UDP to client
//for incoming UDP message
char* buffer; //incoming populated data buffer
MySpecificUdpMessage incomingMessage;
incomingMessage.Deserialize(buffer);
messageProcessor(incomingMessage); //arbitrary message processor
// or
dataProcessor(incomingMessage.getData()); //arbitrary data process for the struct
One alternative approach I've though of is a Serializer class that has a bunch of static overloaded Serialize methods that have different implementations based on the passed in struct. They would simply populate the header based on that info, and deserialize data based on deserialized header, but that class would grow with more messages and doesn't seem very decoupled. I just feel like I'm missing some obvious better practice.
Any tips greatly appreciated.
UdpMessageBaseout out of this. A class that serializes to a generic stream of bytes will prove more useful in the long run, and what if you find you have to serialize a class to UDP and to a file? Inheritance gets messy at that point. But if you just get an array of bytes you can pass that to pretty much any media handler and let the handler do the job inserting any extra protocol wrappings needed by that media.UdpMessageBasewas to enforce the udp-specific-message-header metadata be packaged with the data structure itself upon serialization or deserialization. The header data may be different or unneeded for non-udp variants of the message the serialize thedata, and I don't want an implementer to leave out the header metadata for any udp serializations and only serialize the data in ignorance. The header data is also message specific, not protocol specific.