Oct 12, 2017 06:36 PM
(This post was last modified: Jan 28, 2018 08:01 PM by Ninja.)
Just a little wrapper I made for the Socket object to add easier to use asynchronous methods to it along with events. You can find the gist here:
https://gist.github.com/GekkeVent/d9ed6a...4973271f34
Usage:
The ConnectAsync, SendAsync, ReceiveAsync, and DisconnectAsync methods all have the same parameters as their non-async equivalent and thus can be used the same way. I did not add awaitable support, since people can do that themselves using the Begin<method> methods and get the AsyncWaitHandle from the IAsyncResult themselves. It does support a state object. The events only work for the Async methods, I did not overload the standard methods.
https://gist.github.com/GekkeVent/d9ed6a...4973271f34
Usage:
Code
AsyncSocket socket = new AsyncSocket();
socket.SocketConnected += (s, e) => Console.WriteLine("Connected!");
socket.DataSent += (s, e) => Console.WriteLine("Data sent!");
socket.DataReceived += (s, e) =>
{
byte[] buffer = e.Buffers[0].Array;
object state = e.State;
Console.WriteLine(Encoding.ASCII.GetString(buffer));
}
socket.SocketDisconnected += (s, e) => Console.WriteLine("Disconnected!");The ConnectAsync, SendAsync, ReceiveAsync, and DisconnectAsync methods all have the same parameters as their non-async equivalent and thus can be used the same way. I did not add awaitable support, since people can do that themselves using the Begin<method> methods and get the AsyncWaitHandle from the IAsyncResult themselves. It does support a state object. The events only work for the Async methods, I did not overload the standard methods.


![[Image: F2G97kn.png]](https://i.imgur.com/F2G97kn.png)

![[Image: Syr3DlV.png]](https://i.imgur.com/Syr3DlV.png)