diff options
author | Max Kellermann <max@duempel.org> | 2013-01-15 18:18:02 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-15 18:18:34 +0100 |
commit | 707112677065dd59f638764bb9dbd64d200b8a85 (patch) | |
tree | 10b5f2461c43fa5f847df23abccd9ada00e6b237 | |
parent | 2101daef5a939d41a306fcccf2d94992f82723e2 (diff) | |
download | mpd-707112677065dd59f638764bb9dbd64d200b8a85.tar.gz mpd-707112677065dd59f638764bb9dbd64d200b8a85.tar.xz mpd-707112677065dd59f638764bb9dbd64d200b8a85.zip |
InotifySource: use the SocketMonitor class
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/InotifySource.cxx | 38 | ||||
-rw-r--r-- | src/InotifySource.hxx | 23 | ||||
-rw-r--r-- | src/InotifyUpdate.cxx | 3 | ||||
-rw-r--r-- | test/run_inotify.cxx | 7 |
5 files changed, 29 insertions, 43 deletions
diff --git a/Makefile.am b/Makefile.am index a17346844..d4f4fdc62 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1335,6 +1335,7 @@ test_run_inotify_SOURCES = test/run_inotify.cxx \ src/fd_util.c \ src/InotifySource.cxx test_run_inotify_LDADD = \ + libevent.a \ libutil.a \ $(GLIB_LIBS) endif diff --git a/src/InotifySource.cxx b/src/InotifySource.cxx index 37c96536f..5da32c9bd 100644 --- a/src/InotifySource.cxx +++ b/src/InotifySource.cxx @@ -41,8 +41,8 @@ mpd_inotify_quark(void) return g_quark_from_static_string("inotify"); } -inline void -InotifySource::InEvent() +void +InotifySource::OnSocketReady(gcc_unused unsigned flags) { void *dest; size_t length; @@ -52,7 +52,7 @@ InotifySource::InEvent() if (dest == NULL) MPD_ERROR("buffer full"); - nbytes = read(fd, dest, length); + nbytes = read(Get(), dest, length); if (nbytes < 0) MPD_ERROR("failed to read from inotify: %s", g_strerror(errno)); @@ -81,28 +81,21 @@ InotifySource::InEvent() } } -gboolean -InotifySource::InEvent(G_GNUC_UNUSED GIOChannel *_source, - G_GNUC_UNUSED GIOCondition condition, - gpointer data) -{ - InotifySource &source = *(InotifySource *)data; - source.InEvent(); - return true; -} - inline -InotifySource::InotifySource(mpd_inotify_callback_t _callback, void *_ctx, +InotifySource::InotifySource(EventLoop &_loop, + mpd_inotify_callback_t _callback, void *_ctx, int _fd) - :callback(_callback), callback_ctx(_ctx), fd(_fd), - channel(g_io_channel_unix_new(fd)), - id(g_io_add_watch(channel, G_IO_IN, InEvent, this)), + :SocketMonitor(_fd, _loop), + callback(_callback), callback_ctx(_ctx), buffer(fifo_buffer_new(4096)) { + ScheduleRead(); + } InotifySource * -InotifySource::Create(mpd_inotify_callback_t callback, void *callback_ctx, +InotifySource::Create(EventLoop &loop, + mpd_inotify_callback_t callback, void *callback_ctx, GError **error_r) { int fd = inotify_init_cloexec(); @@ -113,21 +106,18 @@ InotifySource::Create(mpd_inotify_callback_t callback, void *callback_ctx, return NULL; } - return new InotifySource(callback, callback_ctx, fd); + return new InotifySource(loop, callback, callback_ctx, fd); } InotifySource::~InotifySource() { - g_source_remove(id); - g_io_channel_unref(channel); fifo_buffer_free(buffer); - close(fd); } int InotifySource::Add(const char *path_fs, unsigned mask, GError **error_r) { - int wd = inotify_add_watch(fd, path_fs, mask); + int wd = inotify_add_watch(Get(), path_fs, mask); if (wd < 0) g_set_error(error_r, mpd_inotify_quark(), errno, "inotify_add_watch() has failed: %s", @@ -139,7 +129,7 @@ InotifySource::Add(const char *path_fs, unsigned mask, GError **error_r) void InotifySource::Remove(unsigned wd) { - int ret = inotify_rm_watch(fd, wd); + int ret = inotify_rm_watch(Get(), wd); if (ret < 0 && errno != EINVAL) g_warning("inotify_rm_watch() has failed: %s", g_strerror(errno)); diff --git a/src/InotifySource.hxx b/src/InotifySource.hxx index 6299d1e20..0f29ae754 100644 --- a/src/InotifySource.hxx +++ b/src/InotifySource.hxx @@ -20,29 +20,23 @@ #ifndef MPD_INOTIFY_SOURCE_HXX #define MPD_INOTIFY_SOURCE_HXX +#include "event/SocketMonitor.hxx" #include "gerror.h" +#include "gcc.h" #include <glib.h> typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, const char *name, void *ctx); -class InotifySource { +class InotifySource final : private SocketMonitor { mpd_inotify_callback_t callback; void *callback_ctx; - int fd; - - GIOChannel *channel; - - /** - * The channel's source id in the GLib main loop. - */ - guint id; - struct fifo_buffer *buffer; - InotifySource(mpd_inotify_callback_t callback, void *ctx, int fd); + InotifySource(EventLoop &_loop, + mpd_inotify_callback_t callback, void *ctx, int fd); public: /** @@ -51,7 +45,8 @@ public: * * @param a callback invoked for events received from the kernel */ - static InotifySource *Create(mpd_inotify_callback_t callback, + static InotifySource *Create(EventLoop &_loop, + mpd_inotify_callback_t callback, void *ctx, GError **error_r); @@ -73,9 +68,7 @@ public: void Remove(unsigned wd); private: - void InEvent(); - static gboolean InEvent(GIOChannel *source, GIOCondition condition, - gpointer data); + virtual void OnSocketReady(unsigned flags) override; }; #endif diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx index 0fe22385b..7ed0e84ba 100644 --- a/src/InotifyUpdate.cxx +++ b/src/InotifyUpdate.cxx @@ -318,7 +318,8 @@ mpd_inotify_init(unsigned max_depth) return; } - inotify_source = InotifySource::Create(mpd_inotify_callback, nullptr, + inotify_source = InotifySource::Create(*main_loop, + mpd_inotify_callback, nullptr, &error); if (inotify_source == NULL) { g_warning("%s", error->message); diff --git a/test/run_inotify.cxx b/test/run_inotify.cxx index 92790a3a3..29fcf70ab 100644 --- a/test/run_inotify.cxx +++ b/test/run_inotify.cxx @@ -61,7 +61,10 @@ int main(int argc, char **argv) path = argv[1]; - InotifySource *source = InotifySource::Create(my_inotify_callback, + event_loop = new EventLoop(EventLoop::Default()); + + InotifySource *source = InotifySource::Create(*event_loop, + my_inotify_callback, nullptr, &error); if (source == NULL) { g_warning("%s", error->message); @@ -77,8 +80,6 @@ int main(int argc, char **argv) return 2; } - event_loop = new EventLoop(EventLoop::Default()); - struct sigaction sa; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); |