DotnetCore 1 中的 TcpListener.BeginAcceptTcpClient I/O 模型是什么0.x
What is the TcpListener.BeginAcceptTcpClient I/O Model in DotnetCore 1.0.x
我已经知道 BeginXXX(AMP) 或 XXXAsync(TAP) 在 .NetFramework 中使用 IOCP,那么现在我想在 dotnetcore 上构建 httpServer。所以我需要知道内部机制。
我之前在.NetworkFramework 中的版本如下代码:
private void continueAccept(TcpListener tcpListener,Action<TcpClient> processConnection)
{
//IOCP
tcpListener.BeginAcceptTcpClient(ar =>
{
if (listening)
continueAccept(tcpListener, processConnection);
else
return;
try
{
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
acceptCount++;
tcpClient.SendTimeout = 300000;
tcpClient.ReceiveTimeout = 300000;
ConsoleHost1.trace(System.Diagnostics.TraceEventType.Verbose, $"Client Accept { tcpClient.Client.RemoteEndPoint.ToString()}");
ThreadPool.QueueUserWorkItem((o) => {
processConnection((TcpClient)o);
}, tcpClient);
}
catch (Exception e)
{
ConsoleHost1.trace(System.Diagnostics.TraceEventType.Error, $"acceptTD:{e.Message}");
}
}, null);
}
public void startListen(Action<TcpClient> processConnection)
{
tcpListener = new TcpListener(IPAddress.Parse("0.0.0.0"), port1);
tcpListener.Start(maxQueue1);
listening = true;
continueAccept(tcpListener, processConnection);
}
我刚刚解决了。
在windows中:
AcceptAsync 使用 AcceptEx+SocketAsyncEventArgs(IOCP)
BeginAccept 使用 AcceptEx+SafeNativeOverlapped(IOCP)
参考资料来自:
https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Windows.cs
https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs
类 Unix OS:
AcceptAsync 或 AcceptAsync 在用户模式下使用 Interop.Sys.Accept + ThreadPool。
参考资料来自:
https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs
https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs
我已经知道 BeginXXX(AMP) 或 XXXAsync(TAP) 在 .NetFramework 中使用 IOCP,那么现在我想在 dotnetcore 上构建 httpServer。所以我需要知道内部机制。
我之前在.NetworkFramework 中的版本如下代码:
private void continueAccept(TcpListener tcpListener,Action<TcpClient> processConnection)
{
//IOCP
tcpListener.BeginAcceptTcpClient(ar =>
{
if (listening)
continueAccept(tcpListener, processConnection);
else
return;
try
{
TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
acceptCount++;
tcpClient.SendTimeout = 300000;
tcpClient.ReceiveTimeout = 300000;
ConsoleHost1.trace(System.Diagnostics.TraceEventType.Verbose, $"Client Accept { tcpClient.Client.RemoteEndPoint.ToString()}");
ThreadPool.QueueUserWorkItem((o) => {
processConnection((TcpClient)o);
}, tcpClient);
}
catch (Exception e)
{
ConsoleHost1.trace(System.Diagnostics.TraceEventType.Error, $"acceptTD:{e.Message}");
}
}, null);
}
public void startListen(Action<TcpClient> processConnection)
{
tcpListener = new TcpListener(IPAddress.Parse("0.0.0.0"), port1);
tcpListener.Start(maxQueue1);
listening = true;
continueAccept(tcpListener, processConnection);
}
我刚刚解决了。
在windows中:
AcceptAsync 使用 AcceptEx+SocketAsyncEventArgs(IOCP)
BeginAccept 使用 AcceptEx+SafeNativeOverlapped(IOCP)
参考资料来自: https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Windows.cs https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs
类 Unix OS:
AcceptAsync 或 AcceptAsync 在用户模式下使用 Interop.Sys.Accept + ThreadPool。
参考资料来自: https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs https://github.com/dotnet/corefx/blob/release/1.0.0/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs