NetSharp

NetSharp.git
git clone git://git.lenczewski.org/NetSharp.git
Log | Files | Refs | README | LICENSE

commit d495b2314b54b3cd04ce467725216f2150f553da
parent e85ecc83ab9406fd1089cc587959eb21d83d64da
Author: Mikolaj Lenczewski <mikolaj.lenczewski308@gmail.com>
Date:   Sun, 26 Apr 2020 12:39:05 +0100

Added separate callback for async operations.

Diffstat:
MNetSharp/NetSharp/NetSharp.xml | 4++--
MNetSharp/NetSharp/Sockets/Datagram/DatagramSocketClient.cs | 4++--
MNetSharp/NetSharp/Sockets/SocketClient.cs | 19++++++++++++++++---
MNetSharp/NetSharp/Sockets/Stream/StreamSocketClient.cs | 6+++---
4 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/NetSharp/NetSharp/NetSharp.xml b/NetSharp/NetSharp/NetSharp.xml @@ -290,7 +290,7 @@ The remote end point which to which to connect the client. </param> </member> - <member name="M:NetSharp.Sockets.SocketClient.ConnectAsync(System.Net.EndPoint@)"> + <member name="M:NetSharp.Sockets.SocketClient.ConnectAsync(System.Net.EndPoint@,System.Threading.CancellationToken)"> <summary> Asynchronously connects the client to the specified end point. If called on a <see cref="F:System.Net.Sockets.SocketType.Dgram" />-based client, this method configures the default remote host, and the client will ignore any packets not coming from this default host (i.e the given <paramref name="remoteEndPoint" />). @@ -564,7 +564,7 @@ <member name="F:NetSharp.Sockets.Stream.StreamSocketClientOptions.PreallocatedTransmissionArgs"> <summary> The number of <see cref="T:System.Net.Sockets.SocketAsyncEventArgs" /> instances that should be preallocated for use in the - <see cref="M:NetSharp.Sockets.Stream.StreamSocketClient.SendAsync(System.Memory{System.Byte},System.Net.Sockets.SocketFlags)" /> and <see cref="M:NetSharp.Sockets.Stream.StreamSocketClient.ReceiveAsync(System.Memory{System.Byte},System.Net.Sockets.SocketFlags)" /> methods. + <see cref="M:NetSharp.Sockets.Stream.StreamSocketClient.SendAsync(System.Memory{System.Byte},System.Net.Sockets.SocketFlags,System.Threading.CancellationToken)" /> and <see cref="M:NetSharp.Sockets.Stream.StreamSocketClient.ReceiveAsync(System.Memory{System.Byte},System.Net.Sockets.SocketFlags,System.Threading.CancellationToken)" /> methods. </summary> </member> <member name="M:NetSharp.Sockets.Stream.StreamSocketClientOptions.#ctor(System.UInt16)"> diff --git a/NetSharp/NetSharp/Sockets/Datagram/DatagramSocketClient.cs b/NetSharp/NetSharp/Sockets/Datagram/DatagramSocketClient.cs @@ -217,7 +217,7 @@ namespace NetSharp.Sockets.Datagram // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncTransmissionCallback, args); if (Connection.ReceiveFromAsync(args)) return new ValueTask<TransmissionResult>( @@ -260,7 +260,7 @@ namespace NetSharp.Sockets.Datagram // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncTransmissionCallback, args); if (Connection.SendToAsync(args)) return new ValueTask<TransmissionResult>( diff --git a/NetSharp/NetSharp/Sockets/SocketClient.cs b/NetSharp/NetSharp/Sockets/SocketClient.cs @@ -39,13 +39,26 @@ namespace NetSharp.Sockets { } + protected void CancelAsyncOperationCallback(object state) + { + SocketAsyncEventArgs args = (SocketAsyncEventArgs)state; + + AsyncOperationToken token = (AsyncOperationToken)args.UserToken; + + token.CompletionSource.SetResult(false); + + DestroyTransmissionArgs(args); + } + protected void CancelAsyncTransmissionCallback(object state) { - AsyncTransmissionToken token = (AsyncTransmissionToken)state; + SocketAsyncEventArgs args = (SocketAsyncEventArgs)state; + + AsyncTransmissionToken token = (AsyncTransmissionToken)args.UserToken; token.CompletionSource.SetResult(TransmissionResult.Timeout); - DestroyTransmissionArgs(token.TransmissionArgs); + DestroyTransmissionArgs(args); } /// <summary> @@ -81,7 +94,7 @@ namespace NetSharp.Sockets // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncOperationCallback, args); if (Connection.ConnectAsync(args)) return new ValueTask( diff --git a/NetSharp/NetSharp/Sockets/Stream/StreamSocketClient.cs b/NetSharp/NetSharp/Sockets/Stream/StreamSocketClient.cs @@ -299,7 +299,7 @@ namespace NetSharp.Sockets.Stream // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncOperationCallback, args); if (Connection.DisconnectAsync(args)) return new ValueTask( @@ -344,7 +344,7 @@ namespace NetSharp.Sockets.Stream // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncTransmissionCallback, args); if (Connection.ReceiveAsync(args)) return new ValueTask<TransmissionResult>( @@ -391,7 +391,7 @@ namespace NetSharp.Sockets.Stream // TODO find out why the fricc we leak memory CancellationTokenRegistration cancellationRegistration = - cancellationToken.Register(CancelAsyncTransmissionCallback, args.UserToken); + cancellationToken.Register(CancelAsyncTransmissionCallback, args); if (Connection.SendToAsync(args)) return new ValueTask<TransmissionResult>(