Bitcoin Core 28.99.0
P2P Digital Currency
|
RAII helper class that manages a socket and closes it automatically when it goes out of scope. More...
#include <sock.h>
Classes | |
struct | EqualSharedPtrSock |
struct | Events |
Auxiliary requested/occurred events to wait for in WaitMany() . More... | |
struct | HashSharedPtrSock |
Public Types | |
using | Event = uint8_t |
using | EventsPerSock = std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > |
On which socket to wait for what events in WaitMany() . More... | |
Public Member Functions | |
Sock ()=delete | |
Sock (SOCKET s) | |
Take ownership of an existent socket. More... | |
Sock (const Sock &)=delete | |
Copy constructor, disabled because closing the same socket twice is undesirable. More... | |
Sock (Sock &&other) | |
Move constructor, grab the socket from another object and close ours (if set). More... | |
virtual | ~Sock () |
Destructor, close the socket or do nothing if empty. More... | |
Sock & | operator= (const Sock &)=delete |
Copy assignment operator, disabled because closing the same socket twice is undesirable. More... | |
virtual Sock & | operator= (Sock &&other) |
Move assignment operator, grab the socket from another object and close ours (if set). More... | |
virtual ssize_t | Send (const void *data, size_t len, int flags) const |
send(2) wrapper. More... | |
virtual ssize_t | Recv (void *buf, size_t len, int flags) const |
recv(2) wrapper. More... | |
virtual int | Connect (const sockaddr *addr, socklen_t addr_len) const |
connect(2) wrapper. More... | |
virtual int | Bind (const sockaddr *addr, socklen_t addr_len) const |
bind(2) wrapper. More... | |
virtual int | Listen (int backlog) const |
listen(2) wrapper. More... | |
virtual std::unique_ptr< Sock > | Accept (sockaddr *addr, socklen_t *addr_len) const |
accept(2) wrapper. More... | |
virtual int | GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const |
getsockopt(2) wrapper. More... | |
virtual int | SetSockOpt (int level, int opt_name, const void *opt_val, socklen_t opt_len) const |
setsockopt(2) wrapper. More... | |
virtual int | GetSockName (sockaddr *name, socklen_t *name_len) const |
getsockname(2) wrapper. More... | |
virtual bool | SetNonBlocking () const |
Set the non-blocking option on the socket. More... | |
virtual bool | IsSelectable () const |
Check if the underlying socket can be used for select(2) (or the Wait() method). More... | |
virtual bool | Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const |
Wait for readiness for input (recv) or output (send). More... | |
virtual bool | WaitMany (std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const |
Same as Wait() , but wait on many sockets within the same timeout. More... | |
virtual void | SendComplete (Span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const |
Send the given data, retrying on transient errors. More... | |
virtual void | SendComplete (Span< const char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const |
Convenience method, equivalent to SendComplete(MakeUCharSpan(data), timeout, interrupt) . More... | |
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. More... | |
virtual bool | IsConnected (std::string &errmsg) const |
Check if still connected. More... | |
bool | operator== (SOCKET s) const |
Check if the internal socket is equal to s . More... | |
Static Public Attributes | |
static constexpr Event | RECV = 0b001 |
If passed to Wait() , then it will wait for readiness to read from the socket. More... | |
static constexpr Event | SEND = 0b010 |
If passed to Wait() , then it will wait for readiness to send to the socket. More... | |
static constexpr Event | ERR = 0b100 |
Ignored if passed to Wait() , but could be set in the occurred events if an exceptional condition has occurred on the socket or if it has been disconnected. More... | |
Protected Attributes | |
SOCKET | m_socket |
Contained socket. More... | |
Private Member Functions | |
void | Close () |
Close m_socket if it is not INVALID_SOCKET . More... | |
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
using Sock::Event = uint8_t |
using Sock::EventsPerSock = std::unordered_map<std::shared_ptr<const Sock>, Events, HashSharedPtrSock, EqualSharedPtrSock> |
On which socket to wait for what events in WaitMany()
.
The shared_ptr
is copied into the map to ensure that the Sock
object is not destroyed (its destructor would close the underlying socket). If this happens shortly before or after we call poll(2)
and a new socket gets created under the same file descriptor number then the report from WaitMany()
will be bogus.
|
delete |
|
explicit |
|
delete |
Copy constructor, disabled because closing the same socket twice is undesirable.
Sock::Sock | ( | Sock && | other | ) |
|
virtual |
|
virtual |
accept(2) wrapper.
Equivalent to std::make_unique<Sock>(accept(m_socket, addr, addr_len))
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation. The returned unique_ptr is empty if accept()
failed in which case errno will be set.
Reimplemented in FuzzedSock, and StaticContentsSock.
|
virtual |
bind(2) wrapper.
Equivalent to bind(m_socket, addr, addr_len)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 60 of file sock.cpp.
|
private |
|
virtual |
connect(2) wrapper.
Equivalent to connect(m_socket, addr, addrlen)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 55 of file sock.cpp.
|
virtual |
getsockname(2) wrapper.
Equivalent to getsockname(m_socket, name, name_len)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 106 of file sock.cpp.
|
virtual |
getsockopt(2) wrapper.
Equivalent to getsockopt(m_socket, level, opt_name, opt_val, opt_len)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 96 of file sock.cpp.
|
virtual |
Check if still connected.
[out] | errmsg | The error string, if the socket has been disconnected. |
Reimplemented in StaticContentsSock, and FuzzedSock.
Definition at line 376 of file sock.cpp.
|
virtual |
Check if the underlying socket can be used for select(2)
(or the Wait()
method).
Reimplemented in FuzzedSock, and StaticContentsSock.
|
virtual |
listen(2) wrapper.
Equivalent to listen(m_socket, backlog)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Copy assignment operator, disabled because closing the same socket twice is undesirable.
Move assignment operator, grab the socket from another object and close ours (if set).
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 37 of file sock.cpp.
bool Sock::operator== | ( | SOCKET | s | ) | const |
|
virtual |
recv(2) wrapper.
Equivalent to recv(m_socket, buf, len, flags);
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 50 of file sock.cpp.
|
virtual |
Read from socket until a terminator character is encountered.
Will never consume bytes past the terminator from the socket.
[in] | terminator | Character up to which to read from the socket. |
[in] | timeout | Timeout for the entire operation. |
[in] | interrupt | If this is signaled then the operation is canceled. |
[in] | max_data | The maximum amount of data (in bytes) to receive. If this many bytes are received and there is still no terminator, then this method will throw an exception. |
std::runtime_error | if the operation cannot be completed. In this case some bytes may have been consumed from the socket. |
Definition at line 293 of file sock.cpp.
|
virtual |
send(2) wrapper.
Equivalent to send(m_socket, data, len, flags);
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in StaticContentsSock, and FuzzedSock.
Definition at line 45 of file sock.cpp.
|
virtual |
|
virtual |
Send the given data, retrying on transient errors.
[in] | data | Data to send. |
[in] | timeout | Timeout for the entire operation. |
[in] | interrupt | If this is signaled then the operation is canceled. |
std::runtime_error | if the operation cannot be completed. In this case only some of the data will be written to the socket. |
Definition at line 245 of file sock.cpp.
|
virtual |
Set the non-blocking option on the socket.
Reimplemented in FuzzedSock, and StaticContentsSock.
|
virtual |
setsockopt(2) wrapper.
Equivalent to setsockopt(m_socket, level, opt_name, opt_val, opt_len)
. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.
Reimplemented in FuzzedSock, and StaticContentsSock.
|
virtual |
Wait for readiness for input (recv) or output (send).
[in] | timeout | Wait this much for at least one of the requested events to occur. |
[in] | requested | Wait for those events, bitwise-or of RECV and SEND . |
[out] | occurred | If not nullptr and the function returns true , then this indicates which of the requested events occurred (ERR will be added, even if not requested, if an exceptional event occurs on the socket). A timeout is indicated by return value of true and occurred being set to 0. |
occurred
of 0 is returned), false otherwise Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 139 of file sock.cpp.
|
virtual |
Same as Wait()
, but wait on many sockets within the same timeout.
[in] | timeout | Wait this long for at least one of the requested events to occur. |
[in,out] | events_per_sock | Wait for the requested events on these sockets and set occurred for the events that actually occurred. |
what[].occurred
are returned as 0), false otherwise Reimplemented in FuzzedSock, and StaticContentsSock.
Definition at line 159 of file sock.cpp.
|
staticconstexpr |
|
protected |
|
staticconstexpr |
|
staticconstexpr |