我已经知道.NetFramework中的BeginXXX(AMP)或XXXAsync(TAP)使用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的操作系统中:
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