Preface: Serialization libraries cannot be used due to restrictions in the development environment.
I have various struct
that need to be serialized so that they may be broadcast 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) = 0;
virtual bool Deserialize(const char* buffer) = 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) {
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) {
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.