terom@380: #ifndef NETWORK_REACTOR_HH terom@380: #define NETWORK_REACTOR_HH terom@380: terom@380: // forward-declare terom@380: class NetworkReactor; terom@380: terom@380: /** terom@380: * Events to poll for terom@380: */ terom@380: enum NetworkPollBit { terom@380: POLL_READ = 0x01, terom@380: POLL_WRITE = 0x02, terom@380: }; terom@380: terom@380: /** terom@380: * Poll event bitmask of NetworkPollBit's terom@380: */ terom@380: typedef int NetworkPollMask; terom@380: terom@380: #include "Socket.hh" terom@380: #include "Error.hh" terom@380: terom@380: /* terom@380: * Platform-specific includes terom@380: */ terom@380: #ifndef WIN32 terom@380: // linux terom@380: #include terom@380: #else terom@380: #error "This network code won't compile on win32 :)" terom@380: #endif terom@380: terom@380: #include terom@380: terom@380: /** terom@380: * A Reactor manages a set of NetworkSockets, providing readyness notification and a poll method terom@380: */ terom@380: class NetworkReactor { terom@380: protected: terom@380: std::list sockets; terom@380: terom@380: public: terom@380: /** terom@380: * Construct the empty reactor terom@380: */ terom@380: NetworkReactor (void); terom@380: terom@380: /** terom@380: * Our static global reactor terom@380: */ terom@380: static NetworkReactor *current; terom@380: terom@380: /** terom@380: * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the terom@380: * socket itself. terom@380: * terom@380: * @param socket the socket to watch terom@380: */ terom@380: void add_socket (NetworkSocket *socket) { sockets.push_back(socket); } terom@380: terom@380: /** terom@380: * Remove a NetworkSocket from our list of sockets. terom@380: * terom@380: * @param socket the socket to stop watching terom@380: */ terom@380: void remove_socket (NetworkSocket *socket) { sockets.remove(socket); } terom@380: terom@380: /** terom@380: * Poll our sockets and drive any I/O, optionally sleeping for the given timeout. This is efficient if our terom@380: * sockets list is empty. terom@380: */ terom@380: void poll (timeval *timeout = NULL); terom@380: }; terom@380: terom@380: /** terom@380: * Reactor error terom@380: */ terom@380: class NetworkReactorError : public NetworkErrno { terom@380: public: terom@380: NetworkReactorError (std::string op) : NetworkErrno(op) { } terom@380: }; terom@380: terom@380: #endif