关于 ResetEvents 和 Threading,此 tcp 侦听器代码是否正确实施?

Is this tcp listener code implemented correctly regarding ResetEvents and Threading?

我觉得这段代码没有发挥应有的作用。我把它拼凑在一起并且可以工作,但我不完全理解异步线程和 ResetEvents。

当我使用发送 100 个连接的多线程客户端测试此侦听器时,响应时间从前 10 次左右的 300 毫秒增加到最后 50 次的 2-3 秒。

1.) 线程和重置事件处理程序是否按设计实现?

2.) 有什么方法可以加快高流量突发的响应时间吗?

3.) 运行 另一个线程上的 tcpConnectionLogic 有意义吗?

我有一个全局范围内的手动和自动重置事件:

   private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
   private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);

我有一个 windows 服务,它在新线程上启动一个 tcp 侦听器

protected override void OnStart(string[] args)
{
    _mainThread = new Thread(ThreadListener_Begin);

    _mainThread.Name = "EMServiceThread";
    _mainThread.IsBackground = false;
    _mainThread.Start();
}

我的线程 运行 是一个等待手动重新发送事件以发出关闭信号的循环。

private void ThreadListener_Begin()
{
    TcpListener listener = null;
    listener = new TcpListener(IPAddress.Parse("172.16.30.248"), 10010);

    listener.ExclusiveAddressUse = false;
    listener.Start();

        while (!_shutdownEvent.WaitOne(0))
        {
            IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
            connectionWaitHandle.WaitOne();
        }
}

最后,当一个连接进来时,我接受它并将它传递给处理请求的一种逻辑层。

private void HandleAsyncConnection(IAsyncResult result)
{ 
  TcpListener listener = (TcpListener)result.AsyncState;

  connectionWaitHandle.Set();

  TcpClient c = listener.EndAcceptTcpClient(result);

  var _tcpConnectedLogic = new TcpConnectionLogic(c);

  _tcpConnectedLogic.BadRequestAlert += _serviceLogic_BadRequestAlert;
  _tcpConnectedLogic.RequestDecodedAlert += _tcpConnectedLogic_RequestDecodedAlert;
  _tcpConnectedLogic.Start();

    }

编辑 当我停止服务时,我的线程并没有像我预期的那样关闭。相反,它最终会中止。为什么?我怎样才能让它优雅地关闭?

protected override void OnStop()
{
    WriteToEventLog("Stop Command Received" + CurrentTimeStamp, entryType: EventLogEntryType.Information);
    WriteToLogFile("Stop Command Received @ " + CurrentTimeStamp);
    _shutdownEvent.Set();

    if (!_mainThread.Join(4000))
    {
        WriteToEventLog("OnStop: Aborting thread, Join did not work.  This is not preferred.", entryType: EventLogEntryType.Warning);
        _mainThread.Abort();
    }
}

在这个街区

while (!_shutdownEvent.WaitOne(0))
{
    IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
    connectionWaitHandle.WaitOne();
}

您基本上是在循环中接受客户端,因为您通过等待 connectionWaitHandle 来阻塞线程。你也可以忘记异步的东西,只做:

while (!_shutdownEvent.WaitOne(0))
{                
     var client = listener.AcceptTcpClient();
     // start your logic in separate thread                
}

我并不是说您应该这样做,只是请注意,您当前的 "async" 实施不会比同步实施增加任何好处。

然后,由于 connectionWaitHandle 上的相同阻塞,您遇到了中止线程的问题。假设例如你刚刚启动你的监听器并且没有客户端连接过。然后客户端在 connectionWaitHandle 上被阻塞,而您设置 _shutdownEvent 的事实也无济于事 - 主线程将永远没有机会检查它。

修复它的最佳选择是使用基于任务的方法:

class Listener {
    // we don't use cancellation tokens because AcceptTcpClientAsync does not support them anyway
    private volatile bool _shutdown;
    private TcpListener _listener;

    public async Task Start() {
        _listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 10010);
        _listener.ExclusiveAddressUse = false;
        _listener.Start();
        while (!_shutdown) {
            TcpClient client = null;
            try {
                client = await _listener.AcceptTcpClientAsync();
            }
            catch (ObjectDisposedException) {
                // will be thrown when you stop listener   
            }
            if (client != null) {
                var tcpConnectedLogic = new TcpConnectionLogic(client);
                // start processing on thread pool thread if necessary
                // don't wait for it to finish - you need to accept more connections
                tcpConnectedLogic.StartAsync();
            }
        }
    }

    public void Stop() {
        _shutdown = true;
        _listener.Stop();
    }
}