TcpListener vs SocketAsyncEventArgs

前端 未结 1 1169
甜味超标
甜味超标 2021-01-13 07:16

Is there a valid reason to not use TcpListener for implementing a high performance/high throughput TCP server instead of SocketAsyncEventArgs

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 07:40

    I see no evidence that this question is about TcpListener at all. It seems you are only concerned with the code that deals with a connection that already has been accepted. Such a connection is independent of the listener.

    SocketAsyncEventArgs is a CPU-load optimization. I'm convinced you can achieve a higher rate of operations per second with it. How significant is the difference to normal APM/TAP async IO? Certainly less than an order of magnitude. Probably between 1.2x and 3x. Last time I benchmarked loopback TCP transaction rate I found that the kernel took about half of the CPU usage. That means your app can get at most 2x faster by being infinitely optimized.

    Remember that SocketAsyncEventArgs was added to the BCL in the year 2000 or so when CPUs were far less capable.

    Use SocketAsyncEventArgs only when you have evidence that you need it. It causes you to be far less productive. More potential for bugs.

    Here's the template that your socket processing loop should look like:

    while (ConnectionEstablished()) {
     var someData = await ReadFromSocketAsync(socket);
     await ProcessDataAsync(someData);
    }
    

    Very simple code. No callbacks thanks to await.


    In case you are concerned about managed heap fragmentation: Allocate a new byte[1024 * 1024] on startup. When you want to read from a socket read a single byte into some free portion of this buffer. When that single-byte read completes you ask how many bytes are actually there (Socket.Available) and synchronously pull the rest. That way you only pin a single rather small buffer and still can use async IO to wait for data to arrive.

    This technique does not require polling. Since Socket.Available can only increase without reading from the socket we do not risk to perform a read that is too small accidentally.

    Alternatively, you can combat managed heap fragmentation by allocating few very big buffers and handing out chunks.

    Or, if you don't find this to be a problem you don't need to do anything.

    0 讨论(0)
提交回复
热议问题