Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for determining the type of a protocol buffers message

I need to serialize and deserialize a sequence of protocol buffers messages to and from a byte stream. There are a handful of predetermined message types. What is the recommended way of encoding the type information so that my application can know which type it should be reading?

like image 935
user583933 Avatar asked Sep 25 '14 21:09

user583933


1 Answers

The most common way to do this is to use a union message.

For example:

message AnyMessage {
    optional Message1 msg1 = 1;
    optional Message2 msg2 = 2;
    ...
}

Then all messages are encoded/decoded inside an AnyMessage container. Starting with protobuf 2.6, you can also use the oneof specifier which will ensure that only one of the submessages is set.

like image 189
jpa Avatar answered Sep 21 '22 04:09

jpa