如何准备 msgpack 以通过 ZeroMQ 基础结构发送结构?

How to prepare msgpack to send a struct over a ZeroMQ infrastructure?

我正在尝试通过 ZeroMQ 在节点之间进行通信。我需要发送 struct 个:

struct Content{
    std::vector<cv::Mat> image;
    std::string msg;
};

我尝试利用 msgpack:

Content content;
content.image = msg2;
content.mesaj = "naber kardes";

msgpack::type::tuple<Content> src(content);
//                            serialize the object into the buffer.
//                            any classes that implements
//                            write(const char*,size_t) can be a buffer.
std::stringstream buffer;
msgpack::pack(buffer, src);
cout << sizeof(buffer) << endl;

但它给出:

/usr/local/include/msgpack/v1/object.hpp:631:11: error: no member named 'msgpack_pack' in 'Content'

对 c++ 还是新手。

如何在 msgpack 的帮助下通过 ZeroMQ 发送我的内容结构?

您需要为您的 Content 结构提供 pack() 函数:

namespace msgpack
{
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
{
namespace adaptor
{

template <>
struct pack<Content> {
    template <typename Stream>
    msgpack::packer<Stream>& operator()(
                msgpack::packer<Stream>& out, Content const& obj) const
    {
        out.pack(obj.image);
        out.pack(obj.msg);
        return out;
    }
};

}
}
}

如果您的软件不评估 MessagePack 数据,您需要根据 zmq/MessagePack 的 format/layout 期望创建 pack() 函数收件人。

convert() 函数示例(与上述 pack() 函数相同的命名空间):

template <>
struct convert<Content> {
    msgpack::object const& operator()(
                msgpack::object const& o, Content& v) const
    {
        // unpack data in the same format as it was packed. see above!     
        return o;
    }
};