private static void AssertEqual(ExpectedAsyncResult expected, IAsyncResult actual, bool disposeActual = false) { Assert.NotNull(actual); Assert.Same(expected.AsyncState, actual.AsyncState); Assert.Equal(expected.CompletedSynchronously, actual.CompletedSynchronously); Assert.Equal(expected.IsCompleted, actual.IsCompleted); try { Assert.Equal(expected.IsCompleted, actual.AsyncWaitHandle.WaitOne(0)); } finally { if (disposeActual) { actual.Dispose(); } } }
private void EndAccept(IAsyncResult result) #endif { Socket peerSocket = null; try { #if NETSTANDARD1_5 Socket listener = listenerSocket; //(Socket)result.AsyncState; peerSocket = result; //listener.EndAccept(result); #else Socket listener = (Socket)result.AsyncState; peerSocket = listener.EndAccept(result); #endif IPEndPoint endpoint = (IPEndPoint)peerSocket.RemoteEndPoint; Uri uri = new Uri("tcp://" + endpoint.Address.ToString() + ':' + endpoint.Port); Peer peer = new Peer("", uri, EncryptionTypes.All); IConnection connection = null; if (peerSocket.AddressFamily == AddressFamily.InterNetwork) { connection = new IPV4Connection(peerSocket, true); } else { connection = new IPV6Connection(peerSocket, true); } RaiseConnectionReceived(peer, connection, null); } catch (SocketException) { // Just dump the connection if (peerSocket != null) #if NETSTANDARD1_5 { peerSocket.Dispose(); } #else { peerSocket.Close(); } #endif } catch (ObjectDisposedException) { // We've stopped listening } finally { try { if (Status == ListenerStatus.Listening) { #if NETSTANDARD1_5 var r = await listener.AcceptAsync(); EndAccept(r, listener); #else listener.BeginAccept(endAcceptCallback, listener); #endif } } catch (ObjectDisposedException) { } } }