C
C#3y ago
Stay

❔ Reducing networking syscalls

Hi. I'm doing a TCP proxy which receives packets from a client and forwards them to another server (and vice versa). I'm seeing "high" CPU usage when more than 100+ clients are connected and sending/receiving data. VS2022 shows that 50% of the application CPU usage goes into NetworkStream#WriteAsync. I've been thinking of how to reduce the number of calls to this method, but i can't come with a solution that won't add significant latency. It is important to note that the client and server are not sending a continuous stream of data, but messages that can come and go at any time, so a buffered stream is not an option, atleast without a timeout. At the moment this is the only solution that comes to my mind: Some ticking / clock system For example, when calling the SendPacket method instead of instantly writing the packet to the stream saving it in a list, and every, let's say 10 milliseconds, send all the packets stored in that list. But this will add at least 10 milliseconds of latency, which is not great but also not awful So... what is the best way of solving this? I don't think that this is solvable without adding at least a bit of latency
2 Replies
Akseli
Akseli3y ago
use SocketAsyncEventArgs for sending data also prefer using the ValueTask methods if using any #Async methods, the ones that accept a Memory<byte> are ValueTask
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server