2015-10-28 03:21:28 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
|
|
|
#include "socket.hpp"
|
|
|
|
#include "utils/likely.hpp"
|
|
|
|
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
|
|
|
|
class Epoll
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Event = struct epoll_event;
|
|
|
|
|
|
|
|
Epoll(int flags)
|
|
|
|
{
|
|
|
|
epoll_fd = epoll_create1(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(Socket& socket, Event* event)
|
|
|
|
{
|
|
|
|
auto status = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket, event);
|
|
|
|
|
|
|
|
if(UNLIKELY(status))
|
2015-11-23 04:35:40 +08:00
|
|
|
throw NetworkError("Can't add an event to epoll listener.");
|
2015-10-28 03:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int wait(Event* events, int max_events, int timeout)
|
|
|
|
{
|
|
|
|
return epoll_wait(epoll_fd, events, max_events, timeout);
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:34:43 +08:00
|
|
|
int id() const
|
|
|
|
{
|
|
|
|
return epoll_fd;
|
|
|
|
}
|
|
|
|
|
2015-10-28 03:21:28 +08:00
|
|
|
private:
|
|
|
|
int epoll_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|