public static Task<Socket> AcceptAsync(this Socket socket) { var tsc = new TaskCompletionSource<Socket> (); socket.BeginAccept (asyncResult => tsc.SetResult (socket.EndAccept (asyncResult)), null); return tsc.Task; }
public static Task<Socket> AcceptAsync(this Socket socket) { if (socket == null) throw new ArgumentNullException("socket"); var tcs = new TaskCompletionSource<Socket>(); socket.BeginAccept(result => { try { var s = result.AsyncState as Socket; var client = s.EndAccept(result); tcs.SetResult(client); } catch (Exception ex) { tcs.SetException(ex); } }, socket); return tcs.Task; }
public static Task<Socket> AcceptAsyncTask(this Socket socket, CancellationTokenSource cancellationTokenSource) { return Task<Socket>.Factory.FromAsync( (ac, o) => socket.BeginAccept(ac, o), ar => { cancellationTokenSource.Token.ThrowIfCancellationRequested(); return socket.EndAccept(ar); }, null); }
public static Task<Socket> AcceptAsync(this Socket socket) { var source = new TaskCompletionSource<Socket>(socket); socket.BeginAccept(state => { try { source.SetResult(socket.EndAccept(state)); } catch (Exception e) { source.SetException(e); } }, source); return source.Task; }
public static Task<Socket> AcceptAsync(this Socket socket) { var tcs = new TaskCompletionSource<Socket>(socket); socket.BeginAccept(ar => { var t = (TaskCompletionSource<Socket>)ar.AsyncState; var s = (Socket)t.Task.AsyncState; try { t.TrySetResult(s.EndAccept(ar)); } catch (Exception ex) { t.TrySetException(ex); } }, tcs); return tcs.Task; }
public static Task<Either<Socket, SocketError>> AcceptNonBlocking(this Socket s) { var tcs = new TaskCompletionSource<Either<Socket, SocketError>>(); try { IAsyncResult r = s.BeginAccept(iar => { try { Socket rs = s.EndAccept(iar); tcs.SetResult(rs); } catch (SocketException skex) { tcs.SetResult(skex.SocketErrorCode); } catch (Exception ex) { tcs.SetException(ex); } }, (object)null); Debug.Assert(r != null); } catch (SocketException skex) { tcs.SetResult(skex.SocketErrorCode); } catch (Exception ex) { tcs.SetException(ex); } return tcs.Task; }
public static void AcceptAPM(this Socket socket, Action<Socket> handler) { var callback = new AsyncCallback(asyncResult => handler(((Socket)asyncResult.AsyncState).EndAccept(asyncResult))); socket.BeginAccept(callback, socket); }
/// <summary> /// Extends BeginAccept so that when a state object is not needed, null does not need to be passed. /// <example> /// socket.BeginAccept(acceptSocket, receiveSize, callback); /// </example> /// </summary> public static IAsyncResult BeginAccept(this Socket socket, Socket acceptSocket, Int32 receiveSize, AsyncCallback callback) { if(socket == null) throw new ArgumentNullException("socket"); return socket.BeginAccept(acceptSocket, receiveSize, callback, null); }
/// <summary> /// Extends BeginAccept so that when a state object is not needed, null does not need to be passed. /// <example> /// socket.BeginAccept(callback); /// </example> /// </summary> public static IAsyncResult BeginAccept(this Socket socket, AsyncCallback callback) { if(socket == null) throw new ArgumentNullException("socket"); return socket.BeginAccept(callback, null); }
/// <summary> /// BeginAccept /// </summary> /// <param name="socket"></param> /// <param name="callback"></param> /// <returns></returns> public static IAsyncResult BeginAccept(this Socket socket, AsyncCallback callback) => socket.BeginAccept(callback, null);