map TcpListener APM methods to Task-based methods (#40476)
authorGeoff Kizer <geoffrek@microsoft.com>
Thu, 6 Aug 2020 19:20:25 +0000 (12:20 -0700)
committerGitHub <noreply@github.com>
Thu, 6 Aug 2020 19:20:25 +0000 (12:20 -0700)
Co-authored-by: Geoffrey Kizer <geoffrek@windows.microsoft.com>
src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs

index 3d4d3c7..ae0f96d 100644 (file)
@@ -80,6 +80,8 @@
              Link="Common\System\Net\SocketAddress.cs" />
     <Compile Include="$(CommonPath)System\Net\TcpValidationHelpers.cs"
              Link="Common\System\Net\TcpValidationHelpers.cs" />
+    <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
+             Link="Common\System\Threading\Tasks\TaskToApm.cs" />
     <!-- System.Net.Internals -->
     <Compile Include="$(CommonPath)System\Net\Internals\IPEndPointExtensions.cs"
              Link="Common\System\Net\Internals\IPEndPointExtensions.cs" />
              Link="Common\System\Net\SocketProtocolSupportPal.Unix" />
     <Compile Include="$(CommonPath)System\Net\Sockets\SocketErrorPal.Unix.cs"
              Link="Common\System\Net\Sockets\SocketErrorPal.Unix" />
-    <Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
-             Link="Common\System\Threading\Tasks\TaskToApm.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
              Link="Common\Interop\Unix\Interop.Errors.cs" />
     <Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
index adb1c3f..6c623fe 100644 (file)
@@ -207,40 +207,17 @@ namespace System.Net.Sockets
             return new TcpClient(acceptedSocket);
         }
 
-        public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state)
-        {
-
-            if (!_active)
-            {
-                throw new InvalidOperationException(SR.net_stopped);
-            }
-
-            return _serverSocket!.BeginAccept(callback, state);
-        }
+        public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state) =>
+            TaskToApm.Begin(AcceptSocketAsync(), callback, state);
 
-        public Socket EndAcceptSocket(IAsyncResult asyncResult)
-        {
-            if (asyncResult == null)
-            {
-                throw new ArgumentNullException(nameof(asyncResult));
-            }
-
-            LazyAsyncResult? lazyResult = asyncResult as LazyAsyncResult;
-            Socket? asyncSocket = lazyResult == null ? null : lazyResult.AsyncObject as Socket;
-            if (asyncSocket == null)
-            {
-                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
-            }
-
-            // This will throw ObjectDisposedException if Stop() has been called.
-            return asyncSocket.EndAccept(asyncResult);
-        }
+        public Socket EndAcceptSocket(IAsyncResult asyncResult) =>
+            TaskToApm.End<Socket>(asyncResult);
 
         public IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state) =>
-            BeginAcceptSocket(callback, state);
+            TaskToApm.Begin(AcceptTcpClientAsync(), callback, state);
 
         public TcpClient EndAcceptTcpClient(IAsyncResult asyncResult) =>
-            new TcpClient(EndAcceptSocket(asyncResult));
+            TaskToApm.End<TcpClient>(asyncResult);
 
         public Task<Socket> AcceptSocketAsync()
         {