5 #ifndef BITCOIN_UTIL_SOCK_H 6 #define BITCOIN_UTIL_SOCK_H 15 #include <unordered_map> 65 [[nodiscard]]
virtual ssize_t
Send(
const void* data,
size_t len,
int flags)
const;
71 [[nodiscard]]
virtual ssize_t
Recv(
void* buf,
size_t len,
int flags)
const;
77 [[nodiscard]]
virtual int Connect(
const sockaddr* addr, socklen_t addr_len)
const;
83 [[nodiscard]]
virtual int Bind(
const sockaddr* addr, socklen_t addr_len)
const;
89 [[nodiscard]]
virtual int Listen(
int backlog)
const;
97 [[nodiscard]]
virtual std::unique_ptr<Sock>
Accept(sockaddr* addr, socklen_t* addr_len)
const;
104 [[nodiscard]]
virtual int GetSockOpt(
int level,
107 socklen_t* opt_len)
const;
114 [[nodiscard]]
virtual int SetSockOpt(
int level,
117 socklen_t opt_len)
const;
124 [[nodiscard]]
virtual int GetSockName(sockaddr*
name, socklen_t* name_len)
const;
166 [[nodiscard]]
virtual bool Wait(std::chrono::milliseconds timeout,
168 Event* occurred =
nullptr)
const;
180 size_t operator()(
const std::shared_ptr<const Sock>& s)
const 182 return s ? s->m_socket : std::numeric_limits<SOCKET>::max();
188 const std::shared_ptr<const Sock>& rhs)
const 191 return lhs->m_socket == rhs->m_socket;
218 [[nodiscard]]
virtual bool WaitMany(std::chrono::milliseconds timeout,
232 std::chrono::milliseconds timeout,
248 std::chrono::milliseconds timeout,
250 size_t max_data)
const;
257 [[nodiscard]]
virtual bool IsConnected(std::string& errmsg)
const;
280 #endif // BITCOIN_UTIL_SOCK_H
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
virtual bool SetNonBlocking() const
Set the non-blocking option on the socket.
virtual int Bind(const sockaddr *addr, socklen_t addr_len) const
bind(2) wrapper.
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 void SendComplete(const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
SOCKET m_socket
Contained socket.
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 ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
void Close()
Close m_socket if it is not INVALID_SOCKET.
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
size_t operator()(const std::shared_ptr< const Sock > &s) const
virtual int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const
setsockopt(2) wrapper.
A helper class for interruptible sleeps.
static constexpr Event ERR
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
bool operator==(SOCKET s) const
Check if the internal socket is equal to s.
virtual bool IsSelectable() const
Check if the underlying socket can be used for select(2) (or the Wait() method).
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
virtual int Listen(int backlog) const
listen(2) wrapper.
bool operator()(const std::shared_ptr< const Sock > &lhs, const std::shared_ptr< const Sock > &rhs) const
RAII helper class that manages a socket and closes it automatically when it goes out of scope...
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
Auxiliary requested/occurred events to wait for in WaitMany().
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.