1

Has anyone had experience of using cocoaasyncsocket together with google protobuf? I want to seperate frames using a varint, which is simple enough using a client/server combo based on netty, but I don't see a simple way of decoding the initial varint when reading using cocoaasync.

Ellis
  • 689
  • 1
  • 11
  • 24

1 Answers1

0

On the C++ side of things, you'll have to use a combination of ReadVarint32() and VarintSize32() do something like this:

char buf[4];
buf = some_api_call(); /* _Copy/peak_ up to 4 bytes off the wire */
CodedInputStream cos(buf, sizeof(buf) - 1);

uint32_t frame_sz;
if (!cos.ReadInputStream(&frame_sz)) {
  // Unable to read the frame size
  return false;
}

frame_sz should have a valid value that will tell you how many bytes it needs to read.

uint32_t consumed_bytes = CodedOutputStream::VarintSize32(frame_sz);

Now you know how many bytes were consumed to generate frame_sz.

I'd wrap these calls in a library that I extern "C" and would link in from your application.

Sean
  • 9,888
  • 4
  • 40
  • 43