lsalzman / enet

ENet reliable UDP networking library
MIT License
2.66k stars 667 forks source link

enet_host_service performance #223

Open DevilLord41 opened 1 year ago

DevilLord41 commented 1 year ago

Hi, I have enet_host_service with timeout 0ms on client and it seems eating my cpu alot compared to using 10ms. while (enet_host_service(client, &event, 0) > 0) { using 10ms seems work, but not always because sometimes my client cant send packet anymore but still able to receive packet.

mman commented 1 year ago

@DevilLord41 actively polling socket with 0 timeout like this will always consume lot of CPU. It's best to combine with some higher level routines like select(2) or poll(2) so that you only call enet_host_service when you know the the underlying socket is readable - some data are waiting in the kernel to be read, or after you generate a packet to be sent, and eventually re-sent.

Logic I have come up with calls enet_host_service periodically to make sure outgoing packets, and pings are sent and re-sent as needed and fast enough, say 10 times per second, and then I call it immediately I am notified when data is available.

There is unfortunately no magic here, you have to do the plumbing...