1 #ifndef SCD_SOCK_POLLER_H
2 #define SCD_SOCK_POLLER_H
7 #include "scd_socket.h"
10 typedef short sock_ev;
11 const sock_ev SOCK_EV_READ = POLLIN;
12 const sock_ev SOCK_EV_WRITE = POLLOUT;
13 const sock_ev SOCK_EV_CLOSE = POLLERR | POLLHUP | POLLNVAL;
17 * Interface for classes able to handle socket events.
19 class scd_sock_ev_handler_if
23 * Executes the handler for socket events that have been registered
24 * and have ocured. Callback carried out by scd_sock_poller.
25 *\param events the events that occured
27 virtual void handle_sock_ev(sock_ev events) = 0;
30 * Returns the socket that should be watched. The socket must
31 * be the same as long the handler is registered.
33 virtual const scd_socket &get_sock() = 0;
35 virtual ~scd_sock_ev_handler_if() {};
40 * Dispatcher that watches sockets and executes call backs if watched
41 * event occure. Socket events (sock_ev) are an OR combination of flags
42 * supported by ::poll. See "man 2 poll". The watched events for a specific
43 * event handler stay watched until the events are overwritten or the handler
50 * Register a socket event handler (a class handling socket events).
51 * Optionally the events to watch can be specified.
52 * \param handler the object that wants to watch a socket
53 * \param events (optional) the events to watch
54 * \return true if the handler could be registered succesfully
56 bool register_handler(scd_sock_ev_handler_if &handler, sock_ev events = 0);
59 * Remove a socket event handler. The socket is not watched anymore.
60 * \param the object that has previously been registered
61 * \return false if no such handler was registered
63 bool remove_handler(const scd_sock_ev_handler_if &handler);
66 * Set events to watch. The events expire only when overwritten
67 * by calling this function again or by removing the handler.
68 * \param handler the handler that has been registered before
69 * \param events the events the handler is interested in
70 * \return false if the handler has not been registered before
72 bool set_ev(const scd_sock_ev_handler_if &handler, sock_ev events);
75 * Gets the events that are currently watched for the handler.
76 * \param the handler to get the watched events for
77 * \param events the variable where the watched events are written to
78 * \return false if the handler has not been registered before
80 bool get_ev(const scd_sock_ev_handler_if &handler, sock_ev &events) const;
83 * Wait (blocking) for registered events or a timeout to occure.
84 * No callbacks are executed.
85 * \param milis timeout in milliseconds to wait for events, if omitted
86 * or -1 it is waited until events occure
87 * \return true if events occured, false otherwise
88 * \exception scd_exception if unexpected errors occured
90 bool wait(int ms = -1);
93 * Check if events occured and callback the affected handlers.
94 * \return true if some events occured
95 * \exception scd_exception if unexcpected errors occured
100 * Wait (blocking) for registered events to occure and callback the
102 * \exception scd_exception if unexcpected errors occured
107 /* member variables */
108 std::vector<scd_sock_ev_handler_if *> _handlers;
109 std::vector<struct pollfd> _events;
111 /* member functions */
114 * Checks if a handler is already registered.
115 * \param handler the handler to check for existence
116 * \return true if the handler has been registered before
118 bool _handler_exists(const scd_sock_ev_handler_if& handler) const;
121 * Returns the index of an event handler in the vector. This is the same
122 * index as the corresponding pollfd.
123 * \param handler the handler to get the index of
124 * \return the index of the handler or -1 if it could not be found
126 int _get_index(const scd_sock_ev_handler_if& handler) const;
129 * Polls during a specified amount of time.
130 * \param ms miliseconds to block (-1 for infinite)
131 * \exception scd_exception if an unexpected error occured
132 * \return number of sockets for witch events occured
137 * Calls handlers back for which events occured.
142 //scd_sock_poller* scd_get_sock_poller();