diff --git a/src/event/WakeFD.hxx b/src/event/WakeFD.hxx index 4ed1a23b0..2a38b2008 100644 --- a/src/event/WakeFD.hxx +++ b/src/event/WakeFD.hxx @@ -38,10 +38,10 @@ class WakeFD { public: SocketDescriptor GetSocket() const noexcept { -#ifdef USE_EVENTFD - return SocketDescriptor::FromFileDescriptor(fd.Get()); +#ifdef _WIN32 + return fd.Get(); #else - return SocketDescriptor(fd.Get()); + return SocketDescriptor::FromFileDescriptor(fd.Get()); #endif } diff --git a/src/system/EventPipe.cxx b/src/system/EventPipe.cxx index 5738e7b3c..226c0f1a7 100644 --- a/src/system/EventPipe.cxx +++ b/src/system/EventPipe.cxx @@ -34,59 +34,39 @@ #endif #ifdef _WIN32 -static void PoorSocketPair(int fd[2]); +static void PoorSocketPair(UniqueSocketDescriptor &socket0, + UniqueSocketDescriptor &socket01); #endif EventPipe::EventPipe() { #ifdef _WIN32 - PoorSocketPair(fds); + PoorSocketPair(r, w); #else - FileDescriptor r, w; - if (!FileDescriptor::CreatePipeNonBlock(r, w)) + if (!UniqueFileDescriptor::CreatePipeNonBlock(r, w)) throw MakeErrno("pipe() has failed"); - - fds[0] = r.Steal(); - fds[1] = w.Steal(); #endif } -EventPipe::~EventPipe() noexcept -{ -#ifdef _WIN32 - closesocket(fds[0]); - closesocket(fds[1]); -#else - close(fds[0]); - close(fds[1]); -#endif -} +EventPipe::~EventPipe() noexcept = default; bool EventPipe::Read() noexcept { - assert(fds[0] >= 0); - assert(fds[1] >= 0); + assert(r.IsDefined()); + assert(w.IsDefined()); char buffer[256]; -#ifdef _WIN32 - return recv(fds[0], buffer, sizeof(buffer), 0) > 0; -#else - return read(fds[0], buffer, sizeof(buffer)) > 0; -#endif + return r.Read(buffer, sizeof(buffer)) > 0; } void EventPipe::Write() noexcept { - assert(fds[0] >= 0); - assert(fds[1] >= 0); + assert(r.IsDefined()); + assert(w.IsDefined()); -#ifdef _WIN32 - send(fds[1], "", 1, 0); -#else - [[maybe_unused]] ssize_t nbytes = write(fds[1], "", 1); -#endif + w.Write("", 1); } #ifdef _WIN32 @@ -97,10 +77,8 @@ EventPipe::Write() noexcept * rather than wide-available API. */ static void -PoorSocketPair(int fd[2]) +PoorSocketPair(UniqueSocketDescriptor &socket0, UniqueSocketDescriptor &socket1) { - assert (fd != nullptr); - UniqueSocketDescriptor listen_socket; if (!listen_socket.Create(AF_INET, SOCK_STREAM, IPPROTO_TCP)) throw MakeSocketError("Failed to create socket"); @@ -111,7 +89,6 @@ PoorSocketPair(int fd[2]) if (!listen_socket.Listen(1)) throw MakeSocketError("Failed to listen on socket"); - UniqueSocketDescriptor socket0; if (!socket0.Create(AF_INET, SOCK_STREAM, IPPROTO_TCP)) throw MakeSocketError("Failed to create socket"); @@ -120,12 +97,9 @@ PoorSocketPair(int fd[2]) socket0.SetNonBlocking(); - auto socket1 = listen_socket.AcceptNonBlock(); + socket1 = listen_socket.AcceptNonBlock(); if (!socket1.IsDefined()) throw MakeSocketError("Failed to accept connection"); - - fd[0] = socket0.Steal(); - fd[1] = socket1.Steal(); } #endif diff --git a/src/system/EventPipe.hxx b/src/system/EventPipe.hxx index 1387c01ee..1e6a5c020 100644 --- a/src/system/EventPipe.hxx +++ b/src/system/EventPipe.hxx @@ -20,13 +20,23 @@ #ifndef MPD_EVENT_PIPE_HXX #define MPD_EVENT_PIPE_HXX +#ifdef _WIN32 +#include "net/UniqueSocketDescriptor.hxx" +#else +#include "io/UniqueFileDescriptor.hxx" +#endif + /** * A pipe that can be used to trigger an event to the read side. * * Errors in the constructor are fatal. */ class EventPipe { - int fds[2]; +#ifdef _WIN32 + UniqueSocketDescriptor r, w; +#else + UniqueFileDescriptor r, w; +#endif public: /** @@ -39,9 +49,15 @@ public: EventPipe(const EventPipe &other) = delete; EventPipe &operator=(const EventPipe &other) = delete; - int Get() const noexcept { - return fds[0]; +#ifdef _WIN32 + SocketDescriptor Get() const noexcept { + return r; } +#else + FileDescriptor Get() const noexcept { + return r; + } +#endif /** * Checks if Write() was called at least once since the last