- I have a client and a server
- I want to send objects from client to server
- The objects must be send bundled together in a “big packet” containing many objects
- The objects could be in a random order
- The number of objects is not fixed
- There could be objects in the packet which are unknown to the server (so he needs to dump them)
I haven’t got much experience with Serialization.
I would prefer Boosts Serialization-framework (if thats possible with it)
I thought of the following concept
(incomplete Pseudocode, inspired by C++, no specific Boost::Serialization-code):
class SerializableObject
{
virtual int getIdentifier() =0;
virtual Archive serialize() =0;
}
class SubclassA : public SerializableObject
{
int getIdentifier() { return 1; }
Archive serialize() { ... }
...
}
class SubclassB : public SerializableObject
{
int getIdentifier() { return 2; }
Archive serialize() { ... }
...
}
Now on Clientside:
void packAndSendData()
{
archive << objectA.getIdentifier();
archive << objectA;
archive << objectB.getIdentifier();
archive << objectB;
myNetworkObject.sendData(archive);
}
On Serverside:
void receiveAndUnpackData()
{
archive = myNetworkObject.receiveData();
while(archive.containsObjects()) //possible?
{
int type = archive.deserializeNextObject();
if(type == 1)
SubclassA objectA = archive.deserializeNextObject();
if(type == 2)
SubclassB objectB = archive.deserializeNextObject();
else
archive.dropNextObject(); //How to do this? Possible?
}
}
So the questions are:
– Is this a good concept or are there other possibilities?
– Is such a concept possible with Boost::Serialization
– If not: Are there other libs which could help implementing the concept?
I’ve tried to compress the problem as much as possible and to give as much info as I could. Hope it is understandable what I meant and what I try to achieve.
If anyone has a better title for this question please fix the existing one, I had no idea of how describing this question with all of its aspects.
The approach you describe is a start, but have you thought about how you’d serialise references between objects. I.e. serialising an object graph. Also, if you may need to think about data format versioning if your client and server can change out of sync with each other. Its not necessarily a simple problem.
You could look at Google’s Protocol Buffers project. It probably does what you want, and is language neutral.