I already know the BeginXXX(AMP) or XXXAsync(TAP) use IOCP in .NetFramework , then now I want to construct httpServer build on dotnetcore . so I need to know the inner mechanism.
My pervious version in .NetworkFramework like the follow code:
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);
}
I have just resolved.
In windows:
AcceptAsync use the AcceptEx+SocketAsyncEventArgs(IOCP)
BeginAccept use the AcceptEx+SafeNativeOverlapped(IOCP)
Reference from: 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
In Unix-like OS:
AcceptAsync Or AcceptAsync use Interop.Sys.Accept + ThreadPool in user mode.
Reference from: 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