101 if (fd.revents &
POLLIN) {
150 std::chrono::milliseconds timeout,
160 sent +=
static_cast<size_t>(
ret);
161 if (
sent == data.size()) {
167 throw std::runtime_error(
175 throw std::runtime_error(
176 strprintf(
"Send timeout (sent only %u of %u bytes before that)",
182 "Send interrupted (sent only %u of %u bytes before that)",
sent,
195 std::chrono::milliseconds timeout,
213 throw std::runtime_error(
214 strprintf(
"Received too many bytes without a terminator (%u)",
227 throw std::runtime_error(
233 throw std::runtime_error(
234 "Connection unexpectedly closed by peer");
247 throw std::runtime_error(
248 strprintf(
"recv() returned %u bytes on attempt to read "
249 "%u bytes but previous "
250 "peek claimed %u bytes are available",
268 throw std::runtime_error(
269 strprintf(
"Receive timeout (received %u bytes without "
270 "terminator before that)",
275 throw std::runtime_error(
276 strprintf(
"Receive interrupted (received %u bytes without "
277 "terminator before that)",
324 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 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 SOCKET Release()
Get the value of the contained socket and drop ownership.
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.
#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.
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)