diff --git a/src/event/BufferedSocket.cxx b/src/event/BufferedSocket.cxx index f84fe808b..b95519e49 100644 --- a/src/event/BufferedSocket.cxx +++ b/src/event/BufferedSocket.cxx @@ -26,11 +26,6 @@ #include #include -#ifndef WIN32 -#include -#include -#endif - BufferedSocket::~BufferedSocket() { if (input != nullptr) @@ -40,15 +35,7 @@ BufferedSocket::~BufferedSocket() BufferedSocket::ssize_t BufferedSocket::DirectWrite(const void *data, size_t length) { - int flags = 0; -#ifdef MSG_NOSIGNAL - flags |= MSG_NOSIGNAL; -#endif -#ifdef MSG_DONTWAIT - flags |= MSG_DONTWAIT; -#endif - - const auto nbytes = send(Get(), (const char *)data, length, flags); + const auto nbytes = SocketMonitor::Write((const char *)data, length); if (gcc_unlikely(nbytes < 0)) { const auto code = GetSocketError(); if (IsSocketErrorAgain(code)) @@ -65,15 +52,10 @@ BufferedSocket::DirectWrite(const void *data, size_t length) return nbytes; } -ssize_t +BufferedSocket::ssize_t BufferedSocket::DirectRead(void *data, size_t length) { - int flags = 0; -#ifdef MSG_DONTWAIT - flags |= MSG_DONTWAIT; -#endif - - const auto nbytes = recv(Get(), (char *)data, length, flags); + const auto nbytes = SocketMonitor::Read((char *)data, length); if (gcc_likely(nbytes > 0)) return nbytes; diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index 49b17c86f..6e55a5e19 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -25,16 +25,10 @@ #include "util/PeakBuffer.hxx" #include "gcc.h" -#include - -#include - struct fifo_buffer; class EventLoop; class BufferedSocket : private SocketMonitor { - typedef std::make_signed::type ssize_t; - fifo_buffer *input; PeakBuffer output; diff --git a/src/event/SocketMonitor.cxx b/src/event/SocketMonitor.cxx index 57364c883..bf9fcdc59 100644 --- a/src/event/SocketMonitor.cxx +++ b/src/event/SocketMonitor.cxx @@ -25,6 +25,13 @@ #include +#ifdef WIN32 +#include +#else +#include +#include +#endif + /* * GSource methods * @@ -127,3 +134,28 @@ SocketMonitor::Close() { close_socket(Steal()); } + +SocketMonitor::ssize_t +SocketMonitor::Read(void *data, size_t length) +{ + int flags = 0; +#ifdef MSG_DONTWAIT + flags |= MSG_DONTWAIT; +#endif + + return recv(Get(), (char *)data, length, flags); +} + +SocketMonitor::ssize_t +SocketMonitor::Write(const void *data, size_t length) +{ + int flags = 0; +#ifdef MSG_NOSIGNAL + flags |= MSG_NOSIGNAL; +#endif +#ifdef MSG_DONTWAIT + flags |= MSG_DONTWAIT; +#endif + + return send(Get(), (const char *)data, length, flags); +} diff --git a/src/event/SocketMonitor.hxx b/src/event/SocketMonitor.hxx index ddfc21a42..b8ded1967 100644 --- a/src/event/SocketMonitor.hxx +++ b/src/event/SocketMonitor.hxx @@ -24,7 +24,10 @@ #include +#include + #include +#include #ifdef WIN32 /* ERRORis a WIN32 macro that poisons our namespace; this is a @@ -54,6 +57,8 @@ public: static constexpr unsigned ERROR = G_IO_ERR; static constexpr unsigned HANGUP = G_IO_HUP; + typedef std::make_signed::type ssize_t; + SocketMonitor(EventLoop &_loop) :fd(-1), loop(_loop), source(nullptr) {} @@ -106,6 +111,9 @@ public: poll.events &= ~WRITE; } + ssize_t Read(void *data, size_t length); + ssize_t Write(const void *data, size_t length); + protected: virtual void OnSocketReady(unsigned flags) = 0;