DotnetCore 1.0.x中的TcpListener.BeginAcceptTcpClient I / O模型是什么

时间:2016-10-12 07:43:18

标签: c# .net open-source .net-framework-version

我已经知道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);

    }

1 个答案:

答案 0 :(得分:0)

我刚刚解决了。

在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