pizheng / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Difference between TypeModel.Deserialize and TypeModel.DeserializeWithLengthPrefix? #182

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
After serializing (using Protobuf-net v2.0.0.404) using

typeModel.SerializeWithLengthPrefix(ms, myObj, typeof(MyObject), 
PrefixStyle.Base128, 70);

where ms is a MemoryStream, I successfully deserialized using either

typeModel.Deserialize(ms, myObj, typeof(MyObject));
or
typeModel.DeserializeWithLengthPrefix(ms, myObj, typeof(MyObject), 
PrefixStyle.Base128, 70);

However, after transferring the byte[] over the network, the first call still 
works, but the second call hangs the thread indefinitely. Any idea why?

Original issue reported on code.google.com by Erik.Vul...@gmail.com on 9 Jun 2011 at 12:30

GoogleCodeExporter commented 9 years ago
are you sure those two "first call" and "second call" are presented the right 
way around?

To clarify: the default behaviour of a protobuf stream for the *outermost 
object* is to read to the end of the stream, which is fine for files, but is a 
PITA on network streams if you haven't closed it yet. Hence, *WithLengthPrefix, 
which sends a marker first to limit the amount of data consumed - and making it 
possible to have a simple conversation without issue.

I would expect the DeserializeWithLengthPrefix approach to *work*, and the 
Deserialize approach to block until the network stream is closed.

Also, note that:

typeModel.Deserialize(ms, myObj, typeof(MyObject));

and

typeModel.DeserializeWithLengthPrefix(ms, myObj, typeof(MyObject), 
PrefixStyle.Base128, 70);

are *not* compatible - they do different things. Deserialize is compatible with 
Serialize. DeserializeWithLengthPrefix is compatible with 
SerializeWithLengthPrefix.

Original comment by marc.gravell on 13 Jun 2011 at 8:30