Comments (3)
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.
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.
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 yourReceivedCallback
handler. You could use aTaskCompletionSource
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)
- (AsyncTcpListener).RunAsync() causes System.NotSupportedException HOT 2
- Any thoughts on handling network disconnection such as cable unplugged? HOT 3
- How to send message from server to all connected clients? HOT 3
- Implement a byte array pool for DequeueInternal HOT 1
- AsyncTcpClient.Dispose() causes "Network error: Software caused connection abort" when using PuTTY HOT 2
- Async Write and concurrency HOT 3
- Async TcpClient use within .NETStandard 2 Xamarin Forms? HOT 3
- How to send the same data from Server to ALL clients? HOT 1
- Is there are particular design reason why you can't Dequeue without Array Copy? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asynctcpclient.