event/SocketMonitor: add methods Read(), Write()

This commit is contained in:
Max Kellermann
2013-01-30 10:39:17 +01:00
parent fe3f0332f7
commit 73f36858bb
4 changed files with 43 additions and 27 deletions

View File

@@ -26,11 +26,6 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#endif
BufferedSocket::~BufferedSocket() BufferedSocket::~BufferedSocket()
{ {
if (input != nullptr) if (input != nullptr)
@@ -40,15 +35,7 @@ BufferedSocket::~BufferedSocket()
BufferedSocket::ssize_t BufferedSocket::ssize_t
BufferedSocket::DirectWrite(const void *data, size_t length) BufferedSocket::DirectWrite(const void *data, size_t length)
{ {
int flags = 0; const auto nbytes = SocketMonitor::Write((const char *)data, length);
#ifdef MSG_NOSIGNAL
flags |= MSG_NOSIGNAL;
#endif
#ifdef MSG_DONTWAIT
flags |= MSG_DONTWAIT;
#endif
const auto nbytes = send(Get(), (const char *)data, length, flags);
if (gcc_unlikely(nbytes < 0)) { if (gcc_unlikely(nbytes < 0)) {
const auto code = GetSocketError(); const auto code = GetSocketError();
if (IsSocketErrorAgain(code)) if (IsSocketErrorAgain(code))
@@ -65,15 +52,10 @@ BufferedSocket::DirectWrite(const void *data, size_t length)
return nbytes; return nbytes;
} }
ssize_t BufferedSocket::ssize_t
BufferedSocket::DirectRead(void *data, size_t length) BufferedSocket::DirectRead(void *data, size_t length)
{ {
int flags = 0; const auto nbytes = SocketMonitor::Read((char *)data, length);
#ifdef MSG_DONTWAIT
flags |= MSG_DONTWAIT;
#endif
const auto nbytes = recv(Get(), (char *)data, length, flags);
if (gcc_likely(nbytes > 0)) if (gcc_likely(nbytes > 0))
return nbytes; return nbytes;

View File

@@ -25,16 +25,10 @@
#include "util/PeakBuffer.hxx" #include "util/PeakBuffer.hxx"
#include "gcc.h" #include "gcc.h"
#include <type_traits>
#include <stddef.h>
struct fifo_buffer; struct fifo_buffer;
class EventLoop; class EventLoop;
class BufferedSocket : private SocketMonitor { class BufferedSocket : private SocketMonitor {
typedef std::make_signed<size_t>::type ssize_t;
fifo_buffer *input; fifo_buffer *input;
PeakBuffer output; PeakBuffer output;

View File

@@ -25,6 +25,13 @@
#include <assert.h> #include <assert.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
/* /*
* GSource methods * GSource methods
* *
@@ -127,3 +134,28 @@ SocketMonitor::Close()
{ {
close_socket(Steal()); 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);
}

View File

@@ -24,7 +24,10 @@
#include <glib.h> #include <glib.h>
#include <type_traits>
#include <assert.h> #include <assert.h>
#include <stddef.h>
#ifdef WIN32 #ifdef WIN32
/* ERRORis a WIN32 macro that poisons our namespace; this is a /* 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 ERROR = G_IO_ERR;
static constexpr unsigned HANGUP = G_IO_HUP; static constexpr unsigned HANGUP = G_IO_HUP;
typedef std::make_signed<size_t>::type ssize_t;
SocketMonitor(EventLoop &_loop) SocketMonitor(EventLoop &_loop)
:fd(-1), loop(_loop), source(nullptr) {} :fd(-1), loop(_loop), source(nullptr) {}
@@ -106,6 +111,9 @@ public:
poll.events &= ~WRITE; poll.events &= ~WRITE;
} }
ssize_t Read(void *data, size_t length);
ssize_t Write(const void *data, size_t length);
protected: protected:
virtual void OnSocketReady(unsigned flags) = 0; virtual void OnSocketReady(unsigned flags) = 0;