13#include <util/threadinterrupt.h>
80 std::unique_ptr<Sock> sock;
85 sock = std::make_unique<Sock>(
socket);
86 }
catch (
const std::exception&) {
134#if defined(USE_POLL) || defined(WIN32)
148 std::shared_ptr<const Sock>
shared{std::shared_ptr<const Sock>{},
this};
152 if (!
WaitMany(timeout, events_per_sock)) {
156 if (occurred !=
nullptr) {
157 *occurred = events_per_sock.begin()->second.occurred;
168 std::vector<pollfd>
pfds;
169 for (
const auto& [sock,
events] : events_per_sock) {
172 pfd.fd = sock->m_socket;
185 assert(
pfds.size() == events_per_sock.size());
187 for (
auto& [sock,
events] : events_per_sock) {
212 for (
const auto& [sock,
events] : events_per_sock) {
213 if (!sock->IsSelectable()) {
216 const auto& s = sock->m_socket;
233 for (
auto& [sock,
events] : events_per_sock) {
234 const auto& s = sock->m_socket;
252 std::chrono::milliseconds timeout,
262 sent +=
static_cast<size_t>(
ret);
263 if (sent ==
data.size()) {
277 "Send timeout (sent only %u of %u bytes before that)", sent,
data.size()));
282 "Send interrupted (sent only %u of %u bytes before that)", sent,
data.size()));
293 std::chrono::milliseconds timeout,
300 std::chrono::milliseconds timeout,
318 throw std::runtime_error(
319 strprintf(
"Received too many bytes without a terminator (%u)",
data.size()));
335 throw std::runtime_error(
"Connection unexpectedly closed by peer");
347 throw std::runtime_error(
348 strprintf(
"recv() returned %u bytes on attempt to read %u bytes but previous "
349 "peek claimed %u bytes are available",
367 "Receive timeout (received %u bytes without terminator before that)",
data.size()));
372 "Receive interrupted (received %u bytes without terminator before that)",
A helper class for interruptible sleeps.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
static constexpr Event RecvEvent
If passed to Wait(), then it will wait for readiness to read from the 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.
SOCKET m_socket
Contained socket.
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual int Bind(const sockaddr *addr, socklen_t addr_len) const
bind(2) wrapper.
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.
virtual void SendComplete(std::span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
static constexpr Event SendEvent
If passed to Wait(), then it will wait for readiness to send to the socket.
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
void Close()
Close m_socket if it is not INVALID_SOCKET.
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 bool IsConnected(std::string &errmsg) const
Check if still connected.
virtual int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const
setsockopt(2) wrapper.
static constexpr Event ErrorEvent
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
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 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.
virtual int Listen(int backlog) const
listen(2) wrapper.
virtual bool SetNonBlocking() const
Set the non-blocking option on the socket.
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
virtual bool IsSelectable() const
Check if the underlying socket can be used for select(2) (or the Wait() method).
bool operator==(SOCKET s) const
Check if the internal socket is equal to s.
static constexpr std::chrono::seconds MAX_WAIT_TIMEOUT
Maximum timeout supported by Wait() and WaitMany() (about 24.8 days).
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
#define WSAGetLastError()
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
bool IOErrorIsPermanent(int err)
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
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)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.