5#ifndef BITCOIN_UTIL_SOCK_H
6#define BITCOIN_UTIL_SOCK_H
15#include <unordered_map>
93 virtual ssize_t
Send(
const void *data,
size_t len,
int flags)
const;
100 virtual ssize_t
Recv(
void *buf,
size_t len,
int flags)
const;
107 virtual int Connect(
const sockaddr *addr, socklen_t addr_len)
const;
117 [[nodiscard]]
virtual std::unique_ptr<Sock>
118 Accept(sockaddr *addr, socklen_t *addr_len)
const;
126 virtual int GetSockOpt(
int level,
int opt_name,
void *opt_val,
127 socklen_t *opt_len)
const;
164 virtual bool Wait(std::chrono::milliseconds timeout,
Event requested,
165 Event *occurred =
nullptr)
const;
177 size_t operator()(
const std::shared_ptr<const Sock> &s)
const {
178 return s ? s->m_socket : std::numeric_limits<SOCKET>::max();
184 const std::shared_ptr<const Sock> &rhs)
const {
186 return lhs->m_socket == rhs->m_socket;
204 std::unordered_map<std::shared_ptr<const Sock>,
Events,
216 [[nodiscard]]
virtual bool WaitMany(std::chrono::milliseconds timeout,
230 std::chrono::milliseconds timeout,
247 std::chrono::milliseconds timeout,
249 size_t max_data)
const;
256 virtual bool IsConnected(std::string &errmsg)
const;
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(const Sock &)=delete
Copy constructor, disabled because closing the same socket twice is undesirable.
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().
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.
bool operator()(const std::shared_ptr< const Sock > &lhs, const std::shared_ptr< const Sock > &rhs) const
Auxiliary requested/occurred events to wait for in WaitMany().
size_t operator()(const std::shared_ptr< const Sock > &s) const