You need explicit lengths. Usually this is done as <header length varint><header><body>. And the header contains the body length in it. In Java, there's a CodedInputStream/OutputStream which makes it easy to consume fixed lengths (push/popLimit) as well as raw varints (as for the initial header length). Other languages have similar abstractions.
On Sat, Jul 7, 2018 at 2:26 PM, John Lilley <[email protected]> wrote: > I am posting protobuf messages to a message broker, and in order to identify > them, I prefix the message bytes with the serialized result of a "header" > message: > > message Header { > int version = 1; > string message_type = 2; > } > > It is easy, to concatenate the header+actual message bytes and post the > resulting block to a queue. But how do I take these apart on the receiving > end? Suppose I get a byte-buffer consisting of: > > --------------- > | header | > --------------- > | body | > --------------- > > Is it OK to throw this oversized buffer at the Header deserialization? Will > the extra bytes hurt anything? > > Then, once I extract the Header message, how do I know where the body > begins? I could turn around and ask the Header object "how big would you be > if serialized?". Is that reliable? Is there a better way? > > Thanks > john > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/protobuf. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
