使用 MsgPack 通过 ZeroMQ (zmqpp) 发送数据时会出现"msgpack::v1::insufficient_bytes"错误
Sending data through ZeroMQ (zmqpp) using MsgPack gives 'msgpack::v1::insufficient_bytes' error
我使用zmqpp建立了一个PUB/SUB连接,现在我只想使用头,即msgpack-C的C++11版本,将数据从发布服务器发送到订阅服务器。
发布者必须发送两个int64_t
号码——header_1
和header_2
——然后是std::vector<T>
-data
-,其中T
由(header_1, header_2)
组合确定。
由于没有太多关于如何组合msgpack和zmqpp的例子,所以我想到的想法是使用zmqpp::message::add/add_raw
发送一条三部分消息。每个部件都将使用msgpack进行打包/解包。
发布者将单个数据部分打包如下:
zmqpp::message msg;
int64_t header_1 = 1234567;
msgpack::sbuffer buffer;
msgpack::pack(buffer, header_1);
msg.add(buffer.data(), buffer.size());
接收器像这样打开包装:
zmqpp::message msg;
subscriberSock.receive(msg);
int64_t header_1;
msgpack::unpacked unpackedData;
// crash !
msgpack::unpack(unpackedData,
static_cast<const char*>(msg.raw_data(0)),
msg.size(0));
unpackedData.get().convert(&header_1);
当我运行代码时,我在用户端收到以下错误:
terminate called after throwing an instance of 'msgpack::v1::insufficient_bytes'
what(): insufficient bytes
Aborted
此外,尽管我只调用了add()
3次,但zmqpp似乎已经生成了一条由5部分组成的消息。
Q1:我是否正确打包/拆包了数据
Q2:这是使用zmqpp发送消息包缓冲区的正确方法吗
以下是代码的重要部分:
发布服务器
zmqpp::socket publisherSock;
/* connection setup stuff ...*/
// forever send data to the subscribers
while(true)
{
zmqpp::message msg;
// meta info about the data
int64_t header_1 = 1234567;
int64_t header_2 = 89;
// sample data
std::vector<double> data;
data.push_back(1.2);
data.push_back(3.4);
data.push_back(5.6);
{
msgpack::sbuffer buffer;
msgpack::pack(buffer, header_1);
msg.add(buffer.data(), buffer.size());
cout << "header_1:" << header_1 << endl; // header_1:1234567
}
{
msgpack::sbuffer buffer;
msgpack::pack(buffer, header_2);
msg.add(buffer.data(), buffer.size());
cout << "header_2:" << header_2 << endl; // header_2:89
}
{
msgpack::sbuffer buffer;
msgpack::pack(buffer, data);
msg.add_raw(buffer.data(), buffer.size());
std::cout << "data: " << data << std::endl; // data:[1.2 3.4 5.6]
}
std::cout << msg.parts() << " parts" << std::endl; // prints "5 parts"... why ?
publisherSock.send(msg);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
订阅者
zmqpp::socket subscriberSock;
/* connection setup stuff ...*/
zmqpp::message msg;
subscriberSock.receive(msg);
int64_t header_1;
int64_t header_2;
std::vector<double> data;
std::cout << msg.parts() << " parts" << std::endl; // prints "5 parts"
{
// header 1
{
msgpack::unpacked unpackedData;
// crash !
msgpack::unpack(unpackedData,
static_cast<const char*>(msg.raw_data(0)),
msg.size(0));
unpackedData.get().convert(&header_1);
cout << "header_1:" << header_1 << endl;
}
// header 2
{
msgpack::unpacked unpackedData;
msgpack::unpack(unpackedData,
static_cast<const char*>(msg.raw_data(1)),
msg.size(1));
unpackedData.get().convert(&header_2);
cout << "header_2:" << header_2 << endl;
}
// data
{
msgpack::unpacked unpacked_data;
msgpack::unpack(unpacked_data,
static_cast<const char*>(msg.raw_data(2)),
msg.size(2));
unpacked_data.get().convert(&data);
std::cout << "data:" << data << std::endl;
}
}
编辑:解决的问题:正如@Jens所指出的,打包/发送数据的正确方式是使用zmqpp::message::add_raw()
zmqpp::message msg;
int64_t header_1 = 1234567;
msgpack::sbuffer buffer;
msgpack::pack(buffer, header_1);
msg.add_raw(buffer.data(), buffer.size());
我认为对msg.add(buffer.data(), buffer.size()
的调用不会添加buffer.size()
字节的数组,而是调用message::add(Type const& part, Args &&...args)
msg << buffer.data()
,它可能调用message::operator<<(bool)
,因为指针转换为布尔add(buffer.size())
,然后调用msg << buffer.size()
,后者添加size_t
值作为下一部分
看看zmqpp::message类,使用message::add_raw就可以了。
附言:这一切都没有任何保证,因为我从来没有使用过zmqpp或msgpack。
相关文章:
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 如何在 cpp 中解压缩数字,如果它们是使用 struct.pack(fmt, v1, v2, ..) 打包在 pyth
- Msgpack能否提供更好的性能和与Python的struct.pack()相同的功能?
- msgpack:在不知道类型的情况下解包自定义类
- 调用boost v1.58中的函数时出现模板名称使用无效错误
- Msgpack中是否具有版本控制功能
- Affectiva 可以连接 Kinect v1 吗?
- boost::v1.59 和 v1.60 之间的序列化存档版本
- 低功耗 ESP32 + 具有深度睡眠模式的 LoRa (TTGO LoRa32 OLED 板 V1.0).
- c++14 std::experimental::filesystem::v1 和 c++17 std::filesys
- 使用C++中的MsgPack读取二进制文件
- Autobahncpp 解包复杂的 msgpack 消息
- 编译器错误boost :: asio ::占位符:: boost v1.69的错误
- 用 msgpack 打包多态类结构
- 使用 msgpack-c 时需要清理什么以及何时清理?
- 使用Boost Python V1.67和Ubuntu上的CMAKE构建基本C 项目
- msgpack:将类打包在成员函数中
- msgpack:C++和Java之间的消息传递
- add_edge(v1,v2,graph)-时间复杂性
- 使用 MsgPack 通过 ZeroMQ (zmqpp) 发送数据时会出现"msgpack::v1::insufficient_bytes"错误