1 #ifndef LIBSOCKET_EPOLL_H_E58EF2DF7057FA0C7A95D6E753414229 2 #define LIBSOCKET_EPOLL_H_E58EF2DF7057FA0C7A95D6E753414229 42 #include <sys/epoll.h> 68 template <
typename SocketT>
71 typedef std::pair<std::vector<SocketT*>, std::vector<SocketT*> >
79 void add_fd(SocketT& sock,
int method);
80 void del_fd(
const SocketT& sock);
81 ready_socks
wait(
int timeout = -1);
101 template <
typename SocketT>
103 : maxevents(maxevs), events(new struct epoll_event[maxevs]) {
109 string(
"epoll_create1 failed: ") + strerror(errno));
115 template <
typename SocketT>
117 maxevents = new_epollset.maxevents;
118 epollfd = new_epollset.epollfd;
119 events = new_epollset.events;
121 new_epollset.epollfd = -1;
122 new_epollset.events =
nullptr;
125 template <
typename SocketT>
137 template <
typename SocketT>
139 struct epoll_event new_event;
141 new_event.data.ptr = 0;
142 new_event.events = 0;
144 if (method & LIBSOCKET_READ) new_event.events |= EPOLLIN;
145 if (method & LIBSOCKET_WRITE) new_event.events |= EPOLLOUT;
147 new_event.data.ptr = &sock;
149 if (0 > epoll_ctl(epollfd, EPOLL_CTL_ADD, sock.getfd(), &new_event))
151 string(
"epoll_ctl failed: ") + strerror(errno));
159 template <
typename SocketT>
161 if (0 > epoll_ctl(epollfd, EPOLL_CTL_DEL, sock.getfd(),
nullptr))
163 string(
"epoll_ctl failed: ") + strerror(errno));
187 template <
typename SocketT>
192 if (0 > (nfds = epoll_wait(epollfd, events, maxevents, timeout)))
194 string(
"epoll_wait failed: ") + strerror(errno));
196 for (
int i = 0; i < nfds; i++) {
197 if (events[i].events == EPOLLIN)
198 ready.first.push_back(static_cast<SocketT*>(events[i].data.ptr));
199 if (events[i].events == EPOLLOUT)
200 ready.second.push_back(static_cast<SocketT*>(events[i].data.ptr));
Class abstracting calls to the epoll API of Linux.
Contains libsocket elements.
void add_fd(SocketT &sock, int method)
Add a socket to an epollset.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
ready_socks wait(int timeout=-1)
Wait for an event on any file descriptor.
epollset(unsigned int maxevents=128)
Construct a new epollset.
unsigned int maxevents
maxevents is passed to epoll_wait.
int epollfd
The file descriptor used by the epoll API.
void del_fd(const SocketT &sock)
Remove a file descriptor from an epoll set.
struct epoll_event * events
Array of structures, filled on the return of epoll_wait.