poller.cpp 5.17 KB
Newer Older
1 2 3
#include <network/poller.hpp>

#include <assert.h>
louiz’'s avatar
louiz’ committed
4 5
#include <stdio.h>

6 7 8 9 10 11 12 13
#include <cstring>
#include <iostream>

Poller::Poller()
{
#if POLLER == POLL
  memset(this->fds, 0, sizeof(this->fds));
  this->nfds = 0;
louiz’'s avatar
louiz’ committed
14 15 16 17 18 19 20
#elif POLLER == EPOLL
  this->epfd = ::epoll_create1(0);
  if (this->epfd == -1)
    {
      perror("epoll");
      throw std::runtime_error("Could not create epoll instance");
    }
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#endif
}

Poller::~Poller()
{
}

void Poller::add_socket_handler(std::shared_ptr<SocketHandler> socket_handler)
{
  // Raise an error if that socket is already in the list
  const auto it = this->socket_handlers.find(socket_handler->get_socket());
  if (it != this->socket_handlers.end())
    throw std::runtime_error("Trying to insert SocketHandler already managed");

  this->socket_handlers.emplace(socket_handler->get_socket(), socket_handler);
  socket_handler->set_poller(this);

  // We always watch all sockets for receive events
#if POLLER == POLL
  this->fds[this->nfds].fd = socket_handler->get_socket();
  this->fds[this->nfds].events = POLLIN;
  this->nfds++;
#endif
louiz’'s avatar
louiz’ committed
44 45 46 47 48 49 50 51 52 53 54
#if POLLER == EPOLL
  struct epoll_event event;
  event.data.ptr = socket_handler.get();
  event.events = EPOLLIN;
  const int res = ::epoll_ctl(this->epfd, EPOLL_CTL_ADD, socket_handler->get_socket(), &event);
  if (res == -1)
    {
      perror("epoll_ctl");
      throw std::runtime_error("Could not add socket to epoll");
    }
#endif
55 56 57 58 59 60 61 62
}

void Poller::remove_socket_handler(const socket_t socket)
{
  const auto it = this->socket_handlers.find(socket);
  if (it == this->socket_handlers.end())
    throw std::runtime_error("Trying to remove a SocketHandler that is not managed");
  this->socket_handlers.erase(it);
louiz’'s avatar
louiz’ committed
63 64

#if POLLER == POLL
65 66 67 68 69 70 71 72 73 74 75 76 77 78
  for (size_t i = 0; i < this->nfds; i++)
    {
      if (this->fds[i].fd == socket)
        {
          // Move all subsequent pollfd by one on the left, erasing the
          // value of the one we remove
          for (size_t j = i; j < this->nfds - 1; ++j)
            {
              this->fds[j].fd = this->fds[j+1].fd;
              this->fds[j].events= this->fds[j+1].events;
            }
          this->nfds--;
        }
    }
louiz’'s avatar
louiz’ committed
79 80 81 82 83 84 85 86
#elif POLLER == EPOLL
  const int res = ::epoll_ctl(this->epfd, EPOLL_CTL_DEL, socket, nullptr);
  if (res == -1)
    {
      perror("epoll_ctl");
      throw std::runtime_error("Could not remove socket from epoll");
    }
#endif
87 88
}

louiz’'s avatar
louiz’ committed
89
void Poller::watch_send_events(SocketHandler* socket_handler)
louiz’'s avatar
louiz’ committed
90 91 92 93 94 95 96 97 98 99 100
{
#if POLLER == POLL
  for (size_t i = 0; i <= this->nfds; ++i)
    {
      if (this->fds[i].fd == socket_handler->get_socket())
        {
          this->fds[i].events = POLLIN|POLLOUT;
          return;
        }
    }
  throw std::runtime_error("Cannot watch a non-registered socket for send events");
louiz’'s avatar
louiz’ committed
101 102 103 104 105 106 107 108 109 110 111
#elif POLLER == EPOLL
  struct epoll_event event;
  event.data.ptr = socket_handler;
  event.events = EPOLLIN|EPOLLOUT;
  const int res = ::epoll_ctl(this->epfd, EPOLL_CTL_MOD, socket_handler->get_socket(), &event);
  if (res == -1)
    {
      perror("epoll_ctl");
      throw std::runtime_error("Could not modify socket flags in epoll");
    }
#endif
louiz’'s avatar
louiz’ committed
112 113
}

louiz’'s avatar
louiz’ committed
114
void Poller::stop_watching_send_events(SocketHandler* socket_handler)
louiz’'s avatar
louiz’ committed
115 116 117 118 119 120 121 122 123 124 125
{
#if POLLER == POLL
  for (size_t i = 0; i <= this->nfds; ++i)
    {
      if (this->fds[i].fd == socket_handler->get_socket())
        {
          this->fds[i].events = POLLIN;
          return;
        }
    }
  throw std::runtime_error("Cannot watch a non-registered socket for send events");
louiz’'s avatar
louiz’ committed
126 127 128 129 130 131 132 133 134 135 136
#elif POLLER == EPOLL
  struct epoll_event event;
  event.data.ptr = socket_handler;
  event.events = EPOLLIN;
  const int res = ::epoll_ctl(this->epfd, EPOLL_CTL_MOD, socket_handler->get_socket(), &event);
  if (res == -1)
    {
      perror("epoll_ctl");
      throw std::runtime_error("Could not modify socket flags in epoll");
    }
#endif
louiz’'s avatar
louiz’ committed
137 138
}

139
bool Poller::poll()
140 141
{
#if POLLER == POLL
142 143
  if (this->nfds == 0)
    return false;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  int res = ::poll(this->fds, this->nfds, -1);
  if (res < 0)
    {
      perror("poll");
      throw std::runtime_error("Poll failed");
    }
  // We cannot possibly have more ready events than the number of fds we are
  // watching
  assert(static_cast<unsigned int>(res) <= this->nfds);
  for (size_t i = 0; i <= this->nfds && res != 0; ++i)
    {
      if (this->fds[i].revents == 0)
        continue;
      else if (this->fds[i].revents & POLLIN)
        {
          auto socket_handler = this->socket_handlers.at(this->fds[i].fd);
          socket_handler->on_recv();
          res--;
        }
      else if (this->fds[i].revents & POLLOUT)
        {
          auto socket_handler = this->socket_handlers.at(this->fds[i].fd);
          socket_handler->on_send();
          res--;
        }
    }
louiz’'s avatar
louiz’ committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#elif POLLER == EPOLL
  static const size_t max_events = 12;
  struct epoll_event revents[max_events];
  const int nb_events = epoll_wait(this->epfd, revents, max_events, -1);
  if (nb_events == -1)
    {
      perror("epoll_wait");
      throw std::runtime_error("Epoll_wait failed");
    }
  for (int i = 0; i < nb_events; ++i)
    {
      auto socket_handler = static_cast<SocketHandler*>(revents[i].data.ptr);
      if (revents[i].events & EPOLLIN)
        socket_handler->on_recv();
      if (revents[i].events & EPOLLOUT)
        socket_handler->on_send();
    }
187
#endif
188
  return true;
189
}