views:

70

answers:

2

Hello,

I need to implement a simple over-the-network interaction in C++ and I've been wondering whether there are libraries that do that already. My protocol basically sends messages and receives responses. Each message is just a set of 3-4 values of basic data types. I would like to find a library (or libraries) that can do one or more of the following:

  1. Serialize the values into an efficient byte array (I cannot use text based serialization).
  2. Send the message and wait for the result (it can either lock or receive the response asynchronously, I don't care).
  3. It has to be able to correlate between the sent message and the response.

Ideally I would like to be able to write something like this:

// On the sending side
bool send(const string& str, int x, char y)
{
    Message msg;
    msg  << str << x << y;

    // Lock until the response arrives
    return cool_library::send(address, msg);
}

// On the receiving side
bool receive(Message& msg)
{
    string str; 
    int x; 
    char y;

    msg >> str;
    msg >> x;
    msg >> y;

    if (some conditions...)
        return true; // the message was handled successfully
    else
        return false;
}

Pay attention that cool_library::send returns true not when the message was successfully sent but when the other side responded with success result. All this lengthy explanation is just to show that I need a simple functionality. Nothing fancy. I can even send and receive the buffers myself but I need something that can correlate messages to responses. I don't want to go for RPC because it seems to me as an overkill.

Thank you.

+2  A: 

Google protocolbuffers. I've been using it for a while, and it is fast, extensible and quite lightweight.

It is a binary serialization with time/network optimization in mind.

It doesn't have support for actually "sending" through sockets, but it's really easy if you just send an integer with the size of the message and then the message. That way in the other side you read an integer and know the size of the incoming message to call parseFromArray properly.

Arkaitz Jimenez
Indeed looks like a nice serialization solution. But do you know something that can manage the message life-cycle for me? Things like message responses, retries probably?
FireAphis
Also, what do you think of the boost serialization library?
FireAphis
Never used boost serialization, but if you want something that handles the network stuff and adds more reliability(over TCP) its not going to be that cheap, maybe you should look for more complete frameworks.
Arkaitz Jimenez
Maybe it is a wishful thinking on my side and I cannot get away with something easy. I'm looking now into ACE (http://www.cs.wustl.edu/~schmidt/ACE.html). It's a huge library, but maybe that's the right choice :(
FireAphis
A: 

Go for Google's protocol buffer library. It minimizes data sent and auto generates the cpp files that serialize and deserialize automatically from a declaration file. It is also backwards compatible as you release new versions of your protocol!

http://code.google.com/p/protobuf/

ruibm
It is binary, not text.
Arkaitz Jimenez
Ok, I've just checked it, indeed I can choose between text and binary. So, that's for serialization. But what about the responses?
FireAphis
When you define your protocol you can define the response message type. If you will you may even very easily include the original request as a field in the response.
ruibm