net: add RAII socket and use it instead of bare SOCKET

Introduce a class to manage the lifetime of a socket - when the object
that contains the socket goes out of scope, the underlying socket will
be closed.

In addition, the new `Sock` class has a `Send()`, `Recv()` and `Wait()`
methods that can be overridden by unit tests to mock the socket
operations.

The `Wait()` method also hides the
`#ifdef USE_POLL poll() #else select() #endif` technique from higher
level code.
This commit is contained in:
Vasil Dimov
2020-12-23 16:40:11 +01:00
parent dec9b5e850
commit ba9d73268f
7 changed files with 250 additions and 41 deletions

View File

@@ -6,12 +6,97 @@
#include <logging.h>
#include <tinyformat.h>
#include <util/sock.h>
#include <util/system.h>
#include <util/time.h>
#include <codecvt>
#include <cwchar>
#include <locale>
#include <string>
#ifdef USE_POLL
#include <poll.h>
#endif
Sock::Sock() : m_socket(INVALID_SOCKET) {}
Sock::Sock(SOCKET s) : m_socket(s) {}
Sock::Sock(Sock&& other)
{
m_socket = other.m_socket;
other.m_socket = INVALID_SOCKET;
}
Sock::~Sock() { Reset(); }
Sock& Sock::operator=(Sock&& other)
{
Reset();
m_socket = other.m_socket;
other.m_socket = INVALID_SOCKET;
return *this;
}
SOCKET Sock::Get() const { return m_socket; }
SOCKET Sock::Release()
{
const SOCKET s = m_socket;
m_socket = INVALID_SOCKET;
return s;
}
void Sock::Reset() { CloseSocket(m_socket); }
ssize_t Sock::Send(const void* data, size_t len, int flags) const
{
return send(m_socket, static_cast<const char*>(data), len, flags);
}
ssize_t Sock::Recv(void* buf, size_t len, int flags) const
{
return recv(m_socket, static_cast<char*>(buf), len, flags);
}
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested) const
{
#ifdef USE_POLL
pollfd fd;
fd.fd = m_socket;
fd.events = 0;
if (requested & RECV) {
fd.events |= POLLIN;
}
if (requested & SEND) {
fd.events |= POLLOUT;
}
return poll(&fd, 1, count_milliseconds(timeout)) != SOCKET_ERROR;
#else
if (!IsSelectableSocket(m_socket)) {
return false;
}
fd_set fdset_recv;
fd_set fdset_send;
FD_ZERO(&fdset_recv);
FD_ZERO(&fdset_send);
if (requested & RECV) {
FD_SET(m_socket, &fdset_recv);
}
if (requested & SEND) {
FD_SET(m_socket, &fdset_send);
}
timeval timeout_struct = MillisToTimeval(timeout);
return select(m_socket + 1, &fdset_recv, &fdset_send, nullptr, &timeout_struct) != SOCKET_ERROR;
#endif /* USE_POLL */
}
#ifdef WIN32
std::string NetworkErrorString(int err)
{