c++capnproto

C++ Capnp serializing message reader


How do you serialize Capnp Message Reader in C++?

For example, there is a way to serialize the Builder

auto flatArray = capnp::messageToFlatArray(&msg_builder);
auto byteBuffer = flatArray.asBytes();

What is the similar function for reader?

Other approach is to convert reader to builder and use code above. Is that feasible?


Solution

  • There's not currently any utility function to serialize a MessageReader. To be honest, I've never had a need to do this. Since a MessageReader always comes from parsing a message in the first place, it usually makes more sense to keep the original bytes around rather than recreate them. That said, since parsing is zero-copy, it is true that a MessageReader still holds pointers to its original input data, so it wouldn't be difficult to add a method to get those back out in order to re-serialize the message. It just never came up...

    Of course, you can indeed copy the message into a MallocMessageBuilder and then serialize that. That'll involve a redundant copy, but it will work.

    (I'm the author of Cap'n Proto.)