Bitcoin Core  24.99.0
P2P Digital Currency
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Attributes | Private Member Functions | List of all members
Sock Class Reference

RAII helper class that manages a socket. More...

#include <sock.h>

Inheritance diagram for Sock:
[legend]

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 ()
 Default constructor, creates an empty object that does nothing when destroyed. More...
 
 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...
 
Sockoperator= (const Sock &)=delete
 Copy assignment operator, disabled because closing the same socket twice is undesirable. More...
 
virtual Sockoperator= (Sock &&other)
 Move assignment operator, grab the socket from another object and close ours (if set). More...
 
virtual SOCKET Get () const
 Get the value of the contained socket. 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< SockAccept (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 (const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Send the given data, retrying on transient errors. 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...
 

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...
 

Detailed Description

RAII helper class that manages a socket.

Mimics std::unique_ptr, but instead of a pointer it contains a socket and closes it automatically when it goes out of scope.

Definition at line 27 of file sock.h.

Member Typedef Documentation

◆ Event

using Sock::Event = uint8_t

Definition at line 148 of file sock.h.

◆ EventsPerSock

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.

Definition at line 218 of file sock.h.

Constructor & Destructor Documentation

◆ Sock() [1/4]

Sock::Sock ( )

Default constructor, creates an empty object that does nothing when destroyed.

Definition at line 32 of file sock.cpp.

◆ Sock() [2/4]

Sock::Sock ( SOCKET  s)
explicit

Take ownership of an existent socket.

Definition at line 34 of file sock.cpp.

◆ Sock() [3/4]

Sock::Sock ( const Sock )
delete

Copy constructor, disabled because closing the same socket twice is undesirable.

◆ Sock() [4/4]

Sock::Sock ( Sock &&  other)

Move constructor, grab the socket from another object and close ours (if set).

Definition at line 36 of file sock.cpp.

◆ ~Sock()

Sock::~Sock ( )
virtual

Destructor, close the socket or do nothing if empty.

Definition at line 42 of file sock.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ Accept()

std::unique_ptr< Sock > Sock::Accept ( sockaddr *  addr,
socklen_t *  addr_len 
) const
virtual

accept(2) wrapper.

Equivalent to std::make_unique<Sock>(accept(this->Get(), 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 StaticContentsSock, and FuzzedSock.

Definition at line 79 of file sock.cpp.

◆ Bind()

int Sock::Bind ( const sockaddr *  addr,
socklen_t  addr_len 
) const
virtual

bind(2) wrapper.

Equivalent to bind(this->Get(), addr, addr_len). 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 69 of file sock.cpp.

◆ Close()

void Sock::Close ( )
private

Close m_socket if it is not INVALID_SOCKET.

Definition at line 403 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Connect()

int Sock::Connect ( const sockaddr *  addr,
socklen_t  addr_len 
) const
virtual

connect(2) wrapper.

Equivalent to connect(this->Get(), addr, addrlen). 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 64 of file sock.cpp.

Here is the caller graph for this function:

◆ Get()

SOCKET Sock::Get ( ) const
virtual

Get the value of the contained socket.

Returns
socket or INVALID_SOCKET if empty

Definition at line 52 of file sock.cpp.

Here is the caller graph for this function:

◆ GetSockName()

int Sock::GetSockName ( sockaddr *  name,
socklen_t *  name_len 
) const
virtual

getsockname(2) wrapper.

Equivalent to getsockname(this->Get(), name, name_len). 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 115 of file sock.cpp.

Here is the caller graph for this function:

◆ GetSockOpt()

int Sock::GetSockOpt ( int  level,
int  opt_name,
void *  opt_val,
socklen_t *  opt_len 
) const
virtual

getsockopt(2) wrapper.

Equivalent to getsockopt(this->Get(), 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 StaticContentsSock, and FuzzedSock.

Definition at line 105 of file sock.cpp.

Here is the caller graph for this function:

◆ IsConnected()

bool Sock::IsConnected ( std::string &  errmsg) const
virtual

Check if still connected.

Parameters
[out]errmsgThe error string, if the socket has been disconnected.
Returns
true if connected

Reimplemented in FuzzedSock.

Definition at line 378 of file sock.cpp.

Here is the call graph for this function:

◆ IsSelectable()

bool Sock::IsSelectable ( ) const
virtual

Check if the underlying socket can be used for select(2) (or the Wait() method).

Returns
true if selectable

Reimplemented in StaticContentsSock, and FuzzedSock.

Definition at line 139 of file sock.cpp.

◆ Listen()

int Sock::Listen ( int  backlog) const
virtual

listen(2) wrapper.

Equivalent to listen(this->Get(), backlog). 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 74 of file sock.cpp.

◆ operator=() [1/2]

Sock& Sock::operator= ( const Sock )
delete

Copy assignment operator, disabled because closing the same socket twice is undesirable.

◆ operator=() [2/2]

Sock & Sock::operator= ( Sock &&  other)
virtual

Move assignment operator, grab the socket from another object and close ours (if set).

Reimplemented in StaticContentsSock, and FuzzedSock.

Definition at line 44 of file sock.cpp.

Here is the call graph for this function:

◆ Recv()

ssize_t Sock::Recv ( void *  buf,
size_t  len,
int  flags 
) const
virtual

recv(2) wrapper.

Equivalent to recv(this->Get(), buf, 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 59 of file sock.cpp.

Here is the caller graph for this function:

◆ RecvUntilTerminator()

std::string Sock::RecvUntilTerminator ( uint8_t  terminator,
std::chrono::milliseconds  timeout,
CThreadInterrupt interrupt,
size_t  max_data 
) const
virtual

Read from socket until a terminator character is encountered.

Will never consume bytes past the terminator from the socket.

Parameters
[in]terminatorCharacter up to which to read from the socket.
[in]timeoutTimeout for the entire operation.
[in]interruptIf this is signaled then the operation is canceled.
[in]max_dataThe 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.
Returns
The data that has been read, without the terminating character.
Exceptions
std::runtime_errorif the operation cannot be completed. In this case some bytes may have been consumed from the socket.

Definition at line 295 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Send()

ssize_t Sock::Send ( const void *  data,
size_t  len,
int  flags 
) const
virtual

send(2) wrapper.

Equivalent to send(this->Get(), data, 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 54 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendComplete()

void Sock::SendComplete ( const std::string &  data,
std::chrono::milliseconds  timeout,
CThreadInterrupt interrupt 
) const
virtual

Send the given data, retrying on transient errors.

Parameters
[in]dataData to send.
[in]timeoutTimeout for the entire operation.
[in]interruptIf this is signaled then the operation is canceled.
Exceptions
std::runtime_errorif the operation cannot be completed. In this case only some of the data will be written to the socket.

Definition at line 254 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetNonBlocking()

bool Sock::SetNonBlocking ( ) const
virtual

Set the non-blocking option on the socket.

Returns
true if set successfully

Reimplemented in StaticContentsSock, and FuzzedSock.

Definition at line 120 of file sock.cpp.

◆ SetSockOpt()

int Sock::SetSockOpt ( int  level,
int  opt_name,
const void *  opt_val,
socklen_t  opt_len 
) const
virtual

setsockopt(2) wrapper.

Equivalent to setsockopt(this->Get(), 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 StaticContentsSock, and FuzzedSock.

Definition at line 110 of file sock.cpp.

◆ Wait()

bool Sock::Wait ( std::chrono::milliseconds  timeout,
Event  requested,
Event occurred = nullptr 
) const
virtual

Wait for readiness for input (recv) or output (send).

Parameters
[in]timeoutWait this much for at least one of the requested events to occur.
[in]requestedWait for those events, bitwise-or of RECV and SEND.
[out]occurredIf 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.
Returns
true on success (or timeout, if occurred of 0 is returned), false otherwise

Reimplemented in StaticContentsSock, and FuzzedSock.

Definition at line 148 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WaitMany()

bool Sock::WaitMany ( std::chrono::milliseconds  timeout,
EventsPerSock events_per_sock 
) const
virtual

Same as Wait(), but wait on many sockets within the same timeout.

Parameters
[in]timeoutWait this long for at least one of the requested events to occur.
[in,out]events_per_sockWait for the requested events on these sockets and set occurred for the events that actually occurred.
Returns
true on success (or timeout, if all what[].occurred are returned as 0), false otherwise

Reimplemented in StaticContentsSock, and FuzzedSock.

Definition at line 168 of file sock.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ ERR

constexpr Event Sock::ERR = 0b100
staticconstexpr

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.

Definition at line 164 of file sock.h.

◆ m_socket

SOCKET Sock::m_socket
protected

Contained socket.

INVALID_SOCKET designates the object is empty.

Definition at line 273 of file sock.h.

◆ RECV

constexpr Event Sock::RECV = 0b001
staticconstexpr

If passed to Wait(), then it will wait for readiness to read from the socket.

Definition at line 153 of file sock.h.

◆ SEND

constexpr Event Sock::SEND = 0b010
staticconstexpr

If passed to Wait(), then it will wait for readiness to send to the socket.

Definition at line 158 of file sock.h.


The documentation for this class was generated from the following files: