Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protobuf determining message type to deserialize

With protobuf we can define messages and generate their classes and each will know how to serialize/deserialize to binary.

Suppose we have multiple different messages already defined, and we get some byte[] off the wire, how do we determine the message type to use appropriate class and deserialize?

like image 264
vach Avatar asked Sep 17 '15 21:09

vach


1 Answers

You can't. Protocol buffers' wire format does not encode the message type, only the tag numbers and types.

For instance, the wire format of instances of messages of the following protos will be identical (with the same data in the string field, obviously):

message Foo {
  optional string foo_field = 1;
}

message Bar {
  optional string field_contained_in_bar = 1;
}

and an instance of the following message might have the same encoding too, if only the string field is set:

message Baz {
  optional string str = 1;
  optional int32 num = 2;
}

You need to know which message type(s) you are expecting to receive.

Please refer to the examples of the encoding in the documentation.

like image 117
Andy Turner Avatar answered Sep 19 '22 20:09

Andy Turner