java - How to determine message type in protobuf so that I can use that type.parsefrom(byte[ ]) -
i trying send protobuf data cpp side java side.
i have multiple message types defined in .proto
on cpp side, have enums every message type , adding buf output follows:
uint8_t* __temp = (uint8_t*)(buf); *__temp++ = (type) >> 8; *__temp = (type) & 0x00ff;
how 'type' have added buf, can achieve
messagetype parsefrom(byte[] data);
it not clear exact requirement. assume trying send different types of messages , the receiver should able parse correct object out of received bytes. can done shown in example below:
message message1 { required string = 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, wrappermessage object sent on wire wraps message1 or message2 object. on receiving side may parse wrappermessage object first , use hasfield method check if m1 or m2 fields present in wrapped object , parse message1 or message2 object out of it.
"oneof" feature may not available on older version of protobuf compiler.
Comments
Post a Comment