1 #ifndef LIBSOCKET_EPOLL_H_E58EF2DF7057FA0C7A95D6E753414229
2 #define LIBSOCKET_EPOLL_H_E58EF2DF7057FA0C7A95D6E753414229
43 #include <sys/epoll.h>
69 template <
typename SocketT>
72 typedef std::pair<std::vector<SocketT*>, std::vector<SocketT*> >
80 void add_fd(SocketT& sock,
int method);
81 void del_fd(
const SocketT& sock);
82 ready_socks
wait(
int timeout = -1);
102 template <
typename SocketT>
104 : maxevents(maxevs), events(new struct epoll_event[maxevs]) {
110 string(
"epoll_create1 failed: ") + strerror(errno));
116 template <
typename SocketT>
118 maxevents = new_epollset.maxevents;
119 epollfd = new_epollset.epollfd;
120 events = new_epollset.events;
122 new_epollset.epollfd = -1;
123 new_epollset.events =
nullptr;
126 template <
typename SocketT>
138 template <
typename SocketT>
140 struct epoll_event new_event;
142 new_event.data.ptr = 0;
143 new_event.events = 0;
145 if (method & LIBSOCKET_READ) new_event.events |= EPOLLIN;
146 if (method & LIBSOCKET_WRITE) new_event.events |= EPOLLOUT;
148 new_event.data.ptr = &sock;
150 if (0 > epoll_ctl(epollfd, EPOLL_CTL_ADD, sock.getfd(), &new_event))
152 string(
"epoll_ctl failed: ") + strerror(errno));
160 template <
typename SocketT>
162 if (0 > epoll_ctl(epollfd, EPOLL_CTL_DEL, sock.getfd(),
nullptr))
164 string(
"epoll_ctl failed: ") + strerror(errno));
188 template <
typename SocketT>
193 if (0 > (nfds = epoll_wait(epollfd, events, maxevents, timeout)))
195 string(
"epoll_wait failed: ") + strerror(errno));
197 for (
int i = 0; i < nfds; i++) {
198 if (events[i].events == EPOLLIN)
199 ready.first.push_back(
static_cast<SocketT*
>(events[i].data.ptr));
200 if (events[i].events == EPOLLOUT)
201 ready.second.push_back(
static_cast<SocketT*
>(events[i].data.ptr));
Class abstracting calls to the epoll API of Linux.
unsigned int maxevents
maxevents is passed to epoll_wait.
void del_fd(const SocketT &sock)
Remove a file descriptor from an epoll set.
int epollfd
The file descriptor used by the epoll API.
ready_socks wait(int timeout=-1)
Wait for an event on any file descriptor.
struct epoll_event * events
Array of structures, filled on the return of epoll_wait.
void add_fd(SocketT &sock, int method)
Add a socket to an epollset.
epollset(unsigned int maxevents=128)
Construct a new epollset.
Contains libsocket elements.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...