aboutsummaryrefslogtreecommitdiffstats
path: root/src/event
diff options
context:
space:
mode:
Diffstat (limited to 'src/event')
-rw-r--r--src/event/BufferedSocket.cxx256
-rw-r--r--src/event/BufferedSocket.hxx125
-rw-r--r--src/event/Loop.hxx84
-rw-r--r--src/event/MultiSocketMonitor.cxx107
-rw-r--r--src/event/MultiSocketMonitor.hxx124
-rw-r--r--src/event/SocketMonitor.cxx121
-rw-r--r--src/event/SocketMonitor.hxx123
-rw-r--r--src/event/TimeoutMonitor.cxx65
-rw-r--r--src/event/TimeoutMonitor.hxx60
-rw-r--r--src/event/WakeFD.cxx225
-rw-r--r--src/event/WakeFD.hxx80
11 files changed, 1370 insertions, 0 deletions
diff --git a/src/event/BufferedSocket.cxx b/src/event/BufferedSocket.cxx
new file mode 100644
index 000000000..f84fe808b
--- /dev/null
+++ b/src/event/BufferedSocket.cxx
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "BufferedSocket.hxx"
+#include "SocketError.hxx"
+#include "util/fifo_buffer.h"
+
+#include <assert.h>
+#include <stdint.h>
+#include <string.h>
+
+#ifndef WIN32
+#include <sys/types.h>
+#include <sys/socket.h>
+#endif
+
+BufferedSocket::~BufferedSocket()
+{
+ if (input != nullptr)
+ fifo_buffer_free(input);
+}
+
+BufferedSocket::ssize_t
+BufferedSocket::DirectWrite(const void *data, size_t length)
+{
+ int flags = 0;
+#ifdef MSG_NOSIGNAL
+ flags |= MSG_NOSIGNAL;
+#endif
+#ifdef MSG_DONTWAIT
+ flags |= MSG_DONTWAIT;
+#endif
+
+ const auto nbytes = send(Get(), (const char *)data, length, flags);
+ if (gcc_unlikely(nbytes < 0)) {
+ const auto code = GetSocketError();
+ if (IsSocketErrorAgain(code))
+ return 0;
+
+ Cancel();
+
+ if (IsSocketErrorClosed(code))
+ OnSocketClosed();
+ else
+ OnSocketError(NewSocketError(code));
+ }
+
+ return nbytes;
+}
+
+ssize_t
+BufferedSocket::DirectRead(void *data, size_t length)
+{
+ int flags = 0;
+#ifdef MSG_DONTWAIT
+ flags |= MSG_DONTWAIT;
+#endif
+
+ const auto nbytes = recv(Get(), (char *)data, length, flags);
+ if (gcc_likely(nbytes > 0))
+ return nbytes;
+
+ if (nbytes == 0) {
+ OnSocketClosed();
+ return -1;
+ }
+
+ const auto code = GetSocketError();
+ if (IsSocketErrorAgain(code))
+ return 0;
+
+ if (IsSocketErrorClosed(code))
+ OnSocketClosed();
+ else
+ OnSocketError(NewSocketError(code));
+ return -1;
+}
+
+bool
+BufferedSocket::WriteFromBuffer()
+{
+ assert(IsDefined());
+
+ size_t length;
+ const void *data = output.Read(&length);
+ if (data == nullptr) {
+ CancelWrite();
+ return true;
+ }
+
+ auto nbytes = DirectWrite(data, length);
+ if (gcc_unlikely(nbytes <= 0))
+ return nbytes == 0;
+
+ output.Consume(nbytes);
+
+ if (output.IsEmpty())
+ CancelWrite();
+
+ return true;
+}
+
+bool
+BufferedSocket::ReadToBuffer()
+{
+ assert(IsDefined());
+
+ if (input == nullptr)
+ input = fifo_buffer_new(8192);
+
+ size_t length;
+ void *buffer = fifo_buffer_write(input, &length);
+ assert(buffer != nullptr);
+
+ const auto nbytes = DirectRead(buffer, length);
+ if (nbytes > 0)
+ fifo_buffer_append(input, nbytes);
+
+ return nbytes >= 0;
+}
+
+bool
+BufferedSocket::Write(const void *data, size_t length)
+{
+ assert(IsDefined());
+
+#if 0
+ /* TODO: disabled because this would add overhead on some callers (the ones that often), but it may be useful */
+
+ if (output.IsEmpty()) {
+ /* try to write it directly first */
+ const auto nbytes = DirectWrite(data, length);
+ if (gcc_likely(nbytes > 0)) {
+ data = (const uint8_t *)data + nbytes;
+ length -= nbytes;
+ if (length == 0)
+ return true;
+ } else if (nbytes < 0)
+ return false;
+ }
+#endif
+
+ if (!output.Append(data, length)) {
+ // TODO
+ OnSocketError(g_error_new_literal(g_quark_from_static_string("buffered_socket"),
+ 0, "Output buffer is full"));
+ return false;
+ }
+
+ ScheduleWrite();
+ return true;
+}
+
+bool
+BufferedSocket::ResumeInput()
+{
+ assert(IsDefined());
+
+ if (input == nullptr) {
+ ScheduleRead();
+ return true;
+ }
+
+ while (true) {
+ size_t length;
+ const void *data = fifo_buffer_read(input, &length);
+ if (data == nullptr) {
+ ScheduleRead();
+ return true;
+ }
+
+ const auto result = OnSocketInput(data, length);
+ switch (result) {
+ case InputResult::MORE:
+ if (fifo_buffer_is_full(input)) {
+ // TODO
+ OnSocketError(g_error_new_literal(g_quark_from_static_string("buffered_socket"),
+ 0, "Input buffer is full"));
+ return false;
+ }
+
+ ScheduleRead();
+ return true;
+
+ case InputResult::PAUSE:
+ CancelRead();
+ return true;
+
+ case InputResult::AGAIN:
+ continue;
+
+ case InputResult::CLOSED:
+ return false;
+ }
+ }
+}
+
+void
+BufferedSocket::ConsumeInput(size_t nbytes)
+{
+ assert(IsDefined());
+
+ fifo_buffer_consume(input, nbytes);
+}
+
+void
+BufferedSocket::OnSocketReady(unsigned flags)
+{
+ assert(IsDefined());
+
+ if (gcc_unlikely(flags & (ERROR|HANGUP))) {
+ OnSocketClosed();
+ return;
+ }
+
+ if (flags & READ) {
+ assert(input == nullptr || !fifo_buffer_is_full(input));
+
+ if (!ReadToBuffer() || !ResumeInput())
+ return;
+
+ if (input == nullptr || !fifo_buffer_is_full(input))
+ ScheduleRead();
+
+ /* just in case the OnSocketInput() method has added
+ data to the output buffer: try to send it now
+ instead of waiting for the next event loop
+ iteration */
+ if (!output.IsEmpty())
+ flags |= WRITE;
+ }
+
+ if (flags & WRITE) {
+ assert(!output.IsEmpty());
+
+ if (!WriteFromBuffer())
+ return;
+ }
+}
diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx
new file mode 100644
index 000000000..49b17c86f
--- /dev/null
+++ b/src/event/BufferedSocket.hxx
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_BUFFERED_SOCKET_HXX
+#define MPD_BUFFERED_SOCKET_HXX
+
+#include "check.h"
+#include "SocketMonitor.hxx"
+#include "util/PeakBuffer.hxx"
+#include "gcc.h"
+
+#include <type_traits>
+
+#include <stddef.h>
+
+struct fifo_buffer;
+class EventLoop;
+
+class BufferedSocket : private SocketMonitor {
+ typedef std::make_signed<size_t>::type ssize_t;
+
+ fifo_buffer *input;
+ PeakBuffer output;
+
+public:
+ BufferedSocket(int _fd, EventLoop &_loop,
+ size_t normal_size, size_t peak_size=0)
+ :SocketMonitor(_fd, _loop), input(nullptr),
+ output(normal_size, peak_size) {
+ ScheduleRead();
+ }
+
+ ~BufferedSocket();
+
+ using SocketMonitor::IsDefined;
+ using SocketMonitor::Close;
+
+private:
+ ssize_t DirectWrite(const void *data, size_t length);
+ ssize_t DirectRead(void *data, size_t length);
+
+ /**
+ * Send data from the output buffer to the socket.
+ *
+ * @return false if the socket has been closed
+ */
+ bool WriteFromBuffer();
+
+ /**
+ * Receive data from the socket to the input buffer.
+ *
+ * @return false if the socket has been closed
+ */
+ bool ReadToBuffer();
+
+protected:
+ /**
+ * @return false if the socket has been closed
+ */
+ bool Write(const void *data, size_t length);
+
+ /**
+ * @return false if the socket has been closed
+ */
+ bool ResumeInput();
+
+ /**
+ * Mark a portion of the input buffer "consumed". Only
+ * allowed to be called from OnSocketInput(). This method
+ * does not invalidate the pointer passed to OnSocketInput()
+ * yet.
+ */
+ void ConsumeInput(size_t nbytes);
+
+ enum class InputResult {
+ /**
+ * The method was successful, and it is ready to
+ * read more data.
+ */
+ MORE,
+
+ /**
+ * The method does not want to get more data for now.
+ * It will call ResumeInput() when it's ready for
+ * more.
+ */
+ PAUSE,
+
+ /**
+ * The method wants to be called again immediately, if
+ * there's more data in the buffer.
+ */
+ AGAIN,
+
+ /**
+ * The method has closed the socket.
+ */
+ CLOSED,
+ };
+
+ virtual InputResult OnSocketInput(const void *data, size_t length) = 0;
+ virtual void OnSocketError(GError *error) = 0;
+ virtual void OnSocketClosed() = 0;
+
+private:
+ virtual void OnSocketReady(unsigned flags) override;
+};
+
+#endif
diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx
new file mode 100644
index 000000000..31ef1613c
--- /dev/null
+++ b/src/event/Loop.hxx
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_EVENT_LOOP_HXX
+#define MPD_EVENT_LOOP_HXX
+
+#include "check.h"
+#include "gcc.h"
+
+#include <glib.h>
+
+class EventLoop {
+ GMainContext *context;
+ GMainLoop *loop;
+
+public:
+ EventLoop()
+ :context(g_main_context_new()),
+ loop(g_main_loop_new(context, false)) {}
+
+ struct Default {};
+ EventLoop(gcc_unused Default _dummy)
+ :context(g_main_context_ref(g_main_context_default())),
+ loop(g_main_loop_new(context, false)) {}
+
+ ~EventLoop() {
+ g_main_loop_unref(loop);
+ g_main_context_unref(context);
+ }
+
+ GMainContext *GetContext() {
+ return context;
+ }
+
+ void Break() {
+ g_main_loop_quit(loop);
+ }
+
+ void Run() {
+ g_main_loop_run(loop);
+ }
+
+ guint AddIdle(GSourceFunc function, gpointer data) {
+ GSource *source = g_idle_source_new();
+ g_source_set_callback(source, function, data, NULL);
+ guint id = g_source_attach(source, GetContext());
+ g_source_unref(source);
+ return id;
+ }
+
+ GSource *AddTimeout(guint interval_ms,
+ GSourceFunc function, gpointer data) {
+ GSource *source = g_timeout_source_new(interval_ms);
+ g_source_set_callback(source, function, data, nullptr);
+ g_source_attach(source, GetContext());
+ return source;
+ }
+
+ GSource *AddTimeoutSeconds(guint interval_s,
+ GSourceFunc function, gpointer data) {
+ GSource *source = g_timeout_source_new_seconds(interval_s);
+ g_source_set_callback(source, function, data, nullptr);
+ g_source_attach(source, GetContext());
+ return source;
+ }
+};
+
+#endif /* MAIN_NOTIFY_H */
diff --git a/src/event/MultiSocketMonitor.cxx b/src/event/MultiSocketMonitor.cxx
new file mode 100644
index 000000000..6f20b907c
--- /dev/null
+++ b/src/event/MultiSocketMonitor.cxx
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "MultiSocketMonitor.hxx"
+#include "Loop.hxx"
+#include "fd_util.h"
+#include "gcc.h"
+
+#include <assert.h>
+
+/**
+ * The vtable for our GSource implementation. Unfortunately, we
+ * cannot declare it "const", because g_source_new() takes a non-const
+ * pointer, for whatever reason.
+ */
+static GSourceFuncs multi_socket_monitor_source_funcs = {
+ MultiSocketMonitor::Prepare,
+ MultiSocketMonitor::Check,
+ MultiSocketMonitor::Dispatch,
+ nullptr,
+ nullptr,
+ nullptr,
+};
+
+MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop)
+ :loop(_loop),
+ source((Source *)g_source_new(&multi_socket_monitor_source_funcs,
+ sizeof(*source))) {
+ source->monitor = this;
+
+ g_source_attach(&source->base, loop.GetContext());
+}
+
+MultiSocketMonitor::~MultiSocketMonitor()
+{
+ g_source_destroy(&source->base);
+ g_source_unref(&source->base);
+ source = nullptr;
+}
+
+bool
+MultiSocketMonitor::Check() const
+{
+ if (CheckSockets())
+ return true;
+
+ for (const auto &i : fds)
+ if (i.revents != 0)
+ return true;
+
+ return false;
+}
+
+/*
+ * GSource methods
+ *
+ */
+
+gboolean
+MultiSocketMonitor::Prepare(GSource *_source, gint *timeout_r)
+{
+ Source &source = *(Source *)_source;
+ MultiSocketMonitor &monitor = *source.monitor;
+ assert(_source == &monitor.source->base);
+
+ return monitor.Prepare(timeout_r);
+}
+
+gboolean
+MultiSocketMonitor::Check(GSource *_source)
+{
+ const Source &source = *(const Source *)_source;
+ const MultiSocketMonitor &monitor = *source.monitor;
+ assert(_source == &monitor.source->base);
+
+ return monitor.Check();
+}
+
+gboolean
+MultiSocketMonitor::Dispatch(GSource *_source,
+ gcc_unused GSourceFunc callback,
+ gcc_unused gpointer user_data)
+{
+ Source &source = *(Source *)_source;
+ MultiSocketMonitor &monitor = *source.monitor;
+ assert(_source == &monitor.source->base);
+
+ monitor.Dispatch();
+ return true;
+}
diff --git a/src/event/MultiSocketMonitor.hxx b/src/event/MultiSocketMonitor.hxx
new file mode 100644
index 000000000..9d0e1502b
--- /dev/null
+++ b/src/event/MultiSocketMonitor.hxx
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_MULTI_SOCKET_MONITOR_HXX
+#define MPD_MULTI_SOCKET_MONITOR_HXX
+
+#include "check.h"
+#include "gcc.h"
+
+#include <glib.h>
+
+#include <forward_list>
+
+#include <assert.h>
+
+#ifdef WIN32
+/* ERRORis a WIN32 macro that poisons our namespace; this is a
+ kludge to allow us to use it anyway */
+#ifdef ERROR
+#undef ERROR
+#endif
+#endif
+
+class EventLoop;
+
+/**
+ * Monitor multiple sockets.
+ */
+class MultiSocketMonitor {
+ struct Source {
+ GSource base;
+
+ MultiSocketMonitor *monitor;
+ };
+
+ EventLoop &loop;
+ Source *source;
+ std::forward_list<GPollFD> fds;
+
+public:
+ 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;
+
+ MultiSocketMonitor(EventLoop &_loop);
+ ~MultiSocketMonitor();
+
+public:
+ gcc_pure
+ gint64 GetTime() const {
+ return g_source_get_time(&source->base);
+ }
+
+ void InvalidateSockets() {
+ /* no-op because GLib always calls the GSource's
+ "prepare" method before each poll() anyway */
+ }
+
+ void AddSocket(int fd, unsigned events) {
+ fds.push_front({fd, gushort(events), 0});
+ g_source_add_poll(&source->base, &fds.front());
+ }
+
+ template<typename E>
+ void UpdateSocketList(E &&e) {
+ for (auto prev = fds.before_begin(), end = fds.end(),
+ i = std::next(prev);
+ i != end; i = std::next(prev)) {
+ assert(i->events != 0);
+
+ unsigned events = e(i->fd);
+ if (events != 0) {
+ i->events = events;
+ prev = i;
+ } else {
+ g_source_remove_poll(&source->base, &*i);
+ fds.erase_after(prev);
+ }
+ }
+ }
+
+protected:
+ virtual void PrepareSockets(gcc_unused gint *timeout_r) {}
+ virtual bool CheckSockets() const { return false; }
+ virtual void DispatchSockets() = 0;
+
+public:
+ /* GSource callbacks */
+ static gboolean Prepare(GSource *source, gint *timeout_r);
+ static gboolean Check(GSource *source);
+ static gboolean Dispatch(GSource *source, GSourceFunc callback,
+ gpointer user_data);
+
+private:
+ bool Prepare(gint *timeout_r) {
+ PrepareSockets(timeout_r);
+ return false;
+ }
+
+ bool Check() const;
+
+ void Dispatch() {
+ DispatchSockets();
+ }
+};
+
+#endif
diff --git a/src/event/SocketMonitor.cxx b/src/event/SocketMonitor.cxx
new file mode 100644
index 000000000..b75dc72a4
--- /dev/null
+++ b/src/event/SocketMonitor.cxx
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "SocketMonitor.hxx"
+#include "Loop.hxx"
+#include "fd_util.h"
+#include "gcc.h"
+
+#include <assert.h>
+
+/*
+ * GSource methods
+ *
+ */
+
+gboolean
+SocketMonitor::Prepare(gcc_unused GSource *source, gcc_unused gint *timeout_r)
+{
+ return false;
+}
+
+gboolean
+SocketMonitor::Check(GSource *_source)
+{
+ const Source &source = *(const Source *)_source;
+ const SocketMonitor &monitor = *source.monitor;
+ assert(_source == &monitor.source->base);
+
+ return monitor.Check();
+}
+
+gboolean
+SocketMonitor::Dispatch(GSource *_source,
+ gcc_unused GSourceFunc callback,
+ gcc_unused gpointer user_data)
+{
+ Source &source = *(Source *)_source;
+ SocketMonitor &monitor = *source.monitor;
+ assert(_source == &monitor.source->base);
+
+ monitor.Dispatch();
+ return true;
+}
+
+/**
+ * The vtable for our GSource implementation. Unfortunately, we
+ * cannot declare it "const", because g_source_new() takes a non-const
+ * pointer, for whatever reason.
+ */
+static GSourceFuncs socket_monitor_source_funcs = {
+ SocketMonitor::Prepare,
+ SocketMonitor::Check,
+ SocketMonitor::Dispatch,
+ nullptr,
+ nullptr,
+ nullptr,
+};
+
+SocketMonitor::SocketMonitor(int _fd, EventLoop &_loop)
+ :fd(-1), loop(_loop),
+ source(nullptr) {
+ assert(_fd >= 0);
+
+ Open(_fd);
+}
+
+SocketMonitor::~SocketMonitor()
+{
+ if (IsDefined())
+ Close();
+}
+
+void
+SocketMonitor::Open(int _fd)
+{
+ assert(fd < 0);
+ assert(source == nullptr);
+ assert(_fd >= 0);
+
+ fd = _fd;
+ poll = {fd, 0, 0};
+
+ source = (Source *)g_source_new(&socket_monitor_source_funcs,
+ sizeof(*source));
+ source->monitor = this;
+
+ g_source_attach(&source->base, loop.GetContext());
+ g_source_add_poll(&source->base, &poll);
+}
+
+void
+SocketMonitor::Close()
+{
+ assert(IsDefined());
+
+ Cancel();
+
+ close_socket(fd);
+ fd = -1;
+
+ g_source_destroy(&source->base);
+ g_source_unref(&source->base);
+ source = nullptr;
+}
diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx
new file mode 100644
index 000000000..236e5fbda
--- /dev/null
+++ b/src/event/SocketMonitor.hxx
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_SOCKET_MONITOR_HXX
+#define MPD_SOCKET_MONITOR_HXX
+
+#include "check.h"
+
+#include <glib.h>
+
+#include <assert.h>
+
+#ifdef WIN32
+/* ERRORis a WIN32 macro that poisons our namespace; this is a
+ kludge to allow us to use it anyway */
+#ifdef ERROR
+#undef ERROR
+#endif
+#endif
+
+class EventLoop;
+
+class SocketMonitor {
+ struct Source {
+ GSource base;
+
+ SocketMonitor *monitor;
+ };
+
+ int fd;
+ EventLoop &loop;
+ Source *source;
+ GPollFD poll;
+
+public:
+ 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;
+
+ SocketMonitor(EventLoop &_loop)
+ :fd(-1), loop(_loop), source(nullptr) {}
+
+ SocketMonitor(int _fd, EventLoop &_loop);
+
+ ~SocketMonitor();
+
+ bool IsDefined() const {
+ return fd >= 0;
+ }
+
+ int Get() const {
+ assert(IsDefined());
+
+ return fd;
+ }
+
+ void Open(int _fd);
+
+ void Close();
+
+ void Schedule(unsigned flags) {
+ poll.events = flags;
+ poll.revents &= flags;
+ }
+
+ void Cancel() {
+ poll.events = 0;
+ }
+
+ void ScheduleRead() {
+ poll.events |= READ|HANGUP|ERROR;
+ }
+
+ void ScheduleWrite() {
+ poll.events |= WRITE;
+ }
+
+ void CancelRead() {
+ poll.events &= ~(READ|HANGUP|ERROR);
+ }
+
+ void CancelWrite() {
+ poll.events &= ~WRITE;
+ }
+
+protected:
+ virtual void OnSocketReady(unsigned flags) = 0;
+
+public:
+ /* GSource callbacks */
+ static gboolean Prepare(GSource *source, gint *timeout_r);
+ static gboolean Check(GSource *source);
+ static gboolean Dispatch(GSource *source, GSourceFunc callback,
+ gpointer user_data);
+
+private:
+ bool Check() const {
+ return (poll.revents & poll.events) != 0;
+ }
+
+ void Dispatch() {
+ OnSocketReady(poll.revents & poll.events);
+ }
+};
+
+#endif
diff --git a/src/event/TimeoutMonitor.cxx b/src/event/TimeoutMonitor.cxx
new file mode 100644
index 000000000..e0bf997a0
--- /dev/null
+++ b/src/event/TimeoutMonitor.cxx
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "TimeoutMonitor.hxx"
+#include "Loop.hxx"
+
+void
+TimeoutMonitor::Cancel()
+{
+ if (source != nullptr) {
+ g_source_destroy(source);
+ g_source_unref(source);
+ source = nullptr;
+ }
+}
+
+void
+TimeoutMonitor::Schedule(unsigned ms)
+{
+ Cancel();
+ source = loop.AddTimeout(ms, Callback, this);
+}
+
+void
+TimeoutMonitor::ScheduleSeconds(unsigned s)
+{
+ Cancel();
+ source = loop.AddTimeoutSeconds(s, Callback, this);
+}
+
+bool
+TimeoutMonitor::Run()
+{
+ bool result = OnTimeout();
+ if (!result && source != nullptr) {
+ g_source_unref(source);
+ source = nullptr;
+ }
+
+ return result;
+}
+
+gboolean
+TimeoutMonitor::Callback(gpointer data)
+{
+ TimeoutMonitor &monitor = *(TimeoutMonitor *)data;
+ return monitor.Run();
+}
diff --git a/src/event/TimeoutMonitor.hxx b/src/event/TimeoutMonitor.hxx
new file mode 100644
index 000000000..6914bcb61
--- /dev/null
+++ b/src/event/TimeoutMonitor.hxx
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_SOCKET_TIMEOUT_MONITOR_HXX
+#define MPD_SOCKET_TIMEOUT_MONITOR_HXX
+
+#include "check.h"
+
+#include <glib.h>
+
+class EventLoop;
+
+class TimeoutMonitor {
+ EventLoop &loop;
+ GSource *source;
+
+public:
+ TimeoutMonitor(EventLoop &_loop)
+ :loop(_loop), source(nullptr) {}
+
+ ~TimeoutMonitor() {
+ Cancel();
+ }
+
+ bool IsActive() const {
+ return source != nullptr;
+ }
+
+ void Schedule(unsigned ms);
+ void ScheduleSeconds(unsigned s);
+ void Cancel();
+
+protected:
+ /**
+ * @return true reschedules the timeout again
+ */
+ virtual bool OnTimeout() = 0;
+
+private:
+ bool Run();
+ static gboolean Callback(gpointer data);
+};
+
+#endif /* MAIN_NOTIFY_H */
diff --git a/src/event/WakeFD.cxx b/src/event/WakeFD.cxx
new file mode 100644
index 000000000..1a84f5645
--- /dev/null
+++ b/src/event/WakeFD.cxx
@@ -0,0 +1,225 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "WakeFD.hxx"
+#include "fd_util.h"
+#include "gcc.h"
+
+#include <unistd.h>
+
+#ifdef WIN32
+#include <ws2tcpip.h>
+#include <winsock2.h>
+#include <cstring> /* for memset() */
+#endif
+
+#ifdef HAVE_EVENTFD
+#include <sys/eventfd.h>
+#endif
+
+#ifdef WIN32
+static bool PoorSocketPair(int fd[2]);
+#endif
+
+bool
+WakeFD::Create()
+{
+ assert(fds[0] == -1);
+ assert(fds[1] == -1);
+
+#ifdef WIN32
+ return PoorSocketPair(fds);
+#else
+#ifdef HAVE_EVENTFD
+ fds[0] = eventfd_cloexec_nonblock(0, 0);
+ if (fds[0] >= 0) {
+ fds[1] = -2;
+ return true;
+ }
+#endif
+ return pipe_cloexec_nonblock(fds) >= 0;
+#endif
+}
+
+void
+WakeFD::Destroy()
+{
+#ifdef WIN32
+ closesocket(fds[0]);
+ closesocket(fds[1]);
+#else
+ close(fds[0]);
+#ifdef HAVE_EVENTFD
+ if (!IsEventFD())
+#endif
+ close(fds[1]);
+#endif
+
+#ifndef NDEBUG
+ fds[0] = -1;
+ fds[1] = -1;
+#endif
+}
+
+bool
+WakeFD::Read()
+{
+ assert(fds[0] >= 0);
+
+#ifdef WIN32
+ assert(fds[1] >= 0);
+ char buffer[256];
+ return recv(fds[0], buffer, sizeof(buffer), 0) > 0;
+#else
+
+#ifdef HAVE_EVENTFD
+ if (IsEventFD()) {
+ eventfd_t value;
+ return read(fds[0], &value,
+ sizeof(value)) == (ssize_t)sizeof(value);
+ }
+#endif
+
+ assert(fds[1] >= 0);
+
+ char buffer[256];
+ return read(fds[0], buffer, sizeof(buffer)) > 0;
+#endif
+}
+
+void
+WakeFD::Write()
+{
+ assert(fds[0] >= 0);
+
+#ifdef WIN32
+ assert(fds[1] >= 0);
+
+ send(fds[1], "", 1, 0);
+#else
+
+#ifdef HAVE_EVENTFD
+ if (IsEventFD()) {
+ static constexpr eventfd_t value = 1;
+ gcc_unused ssize_t nbytes =
+ write(fds[0], &value, sizeof(value));
+ return;
+ }
+#endif
+
+ assert(fds[1] >= 0);
+
+ gcc_unused ssize_t nbytes = write(fds[1], "", 1);
+#endif
+}
+
+#ifdef WIN32
+
+static void SafeCloseSocket(SOCKET s)
+{
+ int error = WSAGetLastError();
+ closesocket(s);
+ WSASetLastError(error);
+}
+
+/* Our poor man's socketpair() implementation
+ * Due to limited protocol/address family support and primitive error handling
+ * it's better to keep this as a private implementation detail of WakeFD
+ * rather than wide-available API.
+ */
+static bool PoorSocketPair(int fd[2])
+{
+ assert (fd != nullptr);
+
+ SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (listen_socket == INVALID_SOCKET)
+ return false;
+
+ sockaddr_in address;
+ std::memset(&address, 0, sizeof(address));
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ int ret = bind(listen_socket,
+ reinterpret_cast<sockaddr*>(&address),
+ sizeof(address));
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ ret = listen(listen_socket, 1);
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ int address_len = sizeof(address);
+ ret = getsockname(listen_socket,
+ reinterpret_cast<sockaddr*>(&address),
+ &address_len);
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (socket0 == INVALID_SOCKET) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ ret = connect(socket0,
+ reinterpret_cast<sockaddr*>(&address),
+ sizeof(address));
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ SafeCloseSocket(socket0);
+ return false;
+ }
+
+ SOCKET socket1 = accept(listen_socket, nullptr, nullptr);
+ if (socket1 == INVALID_SOCKET) {
+ SafeCloseSocket(listen_socket);
+ SafeCloseSocket(socket0);
+ return false;
+ }
+
+ SafeCloseSocket(listen_socket);
+
+ u_long non_block = 1;
+ if (ioctlsocket(socket0, FIONBIO, &non_block) < 0
+ || ioctlsocket(socket1, FIONBIO, &non_block) < 0) {
+ SafeCloseSocket(socket0);
+ SafeCloseSocket(socket1);
+ return false;
+ }
+
+ fd[0] = static_cast<int>(socket0);
+ fd[1] = static_cast<int>(socket1);
+
+ return true;
+}
+
+#endif
diff --git a/src/event/WakeFD.hxx b/src/event/WakeFD.hxx
new file mode 100644
index 000000000..15b66b4cf
--- /dev/null
+++ b/src/event/WakeFD.hxx
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_WAKE_FD_HXX
+#define MPD_WAKE_FD_HXX
+
+#include "check.h"
+
+#include <assert.h>
+
+/**
+ * This class can be used to wake up an I/O event loop.
+ *
+ * For optimization purposes, this class does not have a constructor
+ * or a destructor.
+ */
+class WakeFD {
+ int fds[2];
+
+public:
+#ifdef NDEBUG
+ WakeFD() = default;
+#else
+ WakeFD():fds{-1, -1} {};
+#endif
+
+ WakeFD(const WakeFD &other) = delete;
+ WakeFD &operator=(const WakeFD &other) = delete;
+
+ bool Create();
+ void Destroy();
+
+ int Get() const {
+ assert(fds[0] >= 0);
+#ifndef HAVE_EVENTFD
+ assert(fds[1] >= 0);
+#endif
+
+ return fds[0];
+ }
+
+ /**
+ * Checks if Write() was called at least once since the last
+ * Read() call.
+ */
+ bool Read();
+
+ /**
+ * Wakes up the reader. Multiple calls to this function will
+ * be combined to one wakeup.
+ */
+ void Write();
+
+private:
+#ifdef HAVE_EVENTFD
+ bool IsEventFD() {
+ assert(fds[0] >= 0);
+
+ return fds[1] == -2;
+ }
+#endif
+};
+
+#endif /* MAIN_NOTIFY_H */