event/SocketMonitor: refactor to SocketEvent

Similar to commits 1686f4e857 and
30a5dd267b
This commit is contained in:
Max Kellermann
2020-10-14 14:24:16 +02:00
parent 4d68a12f03
commit 5a4055fb08
26 changed files with 282 additions and 261 deletions
+7 -9
View File
@@ -30,14 +30,14 @@
#include <sys/inotify.h>
bool
void
InotifySource::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
{
uint8_t buffer[4096];
static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
"inotify buffer too small");
auto ifd = GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetSocket().ToFileDescriptor();
ssize_t nbytes = ifd.Read(buffer, sizeof(buffer));
if (nbytes < 0)
FatalSystemError("Failed to read from inotify");
@@ -63,8 +63,6 @@ InotifySource::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
callback(event->wd, event->mask, name, callback_ctx);
p += sizeof(*event) + event->len;
}
return true;
}
static FileDescriptor
@@ -79,17 +77,17 @@ InotifyInit()
InotifySource::InotifySource(EventLoop &_loop,
mpd_inotify_callback_t _callback, void *_ctx)
:SocketMonitor(SocketDescriptor::FromFileDescriptor(InotifyInit()),
_loop),
:socket_event(_loop, BIND_THIS_METHOD(OnSocketReady),
SocketDescriptor::FromFileDescriptor(InotifyInit())),
callback(_callback), callback_ctx(_ctx)
{
ScheduleRead();
socket_event.ScheduleRead();
}
int
InotifySource::Add(const char *path_fs, unsigned mask)
{
auto ifd = GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetSocket().ToFileDescriptor();
int wd = inotify_add_watch(ifd.Get(), path_fs, mask);
if (wd < 0)
throw MakeErrno("inotify_add_watch() has failed");
@@ -100,7 +98,7 @@ InotifySource::Add(const char *path_fs, unsigned mask)
void
InotifySource::Remove(unsigned wd) noexcept
{
auto ifd = GetSocket().ToFileDescriptor();
auto ifd = socket_event.GetSocket().ToFileDescriptor();
int ret = inotify_rm_watch(ifd.Get(), wd);
if (ret < 0 && errno != EINVAL)
LogErrno(inotify_domain, "inotify_rm_watch() has failed");
+6 -4
View File
@@ -20,12 +20,14 @@
#ifndef MPD_INOTIFY_SOURCE_HXX
#define MPD_INOTIFY_SOURCE_HXX
#include "event/SocketMonitor.hxx"
#include "event/SocketEvent.hxx"
typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
const char *name, void *ctx);
class InotifySource final : private SocketMonitor {
class InotifySource final {
SocketEvent socket_event;
mpd_inotify_callback_t callback;
void *callback_ctx;
@@ -43,7 +45,7 @@ public:
mpd_inotify_callback_t callback, void *ctx);
~InotifySource() noexcept {
Close();
socket_event.Close();
}
/**
@@ -63,7 +65,7 @@ public:
void Remove(unsigned wd) noexcept;
private:
bool OnSocketReady(unsigned flags) noexcept override;
void OnSocketReady(unsigned flags) noexcept;
};
#endif