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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With