O
O
oleg0xff2016-06-28 21:54:11
Programming
oleg0xff, 2016-06-28 21:54:11

How to accept an object of unknown type using protobuf?

I have such a task - two applications will exchange data over the network using protobuf. Protobuf has the ability to save/restore an object from a network stream using the ParseFromIstream / SerializeToOstream function. The problem is that it is a member of the object class - i.e. I must know in advance what class the object will come to me. I don't know, they come randomly. I need to be able to read the stream, understand what object has now arrived and create the appropriate class. I used to do this using my own protocol, but this seems to be not good, it would be necessary to do it through some standard one like protobuf. I can't think of a way to do this with protobuf yet, and I'm already thinking of using JSON instead, although otherwise protobuf fits a lot better than JSON. I would be grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
terrier, 2016-06-29
@terrier

The most reasonable way here is to add the number of the protobuf type to the beginning of the message, and then the protobuf object itself. It is clear that both parties must agree in advance which number corresponds to which type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question