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
+19 -12
View File
@@ -29,7 +29,7 @@
#include "net/Resolver.hxx"
#include "net/AddressInfo.hxx"
#include "net/ToString.hxx"
#include "event/SocketMonitor.hxx"
#include "event/SocketEvent.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "util/Domain.hxx"
@@ -43,9 +43,11 @@
#include <sys/stat.h>
#endif
class ServerSocket::OneServerSocket final : private SocketMonitor {
class ServerSocket::OneServerSocket final {
ServerSocket &parent;
SocketEvent event;
const unsigned serial;
#ifdef HAVE_UN
@@ -59,8 +61,9 @@ public:
OneServerSocket(EventLoop &_loop, ServerSocket &_parent,
unsigned _serial,
A &&_address) noexcept
:SocketMonitor(_loop),
parent(_parent), serial(_serial),
:parent(_parent),
event(_loop, BIND_THIS_METHOD(OnSocketReady)),
serial(_serial),
#ifdef HAVE_UN
path(nullptr),
#endif
@@ -88,10 +91,15 @@ public:
}
#endif
bool IsDefined() const noexcept {
return event.IsDefined();
}
void Open();
using SocketMonitor::IsDefined;
using SocketMonitor::Close;
void Close() noexcept {
event.Close();
}
[[nodiscard]] gcc_pure
std::string ToString() const noexcept {
@@ -99,14 +107,14 @@ public:
}
void SetFD(UniqueSocketDescriptor _fd) noexcept {
SocketMonitor::Open(_fd.Release());
SocketMonitor::ScheduleRead();
event.Open(_fd.Release());
event.ScheduleRead();
}
void Accept() noexcept;
private:
bool OnSocketReady(unsigned flags) noexcept override;
void OnSocketReady(unsigned flags) noexcept;
};
static constexpr Domain server_socket_domain("server_socket");
@@ -140,7 +148,7 @@ inline void
ServerSocket::OneServerSocket::Accept() noexcept
{
StaticSocketAddress peer_address;
UniqueSocketDescriptor peer_fd(GetSocket().AcceptNonBlock(peer_address));
UniqueSocketDescriptor peer_fd(event.GetSocket().AcceptNonBlock(peer_address));
if (!peer_fd.IsDefined()) {
const SocketErrorMessage msg;
FormatError(server_socket_domain,
@@ -160,11 +168,10 @@ ServerSocket::OneServerSocket::Accept() noexcept
parent.OnAccept(std::move(peer_fd), peer_address, uid);
}
bool
void
ServerSocket::OneServerSocket::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
{
Accept();
return true;
}
inline void