InotifySource: use the SocketMonitor class
This commit is contained in:
parent
2101daef5a
commit
7071126770
@ -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
|
||||
|
@ -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));
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user