views:

61

answers:

2
+1  A: 

Protobuf has no terminator - so either close the stream, or use your own length prefix etc. Protobuf-net exposes this easily via SerializeWithLenghtPrefix / DeserializeWithLengthPrefix.

Simply: without this, it can't know where each message ends, so keeps trying to read to the end of the stream.

Marc Gravell
Thanks Marc,I'm unclear where you're seeing the SerializeWithLengthPrefix and its corresponding Derserialize...Are you saying I need to call a method on my InstantMessage object before doing:im.WriteTo(networkStream); ?Thanks again
Phil Whittington
@phil - protobuf-net is a different implementation, but the same approach should work. There *may* be an existing method in Jon's version: I don't know for sure.
Marc Gravell
A: 

Sorry for taking a while to answer this. As Marc says, protocol buffers don't have a terminator, and they aren't length prefixed unless they're nested. However, you can put on the length prefix yourself. If you look at MessageStreamIterator and MessageStreamWriter, you'll see how I do this - basically I pretend that I'm in the middle of a message, writing a nested message as field 1. Unfortunately when reading the message, I have to use internal details (BuildImpl).

There's now another API to do this: IMessage.WriteDelimitedTo and IBuilder.MergeDelimitedFrom. This is probably what you want at the moment, but I seem to remember there's a slight issue with it in terms of detecting the end of the stream (i.e. when there isn't another message to read). I can't remember whether there's a fix for it at the moment - I have a feeling it's changed in the Java version and I may not have ported the change yet. Anyway, that's definitely the area to look at.

Jon Skeet
Jon, apologies for the late response - I have been away on a training course.Thanks for the tip - I had a brief look and I'm sure with that explanation I can figure it out.Thanks again, I'm really grateful.PW
Phil Whittington