Hi Ryan,

On Thu, May 19, 2016 at 10:33 AM, <[email protected]> wrote:

> So this seems trivial but I've been at it for a while and am not making
> much headway so I'm clearly have some fundamental misunderstanding. I'm
> trying to use raw sockets to send and receive messages, standard berkeley
> sockets. Here's what I have so far:
>
> server:
> ::capnp::MallocMessageBuilder builder;
> auto mydata = builder.initRoot<MY_TYPE>();
> mydata.setField1(...
> ...
> capnp::writeMessageToFd(sockfd, builder);
>

OK, you've written a message to the socket. So far so good.


>
> client:
> ::capnp::MallocMessageBuilder builder;
> capnp::readMessageCopyFromFd(sockfd, builder);
>

OK, you're reading a message from the socket. Normally the best way to do
this is using capnp::StreamFdMessageReader as it does fewer copies. The
only reason to read directly into a builder is if you plan to modify the
contents and then write them back out again.

So I'd suggest:

    capnp::StreamFdMessageReader reader(sockfd);


> auto data = messageBuilder.getSegmentsForOutput();
>

I think this is where the confusion starts. You probably shouldn't call
getSegmentsForOutput() -- that's meant for use when writing ("output"), and
is usually called internally by e.g. capnp::writeMessageToFd(). Application
code normally doesn't concern itself with segments.

What you probably wanted to do here is:

    MY_TYPE::Builder rootBuilder = builder.getRoot<MY_TYPE>();

Or if you switched to StreamFdMessageReader:

    MY_TYPE::Reader rootReader = reader.getRoot<MY_TYPE>();


>   for(const auto& datum : data) {
>     capnp::FlatArrayMessageReader reader(datum); //throws error here
>

Here you seem to be iterating over the message segments and expecting each
one to be a Cap'n Proto message in itself. But, they aren't. So it's
expected that this would throw.

There is example code here, FWIW:

    https://capnproto.org/cxx.html

-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 [email protected].
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to