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

@@ -123,3 +123,8 @@ struct timeval MillisToTimeval(int64_t nTimeout)
timeout.tv_usec = (nTimeout % 1000) * 1000;
return timeout;
}
struct timeval MillisToTimeval(std::chrono::milliseconds ms)
{
return MillisToTimeval(count_milliseconds(ms));
}