Bitcoin Core  22.99.0
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 
25 class Sock
26 {
27 public:
31  Sock();
32 
36  explicit Sock(SOCKET s);
37 
41  Sock(const Sock&) = delete;
42 
46  Sock(Sock&& other);
47 
51  virtual ~Sock();
52 
56  Sock& operator=(const Sock&) = delete;
57 
61  virtual Sock& operator=(Sock&& other);
62 
67  [[nodiscard]] virtual SOCKET Get() const;
68 
74  virtual SOCKET Release();
75 
79  virtual void Reset();
80 
85  [[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
86 
91  [[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
92 
97  [[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
98 
104  [[nodiscard]] virtual int GetSockOpt(int level,
105  int opt_name,
106  void* opt_val,
107  socklen_t* opt_len) const;
108 
109  using Event = uint8_t;
110 
114  static constexpr Event RECV = 0b01;
115 
119  static constexpr Event SEND = 0b10;
120 
130  [[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
131  Event requested,
132  Event* occurred = nullptr) const;
133 
134  /* Higher level, convenience, methods. These may throw. */
135 
144  virtual void SendComplete(const std::string& data,
145  std::chrono::milliseconds timeout,
146  CThreadInterrupt& interrupt) const;
147 
160  [[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
161  std::chrono::milliseconds timeout,
162  CThreadInterrupt& interrupt,
163  size_t max_data) const;
164 
170  [[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
171 
172 protected:
177 };
178 
180 std::string NetworkErrorString(int err);
181 
183 bool CloseSocket(SOCKET& hSocket);
184 
185 #endif // BITCOIN_UTIL_SOCK_H
threadinterrupt.h
Sock::m_socket
SOCKET m_socket
Contained socket.
Definition: sock.h:176
Sock::operator=
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
Sock::Connect
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
Definition: sock.cpp:71
flags
int flags
Definition: bitcoin-tx.cpp:513
CloseSocket
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:331
Sock::SendComplete
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:147
Sock::Reset
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:59
Sock
RAII helper class that manages a socket.
Definition: sock.h:25
Sock::RecvUntilTerminator
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:188
compat.h
Sock::Recv
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:66
Sock::Wait
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
Sock::SEND
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:119
time.h
Sock::GetSockOpt
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
Definition: sock.cpp:76
Sock::Send
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:61
Sock::Sock
Sock()
Default constructor, creates an empty object that does nothing when destroyed.
Definition: sock.cpp:30
Sock::Get
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:50
NetworkErrorString
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:313
Sock::IsConnected
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:271
Sock::Event
uint8_t Event
Definition: sock.h:109
MAX_WAIT_FOR_IO
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
Definition: sock.h:19
Sock::Release
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
Definition: sock.cpp:52
Sock::RECV
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:114
Sock::~Sock
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:40
CThreadInterrupt
Definition: threadinterrupt.h:19
SOCKET
unsigned int SOCKET
Definition: compat.h:41