event/SocketMonitor: refactor to SocketEvent
Similar to commits1686f4e857and30a5dd267b
This commit is contained in:
@@ -42,7 +42,7 @@
|
||||
#include "util/ScopeExit.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
#include "protocol/Ack.hxx"
|
||||
#include "event/SocketMonitor.hxx"
|
||||
#include "event/SocketEvent.hxx"
|
||||
#include "event/IdleEvent.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
@@ -85,7 +85,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ProxyDatabase final : public Database, SocketMonitor {
|
||||
class ProxyDatabase final : public Database {
|
||||
SocketEvent socket_event;
|
||||
IdleEvent idle_event;
|
||||
|
||||
DatabaseListener &listener;
|
||||
@@ -149,10 +150,8 @@ private:
|
||||
|
||||
void Disconnect() noexcept;
|
||||
|
||||
void OnSocketReady(unsigned flags) noexcept;
|
||||
void OnIdle() noexcept;
|
||||
|
||||
/* virtual methods from SocketMonitor */
|
||||
bool OnSocketReady(unsigned flags) noexcept override;
|
||||
};
|
||||
|
||||
static constexpr struct {
|
||||
@@ -446,7 +445,7 @@ SendGroup(mpd_connection *connection, ConstBuffer<TagType> group)
|
||||
ProxyDatabase::ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener,
|
||||
const ConfigBlock &block)
|
||||
:Database(proxy_db_plugin),
|
||||
SocketMonitor(_loop),
|
||||
socket_event(_loop, BIND_THIS_METHOD(OnSocketReady)),
|
||||
idle_event(_loop, BIND_THIS_METHOD(OnIdle)),
|
||||
listener(_listener),
|
||||
host(block.GetBlockValue("host", "")),
|
||||
@@ -527,7 +526,7 @@ ProxyDatabase::Connect()
|
||||
idle_received = ~0U;
|
||||
is_idle = false;
|
||||
|
||||
SocketMonitor::Open(SocketDescriptor(mpd_async_get_fd(mpd_connection_get_async(connection))));
|
||||
socket_event.Open(SocketDescriptor(mpd_async_get_fd(mpd_connection_get_async(connection))));
|
||||
idle_event.Schedule();
|
||||
}
|
||||
|
||||
@@ -574,13 +573,13 @@ ProxyDatabase::Disconnect() noexcept
|
||||
assert(connection != nullptr);
|
||||
|
||||
idle_event.Cancel();
|
||||
SocketMonitor::Steal();
|
||||
socket_event.Steal();
|
||||
|
||||
mpd_connection_free(connection);
|
||||
connection = nullptr;
|
||||
}
|
||||
|
||||
bool
|
||||
void
|
||||
ProxyDatabase::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
||||
{
|
||||
assert(connection != nullptr);
|
||||
@@ -588,8 +587,8 @@ ProxyDatabase::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
||||
if (!is_idle) {
|
||||
// TODO: can this happen?
|
||||
idle_event.Schedule();
|
||||
SocketMonitor::Cancel();
|
||||
return true;
|
||||
socket_event.Cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
auto idle = (unsigned)mpd_recv_idle(connection, false);
|
||||
@@ -599,7 +598,7 @@ ProxyDatabase::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
||||
} catch (...) {
|
||||
LogError(std::current_exception());
|
||||
Disconnect();
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,8 +606,7 @@ ProxyDatabase::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
||||
idle_received |= idle;
|
||||
is_idle = false;
|
||||
idle_event.Schedule();
|
||||
SocketMonitor::Cancel();
|
||||
return true;
|
||||
socket_event.Cancel();
|
||||
}
|
||||
|
||||
void
|
||||
@@ -636,14 +634,14 @@ ProxyDatabase::OnIdle() noexcept
|
||||
LogError(std::current_exception());
|
||||
}
|
||||
|
||||
SocketMonitor::Steal();
|
||||
socket_event.Steal();
|
||||
mpd_connection_free(connection);
|
||||
connection = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
is_idle = true;
|
||||
SocketMonitor::ScheduleRead();
|
||||
socket_event.ScheduleRead();
|
||||
}
|
||||
|
||||
const LightSong *
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user