event/PipeEvent: new class wrapping SocketEvent

This commit is contained in:
Max Kellermann
2021-10-13 10:40:48 +02:00
parent be8ed2f59e
commit 4dae8b41da
4 changed files with 112 additions and 9 deletions

View File

@@ -37,7 +37,7 @@ InotifySource::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
"inotify buffer too small");
auto ifd = socket_event.GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetFileDescriptor();
ssize_t nbytes = ifd.Read(buffer, sizeof(buffer));
if (nbytes < 0)
FatalSystemError("Failed to read from inotify");
@@ -78,7 +78,7 @@ InotifyInit()
InotifySource::InotifySource(EventLoop &_loop,
mpd_inotify_callback_t _callback, void *_ctx)
:socket_event(_loop, BIND_THIS_METHOD(OnSocketReady),
SocketDescriptor::FromFileDescriptor(InotifyInit())),
InotifyInit()),
callback(_callback), callback_ctx(_ctx)
{
socket_event.ScheduleRead();
@@ -87,7 +87,7 @@ InotifySource::InotifySource(EventLoop &_loop,
int
InotifySource::Add(const char *path_fs, unsigned mask)
{
auto ifd = socket_event.GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetFileDescriptor();
int wd = inotify_add_watch(ifd.Get(), path_fs, mask);
if (wd < 0)
throw MakeErrno("inotify_add_watch() has failed");
@@ -98,7 +98,7 @@ InotifySource::Add(const char *path_fs, unsigned mask)
void
InotifySource::Remove(unsigned wd) noexcept
{
auto ifd = socket_event.GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetFileDescriptor();
int ret = inotify_rm_watch(ifd.Get(), wd);
if (ret < 0 && errno != EINVAL)
LogErrno(inotify_domain, "inotify_rm_watch() has failed");

View File

@@ -20,13 +20,13 @@
#ifndef MPD_INOTIFY_SOURCE_HXX
#define MPD_INOTIFY_SOURCE_HXX
#include "event/SocketEvent.hxx"
#include "event/PipeEvent.hxx"
typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
const char *name, void *ctx);
class InotifySource final {
SocketEvent socket_event;
PipeEvent socket_event;
mpd_inotify_callback_t callback;
void *callback_ctx;