How to keep some incoming messages as is and reuse?

129 views
Skip to first unread message

Ambrase

unread,
Sep 29, 2020, 10:40:37 AM9/29/20
to Cap'n Proto
Hi

I have an input byte buffer with N messages. There are 2 cases: this byte buffer is zero-byte packed or not.
I read messages like this:
capnp::PackedMessageReader packedReader(cpnpReader.getAdapter(), capnp::ReaderOptions(), cpnpReader.getArray()); (where the adapter is an adapter for InputStream on the top of the received packet from UDP/Mcast, the array is an unpacked output buffer).
or with capnp::FlatArrayMessageReader if the message isn't packed.

I'm trying to save some received messages in the way that they will be movable, unpacked and at the same time ready to send.
I get a reader as:
auto anymessage = packedReader.getRoot<capnp::AnyStruct>();
then I can copy it to a bytebuffer that can be moved or stored:
capnp::copyToUnchecked(anymessage, arrayPtr);

The copied buffer is 8 byte less. I assume it is lacking the WirePointer.
1)Can I somehow save it with WirePointer at all? 
2)How to reuse the saved buffer without the WirePointer as a ready message for 
MallocMessageBuilder? 

I also tried this way:
capnp::AnyPointer::Reader reader = capnp::readMessageUnchecked<capnp::AnyPointer>(reinterpret_cast<const capnp::word*>(storedBuffer.data()));
m_builder.setRoot(kj::fwd<capnp::AnyPointer::Reader>(reader));

But the builder writes 0. So, I did something wrong.

Thanks for your help.

Kenton Varda

unread,
Sep 30, 2020, 6:43:26 PM9/30/20
to Ambrase, Cap'n Proto
Hi Ambrase,

copyToUnchecked() does not write a segment table at the beginning of the message. That's probably why you're seeing a message 8 bytes shorter.

copyToUnchecked() and readMessageUnchecked() are difficult to use correctly (and are insecure when used incorrectly). I strongly recommend against using them.

Instead, consider using capnp::clone(reader) if you just want to clone the message in memory that you can then pass around with move semantics.

-Kenton

--
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/a6951785-d283-4460-a3e5-9ef94a2efffen%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages