memgraph/include/io/network/epoll.hpp

55 lines
952 B
C++
Raw Normal View History

2015-10-28 03:21:28 +08:00
#pragma once
#include <malloc.h>
#include <sys/epoll.h>
#include "io/network/socket.hpp"
2015-10-28 03:21:28 +08:00
#include "utils/likely.hpp"
namespace io
{
2016-08-02 05:14:09 +08:00
class EpollError : BasicException
{
public:
using BasicException::BasicException;
};
2015-10-28 03:21:28 +08:00
class Epoll
{
public:
using Event = struct epoll_event;
Epoll(int flags)
{
epoll_fd = epoll_create1(flags);
2016-08-02 05:14:09 +08:00
if(UNLIKELY(epoll_fd == -1))
throw EpollError("Can't create epoll file descriptor");
2015-10-28 03:21:28 +08:00
}
2016-08-02 05:14:09 +08:00
template <class Stream>
void add(Stream& stream, Event* event)
2015-10-28 03:21:28 +08:00
{
2016-08-02 05:14:09 +08:00
auto status = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stream, event);
2015-10-28 03:21:28 +08:00
if(UNLIKELY(status))
2016-08-02 05:14:09 +08:00
throw EpollError("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;
};
}