73 return connect(
m_socket, addr, addr_len);
78 return getsockopt(
m_socket, level, opt_name,
static_cast<char*
>(opt_val), opt_len);
87 if (requested &
RECV) {
90 if (requested &
SEND) {
98 if (occurred !=
nullptr) {
100 if (fd.revents & POLLIN) {
103 if (fd.revents & POLLOUT) {
116 FD_ZERO(&fdset_recv);
117 FD_ZERO(&fdset_send);
119 if (requested &
RECV) {
123 if (requested &
SEND) {
133 if (occurred !=
nullptr) {
135 if (FD_ISSET(
m_socket, &fdset_recv)) {
138 if (FD_ISSET(
m_socket, &fdset_send)) {
148 std::chrono::milliseconds timeout,
151 const auto deadline = GetTime<std::chrono::milliseconds>() + timeout;
155 const ssize_t ret{
Send(data.data() + sent, data.size() - sent,
MSG_NOSIGNAL)};
158 sent +=
static_cast<size_t>(ret);
159 if (sent == data.size()) {
169 const auto now = GetTime<std::chrono::milliseconds>();
171 if (now >= deadline) {
173 "Send timeout (sent only %u of %u bytes before that)", sent, data.size()));
178 "Send interrupted (sent only %u of %u bytes before that)", sent, data.size()));
183 const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{
MAX_WAIT_FOR_IO});
189 std::chrono::milliseconds timeout,
191 size_t max_data)
const
193 const auto deadline = GetTime<std::chrono::milliseconds>() + timeout;
195 bool terminator_found{
false};
206 if (data.size() >= max_data) {
207 throw std::runtime_error(
208 strprintf(
"Received too many bytes without a terminator (%u)", data.size()));
213 const ssize_t peek_ret{
Recv(buf, std::min(
sizeof(buf), max_data - data.size()), MSG_PEEK)};
224 throw std::runtime_error(
"Connection unexpectedly closed by peer");
226 auto end = buf + peek_ret;
227 auto terminator_pos = std::find(buf, end, terminator);
228 terminator_found = terminator_pos != end;
230 const size_t try_len{terminator_found ? terminator_pos - buf + 1 :
231 static_cast<size_t>(peek_ret)};
233 const ssize_t read_ret{
Recv(buf, try_len, 0)};
235 if (read_ret < 0 ||
static_cast<size_t>(read_ret) != try_len) {
236 throw std::runtime_error(
237 strprintf(
"recv() returned %u bytes on attempt to read %u bytes but previous "
238 "peek claimed %u bytes are available",
239 read_ret, try_len, peek_ret));
243 const size_t append_len{terminator_found ? try_len - 1 : try_len};
245 data.append(buf, buf + append_len);
247 if (terminator_found) {
252 const auto now = GetTime<std::chrono::milliseconds>();
254 if (now >= deadline) {
256 "Receive timeout (received %u bytes without terminator before that)", data.size()));
261 "Receive interrupted (received %u bytes without terminator before that)",
266 const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{
MAX_WAIT_FOR_IO});
274 errmsg =
"not connected";
279 switch (
Recv(&c,
sizeof(c), MSG_PEEK)) {
301 if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
302 nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
303 buf, ARRAYSIZE(buf),
nullptr))
305 return strprintf(
"%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>().to_bytes(buf), err);
309 return strprintf(
"Unknown error (%d)", err);
320#ifdef STRERROR_R_CHAR_P
321 s = strerror_r(err, buf,
sizeof(buf));
324 if (strerror_r(err, buf,
sizeof(buf)))
336 int ret = closesocket(hSocket);
338 int ret = close(hSocket);
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)
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.
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)