Bitcoin ABC 0.32.12
P2P Digital Currency
sock.h
Go to the documentation of this file.
1// Copyright (c) 2020-2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_SOCK_H
6#define BITCOIN_UTIL_SOCK_H
7
8#include <compat/compat.h>
10#include <util/time.h>
11
12#include <chrono>
13#include <memory>
14#include <string>
15#include <unordered_map>
16
21static constexpr auto MAX_WAIT_FOR_IO = 1s;
22
27class Sock {
28public:
29 Sock() = delete;
30
34 explicit Sock(SOCKET s);
35
40 Sock(const Sock &) = delete;
41
46 Sock(Sock &&other);
47
51 virtual ~Sock();
52
57 Sock &operator=(const Sock &) = delete;
58
63 virtual Sock &operator=(Sock &&other);
64
70 virtual ssize_t Send(const void *data, size_t len, int flags) const;
71
77 virtual ssize_t Recv(void *buf, size_t len, int flags) const;
78
84 virtual int Connect(const sockaddr *addr, socklen_t addr_len) const;
85
91 [[nodiscard]] virtual int Bind(const sockaddr *addr,
92 socklen_t addr_len) const;
93
99 [[nodiscard]] virtual int Listen(int backlog) const;
100
109 [[nodiscard]] virtual std::unique_ptr<Sock>
110 Accept(sockaddr *addr, socklen_t *addr_len) const;
111
118 virtual int GetSockOpt(int level, int opt_name, void *opt_val,
119 socklen_t *opt_len) const;
120
127 [[nodiscard]] virtual int SetSockOpt(int level, int opt_name,
128 const void *opt_val,
129 socklen_t opt_len) const;
130
137 [[nodiscard]] virtual int GetSockName(sockaddr *name,
138 socklen_t *name_len) const;
139
144 [[nodiscard]] virtual bool SetNonBlocking() const;
145
151 [[nodiscard]] virtual bool IsSelectable() const;
152
153 using Event = uint8_t;
154
159 static constexpr Event RECV = 0b001;
160
165 static constexpr Event SEND = 0b010;
166
172 static constexpr Event ERR = 0b100;
173
188 virtual bool Wait(std::chrono::milliseconds timeout, Event requested,
189 Event *occurred = nullptr) const;
190
194 struct Events {
195 explicit Events(Event req) : requested{req}, occurred{0} {}
198 };
199
201 size_t operator()(const std::shared_ptr<const Sock> &s) const {
202 return s ? s->m_socket : std::numeric_limits<SOCKET>::max();
203 }
204 };
205
207 bool operator()(const std::shared_ptr<const Sock> &lhs,
208 const std::shared_ptr<const Sock> &rhs) const {
209 if (lhs && rhs) {
210 return lhs->m_socket == rhs->m_socket;
211 }
212 if (!lhs && !rhs) {
213 return true;
214 }
215 return false;
216 }
217 };
218
228 std::unordered_map<std::shared_ptr<const Sock>, Events,
230
240 [[nodiscard]] virtual bool WaitMany(std::chrono::milliseconds timeout,
241 EventsPerSock &events_per_sock) const;
242
250 [[nodiscard]] int WaitReadableOrException(timeval *timeout) const;
251
252 /* Higher level, convenience, methods. These may throw. */
253
262 virtual void SendComplete(const std::string &data,
263 std::chrono::milliseconds timeout,
264 CThreadInterrupt &interrupt) const;
265
279 virtual std::string RecvUntilTerminator(uint8_t terminator,
280 std::chrono::milliseconds timeout,
281 CThreadInterrupt &interrupt,
282 size_t max_data) const;
283
289 virtual bool IsConnected(std::string &errmsg) const;
290
294 bool operator==(SOCKET s) const;
295
296protected:
301
302private:
306 void Close();
307};
308
310std::string NetworkErrorString(int err);
311
312#endif // BITCOIN_UTIL_SOCK_H
int flags
Definition: bitcoin-tx.cpp:546
A helper class for interruptible sleeps.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:27
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
Definition: sock.cpp:69
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:49
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:165
int WaitReadableOrException(timeval *timeout) const
Wait until the socket is readable or has an exceptional condition, or the timeout expires,...
Definition: sock.cpp:244
SOCKET m_socket
Contained socket.
Definition: sock.h:300
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.
Definition: sock.cpp:254
virtual int Bind(const sockaddr *addr, socklen_t addr_len) const
bind(2) wrapper.
Definition: sock.cpp:61
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
Definition: sock.cpp:136
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:38
uint8_t Event
Definition: sock.h:153
Sock(const Sock &)=delete
Copy constructor, disabled because closing the same socket twice is undesirable.
virtual int GetSockName(sockaddr *name, socklen_t *name_len) const
getsockname(2) wrapper.
Definition: sock.cpp:106
void Close()
Close m_socket if it is not INVALID_SOCKET.
Definition: sock.cpp:418
virtual bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const
Same as Wait(), but wait on many sockets within the same timeout.
Definition: sock.cpp:156
static constexpr Event ERR
Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has ...
Definition: sock.h:172
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:394
virtual int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const
setsockopt(2) wrapper.
Definition: sock.cpp:100
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:159
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
Definition: sock.cpp:94
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:57
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:53
Sock()=delete
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.
Definition: sock.cpp:299
virtual int Listen(int backlog) const
listen(2) wrapper.
Definition: sock.cpp:65
virtual bool SetNonBlocking() const
Set the non-blocking option on the socket.
Definition: sock.cpp:110
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
Definition: sock.h:229
virtual bool IsSelectable() const
Check if the underlying socket can be used for select(2) (or the Wait() method).
Definition: sock.cpp:128
bool operator==(SOCKET s) const
Check if the internal socket is equal to s.
Definition: sock.cpp:434
unsigned int SOCKET
Definition: compat.h:55
const char * name
Definition: rest.cpp:47
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
Definition: sock.h:21
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:438
bool operator()(const std::shared_ptr< const Sock > &lhs, const std::shared_ptr< const Sock > &rhs) const
Definition: sock.h:207
Auxiliary requested/occurred events to wait for in WaitMany().
Definition: sock.h:194
Event requested
Definition: sock.h:196
Events(Event req)
Definition: sock.h:195
Event occurred
Definition: sock.h:197
size_t operator()(const std::shared_ptr< const Sock > &s) const
Definition: sock.h:201