开发者

Looking for a library for simple protocol implementation [closed]

开发者 https://www.devze.com 2022-12-16 22:18 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

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 eve开发者_如何学编程n 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.


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消