Reputation: 8664
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.
Upvotes: 0
Views: 873
Reputation: 33645
Boost serialization is definitely a start, however there are other considerations such as
I would recommend that you look at one of the OS messaging products, and specifically if you want to send structures, something like Open DDS. All the serialization etc. is handled nicely for you, and you get lot's more features and functionality. Of course it's quite heavy weight, and slightly less performant than asio + serialization, but you have to do less work.
Upvotes: 0
Reputation: 8179
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.
Are there other libs which could help implementing the concept?
You could look at Google's Protocol Buffers project. It probably does what you want, and is language neutral.
Upvotes: 1