[英]Protobuf - Multiple messages from the same class
I have the following message in a proto
file 我在proto
文件中有以下消息
message User
required string name = 1;
required string password = 2;
This only works with some additional coding. 这仅适用于一些其他编码。 When protobuf reads a message from a stream it does not know the length of one message. 当protobuf从流中读取一条消息时,它不知道一条消息的长度。 It sees simply a stream of fields as name/value pairs and no indicator for start or end of a message. 它仅将字段流视为名称/值对,而没有指示消息开始或结束的指示符。
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. 协议缓冲区连线格式不是自定界的,因此协议缓冲区解析器无法确定消息在何处结束。
Google's implementations contains some helper classes for this usecase. Google的实现包含此用例的一些帮助程序类。 Have a look at CodedInputStream and CodedOutputStream . 看一下CodedInputStream和CodedOutputStream 。
One possible way for writing: 一种可能的书写方式:
One possible way for reading: 一种可能的阅读方式:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.