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 <memory>
14 #include <string>
15 
20 static constexpr auto MAX_WAIT_FOR_IO = 1s;
21 
26 class Sock
27 {
28 public:
32  Sock();
33 
37  explicit Sock(SOCKET s);
38 
42  Sock(const Sock&) = delete;
43 
47  Sock(Sock&& other);
48 
52  virtual ~Sock();
53 
57  Sock& operator=(const Sock&) = delete;
58 
62  virtual Sock& operator=(Sock&& other);
63 
68  [[nodiscard]] virtual SOCKET Get() const;
69 
75  virtual SOCKET Release();
76 
80  virtual void Reset();
81 
86  [[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
87 
92  [[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
93 
98  [[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
99 
106  [[nodiscard]] virtual std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const;
107 
113  [[nodiscard]] virtual int GetSockOpt(int level,
114  int opt_name,
115  void* opt_val,
116  socklen_t* opt_len) const;
117 
118  using Event = uint8_t;
119 
123  static constexpr Event RECV = 0b01;
124 
128  static constexpr Event SEND = 0b10;
129 
139  [[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
140  Event requested,
141  Event* occurred = nullptr) const;
142 
143  /* Higher level, convenience, methods. These may throw. */
144 
153  virtual void SendComplete(const std::string& data,
154  std::chrono::milliseconds timeout,
155  CThreadInterrupt& interrupt) const;
156 
169  [[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
170  std::chrono::milliseconds timeout,
171  CThreadInterrupt& interrupt,
172  size_t max_data) const;
173 
179  [[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
180 
181 protected:
186 };
187 
189 std::string NetworkErrorString(int err);
190 
192 bool CloseSocket(SOCKET& hSocket);
193 
194 #endif // BITCOIN_UTIL_SOCK_H
threadinterrupt.h
Sock::m_socket
SOCKET m_socket
Contained socket.
Definition: sock.h:185
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:72
flags
int flags
Definition: bitcoin-tx.cpp:525
CloseSocket
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:358
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:174
Sock::Reset
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:60
Sock
RAII helper class that manages a socket.
Definition: sock.h:26
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:215
compat.h
Sock::Recv
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:67
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:108
Sock::Accept
virtual std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const
accept(2) wrapper.
Definition: sock.cpp:77
Sock::SEND
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:128
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:103
Sock::Send
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:62
Sock::Sock
Sock()
Default constructor, creates an empty object that does nothing when destroyed.
Definition: sock.cpp:31
Sock::Get
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:51
NetworkErrorString
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:340
Sock::IsConnected
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:298
Sock::Event
uint8_t Event
Definition: sock.h:118
MAX_WAIT_FOR_IO
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
Definition: sock.h:20
Sock::Release
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
Definition: sock.cpp:53
Sock::RECV
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:123
Sock::~Sock
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:41
CThreadInterrupt
Definition: threadinterrupt.h:19
SOCKET
unsigned int SOCKET
Definition: compat.h:41