I am using protobuff over the network. Although the following solution does not guarantee that it will not block, it makes life much better:
byte[] emptyByteArray = new Byte[0]; await stream.ReadAsync(emptyByteArray, 0, 0); TaskData d = Serializer.DeserializeWithLengthPrefix<TaskData>(stream, PrefixStyle.Base128);
Since we check the actual data in the stream, before we start deserializing, it will only be blocked when the stream contains a partial message.
Edit: And we can use a similar trick to serialize:
MemoryStream mstm = new MemoryStream(); Serializer.SerializeWithLengthPrefix(mstm, data, PrefixStyle.Base128); await stream.WriteAsync(mstm.GetBuffer(), 0, (int)mstm.Position);
As a bonus, this ensures that it is never blocked.
Dorus source share