views:

70

answers:

2
  • 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.

+1  A: 

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.

Andy Johnson
A: 

Boost serialization is definitely a start, however there are other considerations such as

  1. 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"
  2. 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.

Nim
There is no problem with being bound to C++. But the application is very performance-focused, so Boost seems to be a good solution. (Another point is that we already use Boost as Thirdparty in our company, so there is no need to bring in new Thirdparty)
MOnsDaR
As far as I know, Boost::Serialization does Data Marshalling. But I'll check this too. Edit: One major point of the Boost::Serialization-goals is that data which has been serialized on one platform comes out the same on another platform. See this link for more info: http://www.boost.org/doc/libs/1_44_0/libs/serialization/doc/index.html
MOnsDaR