84 std::unique_ptr<Sock> sock;
89 sock = std::make_unique<Sock>(
socket);
90 }
catch (
const std::exception &) {
109 Event *occurred)
const {
113 std::shared_ptr<const Sock>
shared{
this, [](
const Sock *) {}};
121 if (occurred !=
nullptr) {
131 std::vector<pollfd>
pfds;
135 pfd.fd = sock->m_socket;
177 const auto &s = sock->m_socket;
198 const auto &s = sock->m_socket;
216 std::chrono::milliseconds timeout,
226 sent +=
static_cast<size_t>(
ret);
227 if (
sent == data.size()) {
233 throw std::runtime_error(
241 throw std::runtime_error(
242 strprintf(
"Send timeout (sent only %u of %u bytes before that)",
248 "Send interrupted (sent only %u of %u bytes before that)",
sent,
261 std::chrono::milliseconds timeout,
279 throw std::runtime_error(
280 strprintf(
"Received too many bytes without a terminator (%u)",
293 throw std::runtime_error(
299 throw std::runtime_error(
300 "Connection unexpectedly closed by peer");
313 throw std::runtime_error(
314 strprintf(
"recv() returned %u bytes on attempt to read "
315 "%u bytes but previous "
316 "peek claimed %u bytes are available",
334 throw std::runtime_error(
335 strprintf(
"Receive timeout (received %u bytes without "
336 "terminator before that)",
341 throw std::runtime_error(
342 strprintf(
"Receive interrupted (received %u bytes without "
343 "terminator before that)",
390 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>()
A helper class for interruptible sleeps.
RAII helper class that manages a socket.
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
SOCKET m_socket
Contained socket.
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual void SendComplete(const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Sock()
Default constructor, creates an empty object that does nothing when destroyed.
virtual bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const
Same as Wait(), but wait on many sockets within the same timeout.
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
static constexpr Event ERR
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
virtual SOCKET Get() const
Get the value of the contained socket.
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
virtual void Reset()
Close if non-empty.
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
virtual std::string RecvUntilTerminator(uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const
Read from socket until a terminator character is encountered.
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
#define WSAGetLastError()
static bool IsSelectableSocket(const SOCKET &s)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
static bool IOErrorIsPermanent(int err)
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Auxiliary requested/occurred events to wait for in WaitMany().
std::string SysErrorString(int err)
Return system error string from errno value.
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)