Coder Social home page Coder Social logo

Comments (3)

ygoe avatar ygoe commented on May 18, 2024 2

The AsyncTcpClient.WaitAsync method waits for data to be available in the receive buffer. It doesn't wait until you processed it. If you want that, you should set a signal when you're done, at the end of your ReceivedCallback handler. You could use a TaskCompletionSource for that.

from asynctcpclient.

ygoe avatar ygoe commented on May 18, 2024 2

You're calling AsyncTcpClient.WaitAsync and expect to wait until you finished processing the received data. That's the wrong method. You're waiting for new data to be available. Instead of this you should wait for your own event.

TaskCompletionSource<T> is a class that lets you create tasks you can wait on. Later, you can explicitly complete these tasks "from the outside", by calling a method like TrySetResult. Whoever waits for this task can then continue. It's to common pattern to asynchronously wait for something you do. It's also used in AsyncTcpClient.WaitAsync to wait for either new data or a closed connection.

from asynctcpclient.

artshade avatar artshade commented on May 18, 2024

The AsyncTcpClient.WaitAsync method waits for data to be available in the receive buffer. It doesn't wait until you processed it. If you want that, you should set a signal when you're done, at the end of your ReceivedCallback handler. You could use a TaskCompletionSource for that.

Thank you, but how to wait for TaskCompletionSource if WaitAsync returns immediately when any data was received?

Currently, this one works:

ct1 = new CancellationTokenSource(1000);
this.asyncTCPClient.WaitAsync(ct1.Token); // Wait for any response available
if (!ct1.IsCancellationRequested) // If received something
{
    ct1 = new CancellationTokenSource(1000);
    while (responseLength != expectedResponseLength) // Wait for all requested data transferred
    {
        if (ct1.IsCancellationRequested) // If full data transfer timed out
        {
            fullDataTimeout= true; // Set flag of data's timeout
            break;
        }
        Thread.Sleep(100); // To escape a busy loop issue
    }
}
else
    connectionTimeout= true; // Set flag of connection's timeout

It works. However, it seems like it is not the best solution. Sorry, but what did you mean by TaskCompletionSource?

from asynctcpclient.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.