#ifndef EVENTMANAGER_H_ #define EVENTMANAGER_H_ #include #include // We need a struct to store both the address of the callback fn // to register with epoll, and the fd that we are listening to, so we can // pass it in as an argument to the callback fn typedef void (*CallbackFn)(int fd); typedef struct CallbackData { CallbackFn callback; int fd; } CallbackData; class EventManager { public: EventManager(); ~EventManager(); // Adds to the global variable epoll_fd, establishing // an event handler that waits for fd to be ready // to read, at which time it calls callback(fd) void RegisterCallback(int fd, CallbackFn callback); // Removes the callback corresponding to fd from epoll_fd void RemoveCallback(int fd); void WaitAndDispatch(); private: int epoll_fd_; // This list contains the data for all the fds currently being listened // to with epoll_fd, and the address of the callback to invoke // Must be a linked list because the kernel maintains pointers to the // node payloads through epoll_event.data.ptr // Only remove nodes from this list after they have been removed from epoll std::list active_list_; }; #endif