event/SocketMonitor: use class SocketDescriptor

This commit is contained in:
Max Kellermann
2017-08-10 18:25:22 +02:00
parent fcfc8bacc0
commit 492b20a89d
21 changed files with 80 additions and 83 deletions
+1 -1
View File
@@ -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();
}
+1 -1
View File
@@ -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) {
+1 -1
View File
@@ -29,7 +29,7 @@
EventLoop::EventLoop()
:SocketMonitor(*this)
{
SocketMonitor::Open(wake_fd.Get());
SocketMonitor::Open(SocketDescriptor(wake_fd.Get()));
SocketMonitor::Schedule(SocketMonitor::READ);
}
+3 -3
View File
@@ -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
+4 -3
View File
@@ -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);
}
+13 -20
View File
@@ -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,18 +291,16 @@ 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)
SocketDescriptor fd(_fd);
StaticSocketAddress address = fd.GetLocalAddress();
if (!address.IsDefined())
throw MakeSocketError("Failed to get socket address");
address.SetSize(address_length);
OneServerSocket &s = AddAddress(address);
s.SetFD(fd);
}
+2 -2
View File
@@ -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();
}
}
+13 -17
View File
@@ -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);
}
+7 -6
View File
@@ -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.