diff options
author | Max Kellermann <max@musicpd.org> | 2017-08-10 18:25:22 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2017-08-10 19:19:58 +0200 |
commit | 492b20a89d98739d0020f0c8c449ce8778405fc4 (patch) | |
tree | bdd11a6b1c5a1eb63e256931a2b20d076ca47760 /src/event | |
parent | fcfc8bacc008ce04aa75e50cb33825648bba917f (diff) |
event/SocketMonitor: use class SocketDescriptor
Diffstat (limited to 'src/event')
-rw-r--r-- | src/event/BufferedSocket.hxx | 2 | ||||
-rw-r--r-- | src/event/FullyBufferedSocket.hxx | 2 | ||||
-rw-r--r-- | src/event/Loop.cxx | 2 | ||||
-rw-r--r-- | src/event/MultiSocketMonitor.cxx | 6 | ||||
-rw-r--r-- | src/event/MultiSocketMonitor.hxx | 7 | ||||
-rw-r--r-- | src/event/ServerSocket.cxx | 33 | ||||
-rw-r--r-- | src/event/SignalMonitor.cxx | 4 | ||||
-rw-r--r-- | src/event/SocketMonitor.cxx | 30 | ||||
-rw-r--r-- | src/event/SocketMonitor.hxx | 13 |
9 files changed, 45 insertions, 54 deletions
diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index 4b77ece15..849229960 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -38,7 +38,7 @@ class BufferedSocket : protected SocketMonitor { StaticFifoBuffer<uint8_t, 8192> input; public: - BufferedSocket(int _fd, EventLoop &_loop) + BufferedSocket(SocketDescriptor _fd, EventLoop &_loop) :SocketMonitor(_fd, _loop) { ScheduleRead(); } diff --git a/src/event/FullyBufferedSocket.hxx b/src/event/FullyBufferedSocket.hxx index 1979556dc..71fcb0c36 100644 --- a/src/event/FullyBufferedSocket.hxx +++ b/src/event/FullyBufferedSocket.hxx @@ -32,7 +32,7 @@ class FullyBufferedSocket : protected BufferedSocket, private IdleMonitor { PeakBuffer output; public: - FullyBufferedSocket(int _fd, EventLoop &_loop, + FullyBufferedSocket(SocketDescriptor _fd, EventLoop &_loop, size_t normal_size, size_t peak_size=0) :BufferedSocket(_fd, _loop), IdleMonitor(_loop), output(normal_size, peak_size) { diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index fab614d73..93fdd0b1f 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -29,7 +29,7 @@ EventLoop::EventLoop() :SocketMonitor(*this) { - SocketMonitor::Open(wake_fd.Get()); + SocketMonitor::Open(SocketDescriptor(wake_fd.Get())); SocketMonitor::Schedule(SocketMonitor::READ); } diff --git a/src/event/MultiSocketMonitor.cxx b/src/event/MultiSocketMonitor.cxx index 02a51e96f..f36c9163a 100644 --- a/src/event/MultiSocketMonitor.cxx +++ b/src/event/MultiSocketMonitor.cxx @@ -57,9 +57,9 @@ MultiSocketMonitor::ReplaceSocketList(pollfd *pfds, unsigned n) { pollfd *const end = pfds + n; - UpdateSocketList([pfds, end](int fd) -> unsigned { + UpdateSocketList([pfds, end](SocketDescriptor fd) -> unsigned { auto i = std::find_if(pfds, end, [fd](const struct pollfd &pfd){ - return pfd.fd == fd; + return pfd.fd == fd.Get(); }); if (i == end) return 0; @@ -71,7 +71,7 @@ MultiSocketMonitor::ReplaceSocketList(pollfd *pfds, unsigned n) for (auto i = pfds; i != end; ++i) if (i->events != 0) - AddSocket(i->fd, i->events); + AddSocket(SocketDescriptor(i->fd), i->events); } #endif diff --git a/src/event/MultiSocketMonitor.hxx b/src/event/MultiSocketMonitor.hxx index db6bec3cb..0b02b3d48 100644 --- a/src/event/MultiSocketMonitor.hxx +++ b/src/event/MultiSocketMonitor.hxx @@ -59,13 +59,14 @@ class MultiSocketMonitor : IdleMonitor, TimeoutMonitor unsigned revents; public: - SingleFD(MultiSocketMonitor &_multi, int _fd, unsigned events) + SingleFD(MultiSocketMonitor &_multi, SocketDescriptor _fd, + unsigned events) :SocketMonitor(_fd, _multi.GetEventLoop()), multi(_multi), revents(0) { Schedule(events); } - int GetFD() const { + SocketDescriptor GetFD() const { return SocketMonitor::Get(); } @@ -153,7 +154,7 @@ public: * * May only be called from PrepareSockets(). */ - void AddSocket(int fd, unsigned events) { + void AddSocket(SocketDescriptor fd, unsigned events) { fds.emplace_front(*this, fd, events); } diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index 2890d4772..3d3716f10 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -108,7 +108,7 @@ public: return ::ToString(address); } - void SetFD(int _fd) noexcept { + void SetFD(SocketDescriptor _fd) noexcept { SocketMonitor::Open(_fd); SocketMonitor::ScheduleRead(); } @@ -150,28 +150,23 @@ inline void OneServerSocket::Accept() noexcept { StaticSocketAddress peer_address; - size_t peer_address_length = sizeof(peer_address); - int peer_fd = - accept_cloexec_nonblock(Get(), peer_address.GetAddress(), - &peer_address_length); - if (peer_fd < 0) { + auto peer_fd = Get().AcceptNonBlock(peer_address); + if (!peer_fd.IsDefined()) { const SocketErrorMessage msg; FormatError(server_socket_domain, "accept() failed: %s", (const char *)msg); return; } - peer_address.SetSize(peer_address_length); - - if (socket_keepalive(peer_fd)) { + if (socket_keepalive(peer_fd.Get())) { const SocketErrorMessage msg; FormatError(server_socket_domain, "Could not set TCP keepalive option: %s", (const char *)msg); } - parent.OnAccept(peer_fd, peer_address, - get_remote_uid(peer_fd)); + parent.OnAccept(peer_fd.Get(), peer_address, + get_remote_uid(peer_fd.Get())); } bool @@ -199,7 +194,7 @@ OneServerSocket::Open() /* register in the EventLoop */ - SetFD(_fd.Steal()); + SetFD(_fd.Release()); } ServerSocket::ServerSocket(EventLoop &_loop) @@ -296,17 +291,15 @@ ServerSocket::AddAddress(AllocatedSocketAddress &&address) } void -ServerSocket::AddFD(int fd) +ServerSocket::AddFD(int _fd) { - assert(fd >= 0); + assert(_fd >= 0); - StaticSocketAddress address; - socklen_t address_length = sizeof(address); - if (getsockname(fd, address.GetAddress(), - &address_length) < 0) - throw MakeSocketError("Failed to get socket address"); + SocketDescriptor fd(_fd); - address.SetSize(address_length); + StaticSocketAddress address = fd.GetLocalAddress(); + if (!address.IsDefined()) + throw MakeSocketError("Failed to get socket address"); OneServerSocket &s = AddAddress(address); s.SetFD(fd); diff --git a/src/event/SignalMonitor.cxx b/src/event/SignalMonitor.cxx index 2fd101c86..06b700118 100644 --- a/src/event/SignalMonitor.cxx +++ b/src/event/SignalMonitor.cxx @@ -56,7 +56,7 @@ public: SignalMonitor(EventLoop &_loop) :SocketMonitor(_loop) { #ifndef USE_SIGNALFD - SocketMonitor::Open(fd.Get()); + SocketMonitor::Open(SocketDescriptor(fd.Get())); SocketMonitor::ScheduleRead(); #endif } @@ -70,7 +70,7 @@ public: fd.Create(mask); if (!was_open) { - SocketMonitor::Open(fd.Get()); + SocketMonitor::Open(SocketDescriptor(fd.Get())); SocketMonitor::ScheduleRead(); } } diff --git a/src/event/SocketMonitor.cxx b/src/event/SocketMonitor.cxx index a1daa07cc..66cf0c532 100644 --- a/src/event/SocketMonitor.cxx +++ b/src/event/SocketMonitor.cxx @@ -46,25 +46,22 @@ SocketMonitor::~SocketMonitor() } void -SocketMonitor::Open(int _fd) +SocketMonitor::Open(SocketDescriptor _fd) { - assert(fd < 0); - assert(_fd >= 0); + assert(!fd.IsDefined()); + assert(_fd.IsDefined()); fd = _fd; } -int +SocketDescriptor SocketMonitor::Steal() { assert(IsDefined()); Cancel(); - int result = fd; - fd = -1; - - return result; + return std::exchange(fd, SocketDescriptor::Undefined()); } void @@ -72,15 +69,14 @@ SocketMonitor::Abandon() { assert(IsDefined()); - int old_fd = fd; - fd = -1; - loop.Abandon(old_fd, *this); + loop.Abandon(std::exchange(fd, SocketDescriptor::Undefined()).Get(), + *this); } void SocketMonitor::Close() { - close_socket(Steal()); + Steal().Close(); } void @@ -92,11 +88,11 @@ SocketMonitor::Schedule(unsigned flags) return; if (scheduled_flags == 0) - loop.AddFD(fd, flags, *this); + loop.AddFD(fd.Get(), flags, *this); else if (flags == 0) - loop.RemoveFD(fd, *this); + loop.RemoveFD(fd.Get(), *this); else - loop.ModifyFD(fd, flags, *this); + loop.ModifyFD(fd.Get(), flags, *this); scheduled_flags = flags; } @@ -111,7 +107,7 @@ SocketMonitor::Read(void *data, size_t length) flags |= MSG_DONTWAIT; #endif - return recv(Get(), (char *)data, length, flags); + return recv(Get().Get(), (char *)data, length, flags); } SocketMonitor::ssize_t @@ -127,5 +123,5 @@ SocketMonitor::Write(const void *data, size_t length) flags |= MSG_DONTWAIT; #endif - return send(Get(), (const char *)data, length, flags); + return send(Get().Get(), (const char *)data, length, flags); } diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx index 989da2cfe..941bf19c0 100644 --- a/src/event/SocketMonitor.hxx +++ b/src/event/SocketMonitor.hxx @@ -22,6 +22,7 @@ #include "check.h" #include "PollGroup.hxx" +#include "net/SocketDescriptor.hxx" #include <type_traits> @@ -52,7 +53,7 @@ class EventLoop; * as thread-safe. */ class SocketMonitor { - int fd; + SocketDescriptor fd; EventLoop &loop; /** @@ -71,7 +72,7 @@ public: SocketMonitor(EventLoop &_loop) :fd(-1), loop(_loop), scheduled_flags(0) {} - SocketMonitor(int _fd, EventLoop &_loop) + SocketMonitor(SocketDescriptor _fd, EventLoop &_loop) :fd(_fd), loop(_loop), scheduled_flags(0) {} ~SocketMonitor(); @@ -81,22 +82,22 @@ public: } bool IsDefined() const { - return fd >= 0; + return fd.IsDefined(); } - int Get() const { + SocketDescriptor Get() const { assert(IsDefined()); return fd; } - void Open(int _fd); + void Open(SocketDescriptor _fd); /** * "Steal" the socket descriptor. This abandons the socket * and returns it. */ - int Steal(); + SocketDescriptor Steal(); /** * Somebody has closed the socket. Unregister this object. |