Is this a good concept for sending serialized objects over Network?
- 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 coul开发者_如何学Cd 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.
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.
Boost serialization is definitely a start, however there are other considerations such as
- Fragmentation - i.e. what happens if the "collection" of your objects cannot fit in to one packet, how do you know at the other end that you've received all the data that represents the "collection"
- Platforms - if you have a homogenous environment (say all linux), this is not an issue, but if you have a mixture, this could be a problem (I don't believe boost serialization works across different byte encodings - I could be wrong here, need to check docs) This applies to languages too (a point raised by Andy Johnson above) - with boost serialization, you're tied to C++.
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.
精彩评论