event/SocketMonitor: add methods Read(), Write()
This commit is contained in:
parent
fe3f0332f7
commit
73f36858bb
|
@ -26,11 +26,6 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#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;
|
||||
|
||||
|
|
|
@ -25,16 +25,10 @@
|
|||
#include "util/PeakBuffer.hxx"
|
||||
#include "gcc.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct fifo_buffer;
|
||||
class EventLoop;
|
||||
|
||||
class BufferedSocket : private SocketMonitor {
|
||||
typedef std::make_signed<size_t>::type ssize_t;
|
||||
|
||||
fifo_buffer *input;
|
||||
PeakBuffer output;
|
||||
|
||||
|
|
|
@ -25,6 +25,13 @@
|
|||
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,10 @@
|
|||
|
||||
#include <glib.h>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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<size_t>::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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue