I am developing my message passing interface using protobuf
and non-blocking libevent
socket libraries. Because of the non-blocking properties, the read callback by libevent
might be invoked at some points where the messages are not complete (e.g., every 4096 bytes received). This means I have to write another wrapper/header indicating the size of each message so I can parse the message back to the protobuf
object correctly. Are there better solutions for this? Or does protobuf
provide an API to handle this situation?
No, there is no better solution. In fact, even when using blocking I/O, you need to write a header indicating the size, because Protobufs are not self-delimiting (by default the protobuf parser will always read until EOF!).