So like I said... you have to write it as
<header length><header which contains message length><message>
To decode it, you can stick the header length as a varint, all via the
coded input/output stream. Sample code for encoding:
makePacket(Message message) {
String name = ... (there has gotta be a way of getting the typename
from the message)
length = message.getSerializedSize()
Header h = Header.newBuilder().setName(name).setLength(length);
CodedOutputStream os = newInstance();
os.writeInt32(h.getSerializedSize());
os.writeMessageNoTag(h);
os.writeMessageNoTag(message);
}
And to decode:
Message readPacket() {
CodedInputStream is;
hlen = is.readInt32();
limit = is.pushLimit(hlen);
Header h = Header.parseFrom(is);
is.popLimit(limit);
is.pushLimit(h.getLength());
messageFactory.getBuilderForType(h.getName()).mergeFrom(is);
}
And you're done. Protobuf is meant for serializing data, not framing
it. Your complaint about lack of framing is well taken, but ... it's
just not part of what protobuf does.
You could just as well have a
message TheOneTrueMessage {
string name;
bytes data;
}
And then always decode that -- this gives you your framing. But this
ends up causing lots of copies of the data.
-ilia