Hi Stephan,

For some reason Google Groups thought your message was spam, but I fished
it out of the spam bucket. (It looked like you sent three different
versions. I accepted this one and deleted the other two.)

I think your problem may be here:

    auto byteArray = capnp::messageToFlatArray(msgBuilder).asBytes();

asBytes() returns an ArrayPtr which points back at the array on which it
was called. But in this line, you're calling it on a temporary value (the
return value of messageToFlatArray), so you end up with `byteArray` being a
dangling pointer (and so its contents will be garbage). You could fix that
like this:

    auto wordArray = capnp::messageToFlatArray(msgBuilder);
    auto byteArray = wordArray.asBytes();

Or like this:

    auto byteArray = capnp::messageToFlatArray(msgBuilder).releaseAsBytes();

-Kenton

On Wed, Aug 30, 2017 at 2:36 AM, <op...@vs.uni-kassel.de> wrote:

> Hi all,
>
> like some other people before me, I would like to send Cap'n Proto
> Messages via ZeroMQ. Nevertheless I did not manage to make it work, yet.
> The exception I receive on the receiver side is:
>
> Exception catched:  Receiver - src/capnp/serialize.c++:43: failed:
> expected array.size() >= offset; Message ends prematurely in segment table.
>
> *Here is the sending code:*
>
> // cap'n proto part
> ::capnp::MallocMessageBuilder msgBuilder;
> discovery_msgs::Beacon::Builder beaconMsgBuilder =
> msgBuilder.initRoot<discovery_msgs::Beacon>();
> beaconMsgBuilder.setIp(this->wirelessIpAddress);
> beaconMsgBuilder.setPort(6666);
> beaconMsgBuilder.setUuid(kj::arrayPtr(this->uuid, sizeof(this->uuid)));
> auto byteArray = capnp::messageToFlatArray(msgBuilder).asBytes();
>
> // zmq part
>
> this->ctx = zmq_ctx_new();
> this->socket = zmq_socket(ctx, ZMQ_RADIO);
> zmq_connect(this->socket, "udp://224.0.0.1:5555");
> zmq_msg_t msg;
> zmq_msg_init_data(&msg, byteArray.begin(), byteArray.size(), NULL, NULL);
> zmq_msg_set_group(&msg, "TestMCGroup");
> zmq_msg_send(&msg, this->socket, 0);
> zmq_msg_close(&msg);
>
>
> *On the receiving side it is:*
>
> this->ctx = zmq_ctx_new();
> this->socket = zmq_socket(ctx, ZMQ_DISH);
> zmq_bind(socket, "udp://224.0.0.1:5555");
> zmq_join(this->socket, "TestMCGroup")
>
>
> zmq_msg_t msg;
> zmq_msg_init(&msg);
> int nbytes = zmq_msg_recv(&msg, this->socket, 0);
>
> // Received message must contain an integral number of words.
> assert(zmq_msg_size(&msg) % sizeof(capnp::word) == 0);
> auto num_words = zmq_msg_size(&msg) / sizeof(capnp::word);
>
> if (reinterpret_cast<uintptr_t>(zmq_msg_data(&msg)) % sizeof(capnp::word)
> == 0) {
>     // message is aligned
>     auto wordArray = kj::ArrayPtr<capnp::word
> const>(reinterpret_cast<capnp::word const *>(zmq_msg_data(&msg)),
> num_words);
>     ::capnp::FlatArrayMessageReader msgReader = ::capnp::
> FlatArrayMessageReader(wordArray); // <-- Throws Exception:  Receiver -
> src/capnp/serialize.c++:43: failed: expected array.size() >= offset;
> Message ends prematurely in segment table.
>     auto beacon = msgReader.getRoot<discovery_msgs::Beacon>();
>     check(zmq_msg_close(&msg), "zmq_msg_close");
> } else {
>     // message is not aligned
>     std::cerr << "Agent: receive(): Not aligned " << std::endl;
> }
>
> In my own words, I simply try to send a message via UDP-Multicast over a
> zmq RADIO socket to a zmq DISH socket. My experience is that the exact same
> bytes are received, that are also sended before. The message is also always
> memory aligned. The only problem is the recreation of the Cap'n Proto
> message. The constructor of the FlatArrayMessageReader throws an exception
> as indicated in the code above:
> Receiver - src/capnp/serialize.c++:43: failed: expected array.size() >=
> offset; Message ends prematurely in segment table.
>
> My interpretation would be, that the message is not correctly encoded, or
> not correctly aligned in memory (maybe my alignment check is wrong).
>
> Does somebody have a clue, what I am doing wrong?
>
> Kind Regards
>   Stephan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to capnproto+unsubscr...@googlegroups.com.
> Visit this group at https://groups.google.com/group/capnproto.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to capnproto+unsubscr...@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to