Bitcoin ABC  0.26.3
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 <string>
14 
19 static constexpr auto MAX_WAIT_FOR_IO = 1s;
20 
26 class Sock {
27 public:
32  Sock();
33 
37  explicit Sock(SOCKET s);
38 
43  Sock(const Sock &) = delete;
44 
49  Sock(Sock &&other);
50 
54  virtual ~Sock();
55 
60  Sock &operator=(const Sock &) = delete;
61 
66  virtual Sock &operator=(Sock &&other);
67 
72  virtual SOCKET Get() const;
73 
79  virtual SOCKET Release();
80 
84  virtual void Reset();
85 
91  virtual ssize_t Send(const void *data, size_t len, int flags) const;
92 
98  virtual ssize_t Recv(void *buf, size_t len, int flags) const;
99 
105  virtual int Connect(const sockaddr *addr, socklen_t addr_len) const;
106 
113  virtual int GetSockOpt(int level, int opt_name, void *opt_val,
114  socklen_t *opt_len) const;
115 
116  using Event = uint8_t;
117 
122  static constexpr Event RECV = 0b01;
123 
128  static constexpr Event SEND = 0b10;
129 
142  virtual bool Wait(std::chrono::milliseconds timeout, Event requested,
143  Event *occurred = nullptr) const;
144 
145  /* Higher level, convenience, methods. These may throw. */
146 
155  virtual void SendComplete(const std::string &data,
156  std::chrono::milliseconds timeout,
157  CThreadInterrupt &interrupt) const;
158 
172  virtual std::string RecvUntilTerminator(uint8_t terminator,
173  std::chrono::milliseconds timeout,
174  CThreadInterrupt &interrupt,
175  size_t max_data) const;
176 
182  virtual bool IsConnected(std::string &errmsg) const;
183 
184 protected:
189 };
190 
192 std::string NetworkErrorString(int err);
193 
195 bool CloseSocket(SOCKET &hSocket);
196 
197 #endif // BITCOIN_UTIL_SOCK_H
int flags
Definition: bitcoin-tx.cpp:533
A helper class for interruptible sleeps.
RAII helper class that manages a socket.
Definition: sock.h:26
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:63
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:128
SOCKET m_socket
Contained socket.
Definition: sock.h:188
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:148
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:81
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:38
uint8_t Event
Definition: sock.h:116
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:29
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
Definition: sock.cpp:53
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:288
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:122
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:49
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
Definition: sock.cpp:75
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:71
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:59
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:67
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:193
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:19
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:331
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:353