From 7d2c493bf4f2863d1b9aab1978ec7c14926f28a1 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 24 May 2021 21:42:06 -0400 Subject: [PATCH] Delete now unused *AsyncResult files from Sockets (#53192) --- .../Net/Sockets/BaseOverlappedAsyncResult.Unix.cs | 31 ---- .../Sockets/BaseOverlappedAsyncResult.Windows.cs | 200 --------------------- .../Net/Sockets/BaseOverlappedAsyncResult.cs | 25 --- 3 files changed, 256 deletions(-) delete mode 100644 src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Unix.cs delete mode 100644 src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs delete mode 100644 src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.cs diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Unix.cs deleted file mode 100644 index 5864b56..0000000 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Unix.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Diagnostics; -using System.Net; -using System.Runtime.InteropServices; -using System.Threading; -using Microsoft.Win32; - -namespace System.Net.Sockets -{ - // BaseOverlappedAsyncResult - // - // This class is used to track state for async Socket operations such as the BeginSend, BeginSendTo, - // BeginReceive, BeginReceiveFrom, BeginSendFile, and BeginAccept calls. - internal partial class BaseOverlappedAsyncResult : ContextAwareResult - { - public BaseOverlappedAsyncResult(Socket socket, object? asyncState, AsyncCallback? asyncCallback) - : base(socket, asyncState, asyncCallback) - { - if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, socket); - } - - protected void CompletionCallback(int numBytes, SocketError errorCode) - { - ErrorCode = (int)errorCode; - InvokeCallback(PostCompletion(numBytes)); - } - } -} diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs deleted file mode 100644 index 14e2b0e..0000000 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs +++ /dev/null @@ -1,200 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics; -using System.Threading; - -namespace System.Net.Sockets -{ - // BaseOverlappedAsyncResult - // - // This class is used to track state for async Socket operations such as the BeginSend, BeginSendTo, - // BeginReceive, BeginReceiveFrom, BeginSendFile, and BeginAccept calls. - internal partial class BaseOverlappedAsyncResult : ContextAwareResult - { - private int _cleanupCount; - private SafeNativeOverlapped? _nativeOverlapped; - - // The WinNT Completion Port callback. - private static readonly unsafe IOCompletionCallback s_ioCallback = new IOCompletionCallback(CompletionPortCallback); - - internal BaseOverlappedAsyncResult(Socket socket, object? asyncState, AsyncCallback? asyncCallback) - : base(socket, asyncState, asyncCallback) - { - _cleanupCount = 1; - if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, socket); - } - - // SetUnmanagedStructures - // - // This needs to be called for overlapped IO to function properly. - // - // Fills in overlapped Structures used in an async overlapped Winsock call. - // These calls are outside the runtime and are unmanaged code, so we need - // to prepare specific structures and ints that lie in unmanaged memory - // since the overlapped calls may complete asynchronously. - internal void SetUnmanagedStructures(object? objectsToPin) - { - Socket s = (Socket)AsyncObject!; - - // Bind the Win32 Socket Handle to the ThreadPool - Debug.Assert(s != null, "m_CurrentSocket is null"); - Debug.Assert(s.SafeHandle != null, "m_CurrentSocket.SafeHandle is null"); - - if (s.SafeHandle.IsInvalid) - { - throw new ObjectDisposedException(s.GetType().FullName); - } - - ThreadPoolBoundHandle boundHandle = s.GetOrAllocateThreadPoolBoundHandle(); - - unsafe - { - Debug.Assert(OperatingSystem.IsWindows()); - NativeOverlapped* overlapped = boundHandle.AllocateNativeOverlapped(s_ioCallback, this, objectsToPin); - _nativeOverlapped = new SafeNativeOverlapped(s.SafeHandle, overlapped); - } - - if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"{boundHandle}::AllocateNativeOverlapped. return={_nativeOverlapped}"); - } - - private static unsafe void CompletionPortCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) - { - Debug.Assert(OperatingSystem.IsWindows()); - BaseOverlappedAsyncResult asyncResult = (BaseOverlappedAsyncResult)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!; - - Debug.Assert(!asyncResult.InternalPeekCompleted, $"asyncResult.IsCompleted: {asyncResult}"); - if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"errorCode:{errorCode} numBytes:{numBytes} nativeOverlapped:{(IntPtr)nativeOverlapped}"); - - // Complete the IO and invoke the user's callback. - SocketError socketError = (SocketError)errorCode; - - if (socketError != SocketError.Success && socketError != SocketError.OperationAborted) - { - // There are cases where passed errorCode does not reflect the details of the underlined socket error. - // "So as of today, the key is the difference between WSAECONNRESET and ConnectionAborted, - // .e.g remote party or network causing the connection reset or something on the local host (e.g. closesocket - // or receiving data after shutdown (SD_RECV)). With Winsock/TCP stack rewrite in longhorn, there may - // be other differences as well." - - Socket? socket = asyncResult.AsyncObject as Socket; - if (socket == null) - { - socketError = SocketError.NotSocket; - } - else if (socket.Disposed) - { - socketError = SocketError.OperationAborted; - } - else - { - try - { - // The async IO completed with a failure. - // Here we need to call WSAGetOverlappedResult() just so GetLastSocketError() will return the correct error. - SocketFlags ignore; - bool success = Interop.Winsock.WSAGetOverlappedResult( - socket.SafeHandle, - nativeOverlapped, - out numBytes, - false, - out ignore); - Debug.Assert(!success, $"Unexpectedly succeeded. errorCode:{errorCode} numBytes:{numBytes}"); - if (!success) - { - socketError = SocketPal.GetLastSocketError(); - } - } - catch (ObjectDisposedException) - { - // Disposed check above does not always work since this code is subject to race conditions - socketError = SocketError.OperationAborted; - } - } - } - - // Set results and invoke callback - asyncResult.CompletionCallback((int)numBytes, socketError); - } - - // Called either synchronously from SocketPal async routines or asynchronously via CompletionPortCallback above. - private void CompletionCallback(int numBytes, SocketError socketError) - { - ErrorCode = (int)socketError; - object? result = PostCompletion(numBytes); - ReleaseUnmanagedStructures(); // must come after PostCompletion, as overrides may use these resources - InvokeCallback(result); - } - - internal unsafe NativeOverlapped* DangerousOverlappedPointer => (NativeOverlapped*)_nativeOverlapped!.DangerousGetHandle(); - - // Check the result of the overlapped operation. - // Handle synchronous success by completing the asyncResult here. - // Handle synchronous failure by cleaning up and returning a SocketError. - internal SocketError ProcessOverlappedResult(bool success, int bytesTransferred) - { - if (success) - { - // Synchronous success. - Socket socket = (Socket)AsyncObject!; - if (socket.SafeHandle.SkipCompletionPortOnSuccess) - { - // The socket handle is configured to skip completion on success, - // so we can complete this asyncResult right now. - CompletionCallback(bytesTransferred, SocketError.Success); - return SocketError.Success; - } - - // Socket handle is going to post a completion to the completion port (may have done so already). - // Return pending and we will continue in the completion port callback. - return SocketError.IOPending; - } - - // Get the socket error (which may be IOPending) - SocketError errorCode = SocketPal.GetLastSocketError(); - - if (errorCode == SocketError.IOPending) - { - // Operation is pending. - // We will continue when the completion arrives (may have already at this point). - return SocketError.IOPending; - } - - // Synchronous failure. - // Release overlapped and pinned structures. - ReleaseUnmanagedStructures(); - - return errorCode; - } - - internal void ReleaseUnmanagedStructures() - { - if (Interlocked.Decrement(ref _cleanupCount) == 0) - { - ForceReleaseUnmanagedStructures(); - } - } - - protected override void Cleanup() - { - base.Cleanup(); - - // If we get all the way to here and it's still not cleaned up... - if (_cleanupCount > 0 && Interlocked.Exchange(ref _cleanupCount, 0) > 0) - { - ForceReleaseUnmanagedStructures(); - } - } - - // Utility cleanup routine. Frees the overlapped structure. - // This should be overridden to free pinned and unmanaged memory in the subclass. - // It needs to also be invoked from the subclass. - protected virtual void ForceReleaseUnmanagedStructures() - { - // Free the unmanaged memory if allocated. - _nativeOverlapped!.Dispose(); - _nativeOverlapped = null; - GC.SuppressFinalize(this); - } - } -} diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.cs deleted file mode 100644 index a60cbb9..0000000 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Net.Sockets -{ - // BaseOverlappedAsyncResult - // - // This class is used to track state for async Socket operations such as the BeginSend, BeginSendTo, - // BeginReceive, BeginReceiveFrom, BeginSendFile, and BeginAccept calls. - internal partial class BaseOverlappedAsyncResult : ContextAwareResult - { - // Sentinel object passed to callers of PostCompletion to use as the - // "result" of this operation, in order to avoid boxing the actual result. - private static readonly object s_resultObjectSentinel = new object(); - // The actual result (number of bytes transferred) - internal int _numBytes; - - // PostCompletion returns the result object to be set before the user's callback is invoked. - internal virtual object? PostCompletion(int numBytes) - { - _numBytes = numBytes; - return s_resultObjectSentinel; // return sentinel rather than boxing numBytes - } - } -} -- 2.7.4