Sharing data between C/C++ and python

1,256 views
Skip to first unread message

mania...@gmail.com

unread,
Sep 1, 2014, 1:16:17 AM9/1/14
to capn...@googlegroups.com
Hi all,

Basically, I want to build a message in python then share it ( via serializing/deserializing ) with in C++ and do stuff, then again share the results 
back to python. Apparently there is a way to deserialize the object/message back from byte array in python, but I dont know how to deserialize it in C++.

I just want to emphasis that I am looking in share memory approach No files based method appreciated.

Anybody have any suggestion ?

Regards
Mani

Kenton Varda

unread,
Sep 2, 2014, 5:37:15 PM9/2/14
to mania...@gmail.com, capnproto
Hi Mani,

Not sure I fully understand your question. To deserialize a byte array in C++, use capnp::FlatArrayMessageReader. Use capnp::messageToFlatArray() to serialize it again. Both are defined in capnp/serialize.h.

-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.
Visit this group at http://groups.google.com/group/capnproto.

mania...@gmail.com

unread,
Sep 3, 2014, 9:30:07 AM9/3/14
to capn...@googlegroups.com, mania...@gmail.com
Hi Kenton

Thanks for the reply,

Let me explain more about my question,

In python I use pyCapnp to serialize a message by using messgae.to_bytes()
the serialize is a string format, so I can simply read string ( via ctype ) in c++ DLL
but  FlatArrayMessageReader get a array of word as input, 
how can I conver a String to array of Word ?

And vice versa ?

Appreciate your help in advance

Mani 

Kenton Varda

unread,
Sep 4, 2014, 2:37:48 AM9/4/14
to Mani Abedini, capnproto
If the data is aligned, you can use a reinterpret_cast, like so:

kj::ArrayPtr<const capnp::word> toWordsAlias(const char* data, size_t size) {
  return kj::arrayPtr(reinterpret_cast<const capnp::word*>(data), size / sizeof(word));
}

If you don't know for sure whether the data is aligned, you will need to make a copy instead:

kj::Array<word> toWordsCopy(const char* data, size_t size) {
  auto result = kj::heapArray<word>(size / sizeof(word));
  memcpy(result.begin(), data, size);
  return result;
}

-Kenton

Henry Harrison

unread,
Feb 2, 2016, 11:31:04 AM2/2/16
to Cap'n Proto, mania...@gmail.com
I have a similar problem. I am trying to convert my buffer to a char* and then back to a FlatArray for reading.
I am getting an error stating that: Caught exception in main: src/capnp/serialize.c++:43: failed: expected array.size() >= offset; Message ends prematurely in segment table.

My code looks like this:
auto msg = capnp::messageToFlatArray(messagebuilder);
memcpy(target_, msg.begin(), (msg.size()*sizeof(capnp::word)));


auto words = kj::heapArray<capnp::word>(sizeof(target_)*sizeof(char)/ sizeof(capnp::word));
memcpy(words.begin(), &target_, sizeof(target_));
capnp::FlatArrayMessageReader message(words);
Message::Reader messageReader = message.getRoot<Message>();

thanks

Kenton Varda

unread,
Feb 5, 2016, 5:21:33 PM2/5/16
to Henry Harrison, Cap'n Proto, Mani Abedini
Hi Henry,

On Tue, Feb 2, 2016 at 8:31 AM, Henry Harrison <hdh...@york.ac.uk> wrote:
auto words = kj::heapArray<capnp::word>(sizeof(target_)*sizeof(char)/ sizeof(capnp::word));
memcpy(words.begin(), &target_, sizeof(target_));

sizeof(target_) looks suspicious here. How is target_ defined? If it has a fixed width, perhaps that width is too small for the message. If it is dynamically-allocated, then sizeof(target_) gives you the size of the pointer.

(Also note that multiplying by sizeof(char) is logically incorrect since sizeof(target_) returns bytes, not an element count -- but since sizeof(char) is 1 anyway this probably isn't affecting anything.)

-Kenton
Reply all
Reply to author
Forward
0 new messages