#ifndef __PollSet_h_ #define __PollSet_h_ #include #include "MiscSupport.h" /* * This is a wrapper around the poll() system call to make it a little * easier to use. Some items are wrapped in STL data structures. This * also adds common error checking code, which will send messages to the * log and abort as required. * * We use poll() instead of select() because select() only works on a fixed * number of sockets. Once the program creates more than 1023 file handles, * select() will cause problems. * * Originally all of the code used select(). These calls were replaced * with poll(), however the word "select" still appears in a few comments. * Also, the functions provided by the TimeVal class would be a lot different * if we had started with poll() from the beginning. * * Note that poll() is only precise to the millisecond, while select() * is more precise, and pselect() is even more precise. This doesn't matter * in the least, because on Linux all three are only accurate to the * millisecond. * * Note: If you are going to keep listening to the same group of handles * over and over (even if you make small changes to that group from time to * time) you should look at IPollSet. PollSet is optimized to be fast when * you are starting from scratch each time. IPollSet is optimized for the * case of making small modifications each time. */ class PollSet { public: typedef std::set< int >HandleSet; private: int _timeout; HandleSet _toPollRead, _toPollWrite; HandleSet _woken; public: PollSet(); // Defaults to none. void setTimeoutNone(); void setTimeoutMs(int timeout); // If the pointer is null, then we disable the timeout. Otherwise we // interpret this like a select call would, with the number of seconds // and microseconds to wait. void setTimeout(timeval *timeVal); // Defaults to empty. Is only reset when you reset it. void addForRead(int fd); void removeForRead(int fd); void addForWrite(int fd); void removeForWrite(int fd); void clearFileDescriptors(); // This clears the needAttention set, then fills it based on the results of // the poll. Fatal errors are written to the log, then we abort. Non fatal // errors are ignored. This can possible return with nothing in the // needAttention set. This could be a timeout or a signal. void poll(); // These are the file handles which were ready after the most recent call to // poll. HandleSet const &woken() const; }; #endif