#include <VWeb.h>
namespace VWeb {
EPollManager::EPollManager() {
  m_EpollID = epoll_create1(0);
  if (m_EpollID < 0)
    exit(49);
  unsigned int flags = fcntl(m_EpollID, F_GETFL, 0);
  flags |= O_NONBLOCK;
  fcntl(m_EpollID, F_SETFL, flags);
}

EPollManager::~EPollManager() { close(m_EpollID); }

bool EPollManager::Dispatch(int sock, uint32_t eventType) const {
  struct epoll_event event {
    0
  };
  event.data.fd = sock;
  event.events = eventType;
  return epoll_ctl(m_EpollID, EPOLL_CTL_ADD, sock, &event) != -1;
}

bool EPollManager::UpdateEvents(int sock, uint32_t eventType) const {
  struct epoll_event event {
    0
  };
  event.data.fd = sock;
  event.events = eventType;
  return epoll_ctl(m_EpollID, EPOLL_CTL_MOD, sock, &event) != -1;
}

int EPollManager::Wait(int maxEvents, epoll_event *events) const {
  return epoll_wait(m_EpollID, events, maxEvents, 10);
}
} // namespace VWeb