Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine message type in protobuf so that I can use that type.parsefrom(byte[ ])

I am trying to send protobuf data from cpp side to java side.

I have multiple message types defined in .proto

On Cpp side, I have enums for every message type and I am adding it to the buf output as follows:

uint8_t* __temp = (uint8_t*)(buf);
*__temp++ = (type) >> 8;
*__temp = (type) & 0x00FF;

How do I get this 'type' that I have added to the buf, so that I can achieve something like

MessageType parseFrom(byte[] data);
like image 701
android.developer Avatar asked Jun 01 '15 01:06

android.developer


People also ask

What encoding does Protobuf use?

Protobuf strings are always valid UTF-8 strings. See the Language Guide: A string must always contain UTF-8 encoded or 7-bit ASCII text.

Does Protobuf handle endianness?

Protocol buffers messages always use little-endian encoding. Implementations running on big-endian architectures should be doing the conversions automatically. If you are receiving data in wrong order, I would suggest using protoc --decode_raw to see whether the error occurs on the transmission or reception side.

Is Protobuf text or binary?

Protobuf is a binary message format crafted by Google and is efficient compared to other message formats like JSON & XML.


2 Answers

It is not clear what is the exact requirement. But I assume you are trying to send different types of messages and the the receiver should be able to parse the correct object out of the received bytes. This can be done as shown in the example below:

message Message1 {
   required string a = 1;
   required string b = 2;
}

message Message2 {
   required int64 id = 1;
   required string data = 2;
}




message WrapperMessage {
    required int64 commonField = 1;
    oneof msg {
        Message1 m1 = 2;
        Message2 m2 = 3;
    }   
}

Basically, always WrapperMessage object is sent over the wire which wraps a Message1 or Message2 object. Then on the receiving side we may parse the WrapperMessage object first and then use HasField method to check if m1 or m2 fields is present in the wrapped object and then parse the Message1 or Message2 object out of it.

"oneof" feature may not be available on older version of protobuf compiler.

like image 144
Nipun Talukdar Avatar answered Oct 20 '22 12:10

Nipun Talukdar


Protobuf 3 introduced a new concept, Any, that handles this. A good description can be found here.

like image 45
Philip Nelson Avatar answered Oct 20 '22 11:10

Philip Nelson