Skip to content

Need help with async methods #201

Closed
Closed

Description

@Geppard

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:

  1. 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?

  1. 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

  1. yfakariya commented on Nov 29, 2016

    @yfakariya
    Member

    Sorry for delay, and thank you for reporting. I will investigate this.

  2. yfakariya commented on Dec 4, 2016

    @yfakariya
    Member

    I've investigated it.

    1. This is bug -- MessagePackSerializer<T>.PackAsync internally specifies to Packer to use BufferedStream for 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.
    2. This is intentional. You must call Unpacker.Read or Unpacker.ReadAsync before passing to UnpackFrom or UnpackFromAsync. I'll add XML doc for it, but this behavior is by design.
  3. added
    bug
    on Dec 4, 2016
  4. Geppard commented on Dec 8, 2016

    @Geppard
    Author

    @yfakariya, i've tested beta-2 branch with this fix and all works fine. Thank you!

  5. yfakariya commented on May 4, 2017

    @yfakariya
    Member

    Close because it was fixed and verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions