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:
for (;;) {
epoll(); // blocking poll until an event happens, optionally with a timeout
// iterate signaled sockets and process data
// Other tasks
}
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:
class SocketPoller {
public:
void registerSocket(Socket * s, int EventMask);
void unregisterSocket(Socket * s);
virtual void tick() = 0;
}
class SocketPollerEPoll : public SocketPoller {
public:
void tick() {
epoll(...);
// for each socket with events:
TheSocket->notifyReadable();
}
};
class SocketPollerSelect : public SocketPoller {
public:
void tick() {
select(...);
// for each socket with events:
TheSocket->notifyReadable();
}
};
Even though this question has already been answered, you might consider using Boost.Asio for your communication framework. It nicely wrappers the various polling methods on a variety of platforms into a clean, consistent, and type-safe header only library. It is mature, well supported, and discussed on SO frequently.
You can use Push framework, http://www.pushframework.com It uses asynchronous IOs efficiently. Also would abstract the low-level details so you can concentrate on the business logic of your app.
精彩评论