a8/a8/eventfd.cc
aozhiwei 67358105a4 1
2020-01-09 17:50:39 +08:00

80 lines
1.4 KiB
C++

#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <a8/a8.h>
#include <a8/eventfd.h>
namespace a8
{
EventFD::EventFD()
{
fd_ = ::eventfd(0, 0);
if (fd_ == a8::INVALID_FD) {
abort();
}
}
EventFD::~EventFD()
{
UnInit();
}
void EventFD::Init(void* context)
{
context_ = context;
}
void EventFD::UnInit()
{
if (fd_ != a8::INVALID_FD) {
struct epoll_event ev;
::epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd_, &ev);
::close(fd_);
fd_ = a8::INVALID_FD;
}
}
void EventFD::Write(unsigned long long value)
{
::eventfd_write(fd_, value);
}
void EventFD::SetEpollFd(int epoll_fd)
{
struct epoll_event ev;
ev.data.fd = fd_;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = this;
int ret = ::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_, &ev);
if (ret != 0) {
abort();
}
epoll_fd_ = epoll_fd;
}
void EventFD::DoRecv()
{
eventfd_t value = 0;
::eventfd_read(fd_, &value);
if (OnEvent) {
OnEvent(context_, value);
}
}
void EventFD::DoSend()
{
abort();
}
void EventFD::DoError()
{
abort();
}
}