Set maximum size of protobuf object

前端 未结 1 1851
广开言路
广开言路 2021-01-27 10:52

I have some protobuf object, let\'s call it\'s Msg, with some repeated fields. I want to serialize it and send. But i have max limit for package, witch i can send.

相关标签:
1条回答
  • 2021-01-27 11:21

    As described in the documentation, protobuf does not take care of where a message start and stops, so you'll have to do that yourself.

    The output of the protobuf serializer is just a serialized buffer. This means that there is no practical way of setting the maximum size of messages you are sending, so some sort of mechanism is needed.

    From the doc:

    [...] If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. [...]

    Depending on what language you're writing, there are libraries that does length-prefixing for e.g. TCP that you can use.

    You should also be aware of that when sending multiple protobufs on a TCP socket, they are likely to be buffered for network optimizations (concatenated) and sent together. Meaning some sort of delimiter is needed anyway.

    0 讨论(0)
提交回复
热议问题