使用MsgPack通过ZeroMQ(zmqpp)发送数据给出了msgpack :: v1 :: insufficient_bytes'错误

时间:2015-08-22 16:17:30

标签: c++ c++11 zeromq msgpack messagepack

我使用zmqpp建立了PUB / SUB连接,现在我想使用仅限标题的C ++ 11版msgpack-c将数据从发布者发送给订阅者。

发布商必须发送2个int64_t个号码 - header_1header_2 - 然后是std::vector<T> - data - ,其中{ {1}}由T组合确定。

Sinse没有关于如何组合msgpack和zmqpp的许多例子,我想出的想法是使用zmqpp::message::add/add_raw发送一个3部分的消息。使用msgpack,每个部分都是packed / unpacked

发布商按如下方式打包单个数据部分:

(header_1, header_2)

接收器像这样解压缩它:

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);

此外,似乎zmqpp已经生成了一个由5部分组成的消息,即使我只调用了terminate called after throwing an instance of 'msgpack::v1::insufficient_bytes' what(): insufficient bytes Aborted 3次。

Q1:我是否正确打包/解包数据?

Q2:这是使用zmqpp发送msgpack缓冲区的正确方法吗?

以下是代码的重要部分:

发布商

add()

订户

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));
}

编辑:问题已解决:正如@Jens指出的那样,打包/发送数据的正确方法是使用zmqpp::message::add_raw()

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;
    }

}

1 个答案:

答案 0 :(得分:2)

我认为对msg.add(buffer.data(), buffer.size()的调用不会添加buffer.size()字节数组,而是调用message::add(Type const& part, Args &&...args)

  1. msg << buffer.data(),由于指针转换为bool
  2. ,可能会调用message::operator<<(bool)
  3. add(buffer.size())然后调用msg << buffer.size(),其中添加size_t值作为下一部分。
  4. 查看zmqpp :: message类,使用message :: add_raw应该可以解决问题。

    PS:这一切都没有任何保证,因为我从未使用过zmqpp或msgpack。