<分区>
我需要用 C++ 实现一个简单的网络交互,我一直想知道是否已经有库可以做到这一点。我的协议(protocol)基本上发送消息并接收响应。每条消息只是一组 3-4 个基本数据类型的值。我想找到一个可以执行以下一项或多项操作的库:
- 将值序列化为高效的字节数组(我不能使用基于文本的序列化)。
- 发送消息并等待结果(它可以锁定或异步接收响应,我不在乎)。
- 它必须能够关联发送的消息和响应。
理想情况下,我希望能够写出这样的东西:
// 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;
}
注意 cool_library::send 不是在消息发送成功时返回 true,而是在对方响应成功时返回 true。所有这些冗长的解释只是为了表明我需要一个简单的功能。没有什么花哨。我什至可以自己发送和接收缓冲区,但我需要一些可以将消息与响应相关联的东西。我不想选择 RPC,因为在我看来它有点矫枉过正。
谢谢。