aboutsummaryrefslogtreecommitdiffstats
path: root/src/event/SocketMonitor.hxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/event/SocketMonitor.hxx61
1 files changed, 39 insertions, 22 deletions
diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx
index 5369ddb8a..f6aac2bd6 100644
--- a/src/event/SocketMonitor.hxx
+++ b/src/event/SocketMonitor.hxx
@@ -22,9 +22,11 @@
#include "check.h"
-#ifdef USE_EPOLL
-#include <sys/epoll.h>
-#else
+#ifdef USE_INTERNAL_EVENTLOOP
+#include "PollGroup.hxx"
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
#include <glib.h>
#endif
@@ -34,8 +36,8 @@
#include <stddef.h>
#ifdef WIN32
-/* ERRORis a WIN32 macro that poisons our namespace; this is a
- kludge to allow us to use it anyway */
+/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
+ to allow us to use it anyway */
#ifdef ERROR
#undef ERROR
#endif
@@ -43,9 +45,14 @@
class EventLoop;
+/**
+ * Monitor events on a socket. Call Schedule() to announce events
+ * you're interested in, or Cancel() to cancel your subscription. The
+ * #EventLoop will invoke virtual method OnSocketReady() as soon as
+ * any of the subscribed events are ready.
+ */
class SocketMonitor {
-#ifdef USE_EPOLL
-#else
+#ifdef USE_GLIB_EVENTLOOP
struct Source {
GSource base;
@@ -56,38 +63,44 @@ class SocketMonitor {
int fd;
EventLoop &loop;
-#ifdef USE_EPOLL
+#ifdef USE_INTERNAL_EVENTLOOP
/**
* A bit mask of events that is currently registered in the EventLoop.
*/
unsigned scheduled_flags;
-#else
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
Source *source;
GPollFD poll;
#endif
public:
-#ifdef USE_EPOLL
- static constexpr unsigned READ = EPOLLIN;
- static constexpr unsigned WRITE = EPOLLOUT;
- static constexpr unsigned ERROR = EPOLLERR;
- static constexpr unsigned HANGUP = EPOLLHUP;
-#else
+#ifdef USE_INTERNAL_EVENTLOOP
+ static constexpr unsigned READ = PollGroup::READ;
+ static constexpr unsigned WRITE = PollGroup::WRITE;
+ static constexpr unsigned ERROR = PollGroup::ERROR;
+ static constexpr unsigned HANGUP = PollGroup::HANGUP;
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
static constexpr unsigned READ = G_IO_IN;
static constexpr unsigned WRITE = G_IO_OUT;
static constexpr unsigned ERROR = G_IO_ERR;
static constexpr unsigned HANGUP = G_IO_HUP;
-#endif
+#endif
typedef std::make_signed<size_t>::type ssize_t;
-#ifdef USE_EPOLL
+#ifdef USE_INTERNAL_EVENTLOOP
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), scheduled_flags(0) {}
SocketMonitor(int _fd, EventLoop &_loop)
:fd(_fd), loop(_loop), scheduled_flags(0) {}
-#else
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), source(nullptr) {}
@@ -128,9 +141,11 @@ public:
unsigned GetScheduledFlags() const {
assert(IsDefined());
-#ifdef USE_EPOLL
+#ifdef USE_INTERNAL_EVENTLOOP
return scheduled_flags;
-#else
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
return poll.events;
#endif
}
@@ -167,9 +182,11 @@ protected:
virtual bool OnSocketReady(unsigned flags) = 0;
public:
-#ifdef USE_EPOLL
+#ifdef USE_INTERNAL_EVENTLOOP
void Dispatch(unsigned flags);
-#else
+#endif
+
+#ifdef USE_GLIB_EVENTLOOP
/* GSource callbacks */
static gboolean Prepare(GSource *source, gint *timeout_r);
static gboolean Check(GSource *source);