Question

I can't seem to send a vector of a struct that I serialized with msgpack through ZeroMQ.

It's a vector of this struct:

struct MyData
{
    MyData() : id(0), x(0), y(0), a(0) {}
    MyData(const Obj &r) : id(0), x(r.pose[0]), y(r.pose[1]), a(r.pose[2]) {}
    MyData(const Obj *r) : id(0), x(r->pose[0]), y(r->pose[1]), a(r->pose[2]) {}
    double id;
    double x;
    double y;
    double a;
    MSGPACK_DEFINE(id, x, y, a);
};

On the sending side:

data is a std::vector<MyData>

msgpack::sbuffer sbuf;
msgpack::pack(sbuf, data);
zmq::message_t msg(sbuf.data(), sizeof(char *) * sbuf.size(), NULL, NULL);
local_socket->send(msg); // this is just zeromq's send function

Did I construct my sbuffer or message_t wrong?

On the receiving side:

I'm not sure if I was supposed to cast the msg.data() or not but I can't find any good documentation on how to work with ZeroMQ and messagepack.

message_t msg;
server_socket->recv(&msg);

msgpack::unpacked unpacked;
msgpack::unpack(&unpacked, reinterpret_cast<char*>(msg.data()), msg.size());
msgpack::object obj = unpacked.get();

std::vector<MyData> data;
obj.convert(&data);
printf("size %d\n", data.size());

I get the following error:

terminate called after throwing an instance of 'msgpack::type_error'
what(): std::bad_cast Aborted

I would appreciate any help.

Was it helpful?

Solution

This seems to have done the trick for me:

msgpack::sbuffer sbuf;
msgpack::pack(sbuf, data);
zmq::message_t msg(sbuf.size());
memcpy(msg.data(), sbuf.data(), sbuf.size());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top