简体   繁体   English

具有基于任务的异步模式的UDP侦听器

[英]UDP listener with task-based asynchronous pattern

I am messing around with a UDP listener and the TAP. 我在弄乱UDP侦听器和TAP。

I have a class that is responsible for listening for incoming UDP and that can be started and stopped. 我有一类负责监听传入的UDP,并且可以启动和停止。 The Start() method simply calls the following method Start()方法仅调用以下方法

    private async void Listen()
    {
        var resp = await _udpClient.ReceiveAsync().ConfigureAwait(continueOnCapturedContext: false);

        var eventHandler = PacketReceived;
        if (eventHandler != null)
            eventHandler(this, new UdpPacketReceivedEventArgs(resp));

        if (_running)
            Listen();
    }

Is this correct? 这个对吗? I am a bit worried that I might exhaust the thread pool, because the thread is being kept alive by spawning a new Listen() call. 我有点担心我可能耗尽线程池,因为通过产生新的Listen()调用使线程保持活动状态。

Is this correct? 这个对吗? I am a bit worried that I might exhaust the thread pool, because the thread is being kept alive by spawning a new Listen() call. 我有点担心我可能耗尽线程池,因为通过产生新的Listen()调用使线程保持活动状态。

Yes, it's totally correct, this operation is not CPU bound, it's IO bound. 是的, 完全正确,此操作不受CPU限制,受IO限制。 So, there are no threads in thread pool at all that are blocked waiting for the operation to complete. 因此,线程池中根本没有任何线程被阻塞以等待操作完成。

There is a thread waiting for network requests to complete, but it is shared between all network requests (like UI thread). 有一个线程等待网络请求完成,但是它在所有网络请求之间共享(例如UI线程)。 It's called the IO completion port thread on Windows. 在Windows上称为IO完成端口线程。 When the network request completes, an interrupt handler in the operating system adds a job to a queue for the IO completion port. 网络请求完成后,操作系统中的中断处理程序会将作业添加到IO完成端口的队列中。 To perform 1000 network requests, the requests are all started, and as the responses arrive, they are processed in turn by the single IO completion port. 为了执行1000个网络请求,所有请求都将启动,并且随着响应到达,它们将由单个IO完成端口依次处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM