Repository navigation
Closed
Description
Hi.
I'm using 0.9.0-beta1 version.
I try to develop client-server application and i have some questions about async methods in Msgpack:
- Server-side code:
static async Task SendData(TcpClient client) {
NetworkStream stream = client.GetStream();
var context = new SerializationContext();
context.SerializerOptions.WithAsync = true;
var serializer = context.GetSerializer<Client.PhotoEntry>();
var targetObject = new Client.PhotoEntry { id = 1, title = "My photo" };
// await serializer.PackAsync(stream, targetObject); // 1 - not packing
await serializer.PackToAsync(MsgPack.Packer.Create(stream), targetObject); // 2 - packing
}
If i use PackAsync method to pack data into Network stream, then stream is empty (method NetworkStream.DataAvailable show "false" on client).
If i use PackToAsync method with Packer.Create, then allright (method NetworkStream.DataAvailable show "true" on client).
What's wrong?
- Client-side code:
static async Task ReadData(TcpClient client) {
if(client.GetStream().CanRead) {
while(client.GetStream().DataAvailable) {
NetworkStream stream = client.GetStream();
var context = new SerializationContext();
context.SerializerOptions.WithAsync = true;
var serializer = context.GetSerializer<PhotoEntry>();
var deserializedObject = await serializer.UnpackFromAsync(MsgPack.Unpacker.Create(stream)); // result is null
// var deserializedObject = serializer.Unpack(stream); // allright
try {
Console.WriteLine("Object title=" + deserializedObject.title);
} catch {
}
}
}
}
If i use UnpackFromAsync method with Unpacker.Create, then result is null (method NetworkStream.DataAvailable show "true").
If i use simple Unpack method, then allright (console show my object data).
Where is my mistake?
Activity
yfakariya commentedon Nov 29, 2016
Sorry for delay, and thank you for reporting. I will investigate this.
yfakariya commentedon Dec 4, 2016
I've investigated it.
MessagePackSerializer<T>.PackAsyncinternally specifies toPackerto useBufferedStreamfor performance, but it does not (and terribly be unable to) flush the buffer. I will fix it (design and impl), and the work around is exactly what you did.Unpacker.ReadorUnpacker.ReadAsyncbefore passing toUnpackFromorUnpackFromAsync. I'll add XML doc for it, but this behavior is by design.Fix asynchronous packing does not flush buffering stream #201
Geppard commentedon Dec 8, 2016
@yfakariya, i've tested beta-2 branch with this fix and all works fine. Thank you!
yfakariya commentedon May 4, 2017
Close because it was fixed and verified.