I have a server-client application. The server is in C++ while the client is in C#. The server might be running on a Linux instance while the client is in Windows. Hence we cannot make any assumptions about the architecture and hence endianness. I am using protobuf to send data structures back and forth. Currently have adopted a strategy of sending a message as a header which will contain the size of the next message which is a large data structure. It looks like:
message message_size {
required fixed64 size = 1;
}
Is there a way I can know how many bytes protobuf will be sending on both ends, which is how many bytes will the above structure be serialized into?
The ultimate problem here is that root protobuf messages are not length prefixed (by design). There is no explicit agreed-upon API for dealing with multiple messages over an open stream, so basically: any length prefix that you care to encode and handle would work. A common pattern is to treat it using protobuf rules with a dummy field-number; in the case of protobuf-net, that is what SerializeWithLengthPrefix
and DeserializeWithLengthPrefix
does; but: that may not be easily available from C++. It might be easier just to encode the length manually using fixed-width/endianness rules, then dump the raw payload right after.