summaryrefslogtreecommitdiff
path: root/src/network/poller.hpp
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2013-11-02 03:19:24 +0100
committerFlorent Le Coz <louiz@louiz.org>2013-11-02 03:19:24 +0100
commit7869ef2ace9a487abb0b489ca432b0a8878c5083 (patch)
treefbb112c8f49266b1bee8303b169b512871d1bd84 /src/network/poller.hpp
downloadbiboumi-7869ef2ace9a487abb0b489ca432b0a8878c5083.tar.gz
biboumi-7869ef2ace9a487abb0b489ca432b0a8878c5083.tar.bz2
biboumi-7869ef2ace9a487abb0b489ca432b0a8878c5083.tar.xz
biboumi-7869ef2ace9a487abb0b489ca432b0a8878c5083.zip
First step of the connection skeleton
Basic connect, socket creating, polling, recving, etc.
Diffstat (limited to 'src/network/poller.hpp')
-rw-r--r--src/network/poller.hpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/network/poller.hpp b/src/network/poller.hpp
new file mode 100644
index 0000000..46a184e
--- /dev/null
+++ b/src/network/poller.hpp
@@ -0,0 +1,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