Skip to content

Commit

Permalink
map TcpListener APM methods to Task-based methods (#40476)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoffrey Kizer <geoffrek@windows.microsoft.com>
  • Loading branch information
geoffkizer and Geoffrey Kizer authored Aug 6, 2020
1 parent 8978800 commit 2021d54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -215,8 +217,6 @@
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,40 +207,17 @@ public TcpClient AcceptTcpClient()
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()
{
Expand Down

0 comments on commit 2021d54

Please sign in to comment.