Bitcoin Core  21.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  virtual SOCKET Get() const;
68 
74  virtual SOCKET Release();
75 
79  virtual void Reset();
80 
85  virtual ssize_t Send(const void* data, size_t len, int flags) const;
86 
91  virtual ssize_t Recv(void* buf, size_t len, int flags) const;
92 
93  using Event = uint8_t;
94 
98  static constexpr Event RECV = 0b01;
99 
103  static constexpr Event SEND = 0b10;
104 
114  virtual bool Wait(std::chrono::milliseconds timeout,
115  Event requested,
116  Event* occurred = nullptr) const;
117 
118  /* Higher level, convenience, methods. These may throw. */
119 
128  virtual void SendComplete(const std::string& data,
129  std::chrono::milliseconds timeout,
130  CThreadInterrupt& interrupt) const;
131 
142  virtual std::string RecvUntilTerminator(uint8_t terminator,
143  std::chrono::milliseconds timeout,
144  CThreadInterrupt& interrupt) const;
145 
151  virtual bool IsConnected(std::string& errmsg) const;
152 
153 private:
158 };
159 
161 std::string NetworkErrorString(int err);
162 
164 bool CloseSocket(SOCKET& hSocket);
165 
166 #endif // BITCOIN_UTIL_SOCK_H
threadinterrupt.h
Sock::m_socket
SOCKET m_socket
Contained socket.
Definition: sock.h:157
Sock::operator=
Sock & operator=(const Sock &)=delete
Copy assignment operator, disabled because closing the same socket twice is undesirable.
flags
int flags
Definition: bitcoin-tx.cpp:512
CloseSocket
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
Definition: sock.cpp:313
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:135
Sock::Reset
virtual void Reset()
Close if non-empty.
Definition: sock.cpp:57
Sock
RAII helper class that manages a socket.
Definition: sock.h:25
compat.h
Sock::Recv
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
Definition: sock.cpp:64
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:69
Sock::SEND
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
Definition: sock.h:103
time.h
Sock::Send
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
Definition: sock.cpp:59
Sock::RecvUntilTerminator
virtual std::string RecvUntilTerminator(uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Read from socket until a terminator character is encountered.
Definition: sock.cpp:176
Sock::Sock
Sock()
Default constructor, creates an empty object that does nothing when destroyed.
Definition: sock.cpp:28
Sock::Get
virtual SOCKET Get() const
Get the value of the contained socket.
Definition: sock.cpp:48
NetworkErrorString
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: sock.cpp:295
Sock::IsConnected
virtual bool IsConnected(std::string &errmsg) const
Check if still connected.
Definition: sock.cpp:253
Sock::Event
uint8_t Event
Definition: sock.h:93
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:50
Sock::RECV
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
Definition: sock.h:98
Sock::~Sock
virtual ~Sock()
Destructor, close the socket or do nothing if empty.
Definition: sock.cpp:38
CThreadInterrupt
Definition: threadinterrupt.h:19
SOCKET
unsigned int SOCKET
Definition: compat.h:41