Bitcoin ABC 0.30.7
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.h>
9#include <threadinterrupt.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
28class Sock {
29public:
34 Sock();
35
39 explicit Sock(SOCKET s);
40
45 Sock(const Sock &) = delete;
46
51 Sock(Sock &&other);
52
56 virtual ~Sock();
57
62 Sock &operator=(const Sock &) = delete;
63
68 virtual Sock &operator=(Sock &&other);
69
74 virtual SOCKET Get() const;
75
81 virtual SOCKET Release();
82
86 virtual void Reset();
87
93 virtual ssize_t Send(const void *data, size_t len, int flags) const;
94
100 virtual ssize_t Recv(void *buf, size_t len, int flags) const;
101
107 virtual int Connect(const sockaddr *addr, socklen_t addr_len) const;
108
117 [[nodiscard]] virtual std::unique_ptr<Sock>
118 Accept(sockaddr *addr, socklen_t *addr_len) const;
119
126 virtual int GetSockOpt(int level, int opt_name, void *opt_val,
127 socklen_t *opt_len) const;
128
129 using Event = uint8_t;
130
135 static constexpr Event RECV = 0b001;
136
141 static constexpr Event SEND = 0b010;
142
148 static constexpr Event ERR = 0b100;
149
164 virtual bool Wait(std::chrono::milliseconds timeout, Event requested,
165 Event *occurred = nullptr) const;
166
170 struct Events {
171 explicit Events(Event req) : requested{req}, occurred{0} {}
174 };
175
177 size_t operator()(const std::shared_ptr<const Sock> &s) const {
178 return s ? s->m_socket : std::numeric_limits<SOCKET>::max();
179 }
180 };
181
183 bool operator()(const std::shared_ptr<const Sock> &lhs,
184 const std::shared_ptr<const Sock> &rhs) const {
185 if (lhs && rhs) {
186 return lhs->m_socket == rhs->m_socket;
187 }
188 if (!lhs && !rhs) {
189 return true;
190 }
191 return false;
192 }
193 };
194
204 std::unordered_map<std::shared_ptr<const Sock>, Events,
206
216 [[nodiscard]] virtual bool WaitMany(std::chrono::milliseconds timeout,
217 EventsPerSock &events_per_sock) const;
218
219 /* Higher level, convenience, methods. These may throw. */
220
229 virtual void SendComplete(const std::string &data,
230 std::chrono::milliseconds timeout,
231 CThreadInterrupt &interrupt) const;
232
246 virtual std::string RecvUntilTerminator(uint8_t terminator,
247 std::chrono::milliseconds timeout,
248 CThreadInterrupt &interrupt,
249 size_t max_data) const;
250
256 virtual bool IsConnected(std::string &errmsg) const;
257
258protected:
263};
264
266std::string NetworkErrorString(int err);
267
269bool CloseSocket(SOCKET &hSocket);
270
271#endif // BITCOIN_UTIL_SOCK_H
int flags
Definition: bitcoin-tx.cpp:541
A helper class for interruptible sleeps.
RAII helper class that manages a socket.
Definition: sock.h:28
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
Definition: sock.cpp:77
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:65
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:141
SOCKET m_socket
Contained socket.
Definition: sock.h:262
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:215
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:108
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:40
uint8_t Event
Definition: sock.h:129
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.
Definition: sock.cpp:31
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:128
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
Definition: sock.cpp:55
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:148
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:355
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:135
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:51
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
Definition: sock.cpp:102
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:73
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:61
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:69
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:260
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:205
unsigned int SOCKET
Definition: compat.h:40
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:398
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:405
bool operator()(const std::shared_ptr< const Sock > &lhs, const std::shared_ptr< const Sock > &rhs) const
Definition: sock.h:183
Auxiliary requested/occurred events to wait for in WaitMany().
Definition: sock.h:170
Event requested
Definition: sock.h:172
Events(Event req)
Definition: sock.h:171
Event occurred
Definition: sock.h:173
size_t operator()(const std::shared_ptr< const Sock > &s) const
Definition: sock.h:177