2adnielsenx xx
2adnielsenx xx

Reputation: 488

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

I am trying to communicate between nodes via ZeroMQ. I need to send a struct of:

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

I try to utilize 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;

but it gives:

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

still new to c++.

How can I send my Content struct through ZeroMQ with a help of the msgpack?

Upvotes: 1

Views: 1412

Answers (1)

Jens Luedicke
Jens Luedicke

Reputation: 994

You need to provide a pack() function for your Content struct:

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

}
}
}

If your software does not evaluate the MessagePack data, you need to create the pack() function according to the format/layout expectations of your zmq/MessagePack recipient.

Example of a convert() function (same namespace as pack() function above):

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

Upvotes: 1

Related Questions