Alright, I found out in this question that polling sockets does not scale, so I’ve decided to look into asynchronous sockets, and I have several questions.
- If I have several hundred clients all trying to send data to their partner, what would be the best async method to use? select() poll() or can I just call recv() on a non-blocking socket?
- When I poll and find there is data to read, should I spawn a thread to take care of it?
- Should I worry about any sleep function, or should I just let the program take 100% CPU?
- Would it be efficient at all to put this whole functionality into a class? I would really like to do something like this:
//thread 1:
while(!quit){
manager.check_clients();
manager.handle_clients();
manager.display_clients();
}
//thread 2:
while(!quit)
manager.manage_admin_input();
The choice of polling method is OS dependent. On linux, use epoll, preferrably edge-triggered. On FreeBSD, use kqueue. On Windows, use e.g. WSAEventSelect and WSAWaitForMultipleEvents.
Your main loop should simply be:
Whether you choose to implement this in each thread in a thread pool, or just once in the main thread, is dependent on the rest of your application. The key is to let the polling function do the wait, this way you won’t use excessive CPU.
You can either use non-blocking sockets, or
ioctl(FIONREAD...to check how much data is readable on each socket.My preferred OOP design for socket handling is to make a socket completely unaware of the socket poller. A socket poller, hiding the actual function used for polling, would accept sockets and the events it should watch for, do it’s polling in e.g. a
tick()function, then tell each socket or an external listening class that there’s stuff to do with the socket. Something like: