Bitcoin Core  24.99.0
P2P Digital Currency
Public Member Functions | Private Attributes | List of all members
StaticContentsSock Class Reference

A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores all writes. More...

#include <net.h>

Inheritance diagram for StaticContentsSock:
[legend]
Collaboration diagram for StaticContentsSock:
[legend]

Public Member Functions

 StaticContentsSock (const std::string &contents)
 
 ~StaticContentsSock () override
 
StaticContentsSockoperator= (Sock &&other) override
 Move assignment operator, grab the socket from another object and close ours (if set). More...
 
ssize_t Send (const void *, size_t len, int) const override
 send(2) wrapper. More...
 
ssize_t Recv (void *buf, size_t len, int flags) const override
 recv(2) wrapper. More...
 
int Connect (const sockaddr *, socklen_t) const override
 connect(2) wrapper. More...
 
int Bind (const sockaddr *, socklen_t) const override
 bind(2) wrapper. More...
 
int Listen (int) const override
 listen(2) wrapper. More...
 
std::unique_ptr< SockAccept (sockaddr *addr, socklen_t *addr_len) const override
 accept(2) wrapper. More...
 
int GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
 getsockopt(2) wrapper. More...
 
int SetSockOpt (int, int, const void *, socklen_t) const override
 setsockopt(2) wrapper. More...
 
int GetSockName (sockaddr *name, socklen_t *name_len) const override
 getsockname(2) wrapper. More...
 
bool Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
 Wait for readiness for input (recv) or output (send). More...
 
bool WaitMany (std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
 Same as Wait(), but wait on many sockets within the same timeout. More...
 
- Public Member Functions inherited from Sock
 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 SOCKET Get () const
 Get the value of the contained socket. 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...
 

Private Attributes

const std::string m_contents
 
size_t m_consumed
 

Additional Inherited Members

- Public Types inherited from Sock
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...
 
- Static Public Attributes inherited from Sock
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 inherited from Sock
SOCKET m_socket
 Contained socket. More...
 

Detailed Description

A mocked Sock alternative that returns a statically contained data upon read and succeeds and ignores all writes.

The data to be returned is given to the constructor and when it is exhausted an EOF is returned by further reads.

Definition at line 103 of file net.h.

Constructor & Destructor Documentation

◆ StaticContentsSock()

StaticContentsSock::StaticContentsSock ( const std::string &  contents)
inlineexplicit

Definition at line 106 of file net.h.

◆ ~StaticContentsSock()

StaticContentsSock::~StaticContentsSock ( )
inlineoverride

Definition at line 112 of file net.h.

Member Function Documentation

◆ Accept()

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

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

Definition at line 138 of file net.h.

◆ Bind()

int StaticContentsSock::Bind ( const sockaddr *  addr,
socklen_t  addr_len 
) const
inlineoverridevirtual

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

Definition at line 134 of file net.h.

◆ Connect()

int StaticContentsSock::Connect ( const sockaddr *  addr,
socklen_t  addr_len 
) const
inlineoverridevirtual

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

Definition at line 132 of file net.h.

◆ GetSockName()

int StaticContentsSock::GetSockName ( sockaddr *  name,
socklen_t *  name_len 
) const
inlineoverridevirtual

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

Definition at line 163 of file net.h.

◆ GetSockOpt()

int StaticContentsSock::GetSockOpt ( int  level,
int  opt_name,
void *  opt_val,
socklen_t *  opt_len 
) const
inlineoverridevirtual

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

Definition at line 155 of file net.h.

◆ Listen()

int StaticContentsSock::Listen ( int  backlog) const
inlineoverridevirtual

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

Definition at line 136 of file net.h.

◆ operator=()

StaticContentsSock& StaticContentsSock::operator= ( Sock &&  other)
inlineoverridevirtual

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

Reimplemented from Sock.

Definition at line 114 of file net.h.

Here is the call graph for this function:

◆ Recv()

ssize_t StaticContentsSock::Recv ( void *  buf,
size_t  len,
int  flags 
) const
inlineoverridevirtual

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

Definition at line 122 of file net.h.

◆ Send()

ssize_t StaticContentsSock::Send ( const void *  data,
size_t  len,
int  flags 
) const
inlineoverridevirtual

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

Definition at line 120 of file net.h.

◆ SetSockOpt()

int StaticContentsSock::SetSockOpt ( int  level,
int  opt_name,
const void *  opt_val,
socklen_t  opt_len 
) const
inlineoverridevirtual

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

Definition at line 161 of file net.h.

◆ Wait()

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

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

Definition at line 169 of file net.h.

◆ WaitMany()

bool StaticContentsSock::WaitMany ( std::chrono::milliseconds  timeout,
EventsPerSock events_per_sock 
) const
inlineoverridevirtual

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

Definition at line 179 of file net.h.

Member Data Documentation

◆ m_consumed

size_t StaticContentsSock::m_consumed
mutableprivate

Definition at line 190 of file net.h.

◆ m_contents

const std::string StaticContentsSock::m_contents
private

Definition at line 189 of file net.h.


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