poller.hpp 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#ifndef POLLER_INCLUDED
# define POLLER_INCLUDED

#include <network/socket_handler.hpp>

#include <unordered_map>
#include <memory>

#define POLL 1
#define EPOLL 2
#define KQUEUE 3

#define POLLER POLL

#if POLLER == POLL
 #include <poll.h>
 // TODO, dynamic size, without artificial limit
 #define MAX_POLL_FD_NUMBER 4096
#endif

/**
 * We pass some SocketHandlers to this the Poller, which uses
 * poll/epoll/kqueue/select etc to wait for events on these SocketHandlers,
 * and call the callbacks when event occurs.
 *
 * TODO: support for all these pollers:
 * - poll(2) (mandatory)
 * - epoll(7)
 * - kqueue(2)
 */


class Poller
{
public:
  explicit Poller();
  ~Poller();
  /**
   * Add a SocketHandler to be monitored by this Poller. All receive events
   * are always automatically watched.
   */
  void add_socket_handler(std::shared_ptr<SocketHandler> socket_handler);
  /**
   * Remove (and stop managing) a SocketHandler, designed by the given socket_t.
   */
  void remove_socket_handler(const socket_t socket);
  /**
   * Wait for all watched events, and call the SocketHandlers' callbacks
   * when one is ready.
   */
  void poll();

private:
  /**
   * A "list" of all the SocketHandlers that we manage, indexed by socket,
   * because that's what is returned by select/poll/etc when an event
   * occures.
   */
  std::unordered_map<socket_t, std::shared_ptr<SocketHandler>> socket_handlers;

#if POLLER == POLL
  struct pollfd fds[MAX_POLL_FD_NUMBER];
  nfds_t nfds;
#endif

  Poller(const Poller&) = delete;
  Poller(Poller&&) = delete;
  Poller& operator=(const Poller&) = delete;
  Poller& operator=(Poller&&) = delete;
};

#endif // POLLER_INCLUDED