ygoe / asynctcpclient Goto Github PK
View Code? Open in Web Editor NEWAn asynchronous variant of TcpClient and TcpListener for .NET Standard.
An asynchronous variant of TcpClient and TcpListener for .NET Standard.
First of all I want to congratulate you on the excellent code.
I have a problem. How to implement sending a message from the server to all connected clients. I've tried to debug to understand but I'm a little lost
Thank you,
Alex
Currently since we allocate a new byte array for every ByteBuffer dequeue we make, it may not be very efficient for high loaded server since it will increase gc by a lot.
Alternatively, a custom byte array pool can be implemented to handle this for performance reasons so that we do less allocations
Dequeue always performs an array copy and returns the bytes.
If consumer Peeks at the data, consumer now has the byte array. Consumer can follow up with a dequeue to remove the bytes from the buffer, but consumer does not necessarily need the byte array returned because consumer already has it.
It seems desireable to be able to Peek at byte buffer, and then Dequeue it without performing a needless array copy.
To me it makes sense to add a VoidDequeue method that allows dequeuing without return. Is there a particular reason this is not implemented?
I added your classes to my code and have to say: nice work. But i had a little issue with AsyncTcpClient:
My Environment:
User Problem:
When using PuTTY to test the communication, the "serverClient.Dispose()" causes PuTTY to complain about a lost connection (Message Box "Network error: Software caused connection abort").
Code Problem:
When calling AsyncTcpClient.Dispose(), the underlying socket gets forcefully terminated without properly diconnecting it.
Solution:
Instead of
tcpClient?.Dispose();
use
if (tcpClient != null){
if (tcpClient.Client != null){
tcpClient.Client.Disconnect(false);
}
tcpClient.Close();
tcpClient.Dispose();
}
Best Regards
Arnothar
Dear Developer,
Thank you for this project.
Although, for example, we have a code where:
... // Somehere in AsyncTCPClient's initialization
ReceivedCallback = (c, count) =>
{
Console.WriteLine("2");
response = c.ByteBuffer.Dequeue(count);
if (responseLength + count <= int.MaxValue)
responseLength += count;
Console.WriteLine("3, responseLength = {0}", responseLength );
return Task.CompletedTask;
}
...
ct1 = new CancellationTokenSource(1000); // 1000ms for a request timeout
t1 = this.asyncTCPClient.Send(new ArraySegment<byte>(data, 0, data.Length), ct1.Token);
if (ct1.IsCancellationRequested) // If connection or any answer timeout
{
Console.WriteLine("Timeout");
}
else
{
ct1 = new CancellationTokenSource(1000); // 1000ms for a response timeour
Console.WriteLine("1");
t1 = this.asyncTCPClient.WaitAsync(ct1.Token); // Wait for any response available
Console.WriteLine("4, responseLength = {0}", responseLength);
}
And the output would be:
1
4, responseLength = 0 // It seems that WaitAsync does not wait while ByteBuffer dequeues.
2
3, responseLength = 41
Is it possible to force WaitAsync
to wait while the full transaction would be completed?
Hello,
Thank you for sharing this promising library. I have a question regarding async write. Indeed, you don't provide a similar mechanism as what you do for reading.
As I understand the logic of your code, you pull asynchronously (but sequentially) every bytes that are available from the stream.
Why don't you chose to not provide such a buffer for writing the packets sequentially ad then pushing asynchronously all the packets to the (write) stream ? What about concurrency of several tight-loop calls to WriteAsync ?
Thanks,
Lionel
Hi,
I am working an a project that with following requirements
Based on above, is this library helpful in such situation?
By looking at the code, I see use of Console.Read/Write which is not something you do in mobile apps.
Much appreciated,
The close callbacks does not seem to execute for example when unplugging the network cable.
Would be nice if it can.
ReceiveTimeout, SendTimeout do not help (Only for sync methods)
Its like the ReadAsync is stuck waiting for data?
Reducing Keep Alive to 5
// Reduce Keep Alive check time to 5 seconds. // https://social.technet.microsoft.com/wiki/contents/articles/39650.c-the-use-of-keep-alive-to-deal-socket-abnormal-disconnection-method.aspx uint dummy = 0; byte[] inOptionValues = new byte[System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 3]; // Set keepalive on BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); // Interval time between last operation on socket and first checking. example:5000ms=5s BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy)); // After first checking, socket will check serval times by 1000ms. BitConverter.GetBytes((uint)500).CopyTo(inOptionValues, System.Runtime.InteropServices.Marshal.SizeOf(dummy) * 2); tcpClient.Client.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
Or how can I check for socket error after SendAsync then kill the ReadAsync so that it attempts reconnection or exits runAsync?
Thank you for your AsyncTcpClient Solution. It's very useful.
I'd like to send the same data from Server to ALL clients.
For example, in a window of Server App, Click Button then, send the same data to all connected clients. No needs ReceivedCallback.
I have tried to add AsyncTcpClient Classes to list , but I could not do it.
Could you help me?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.