Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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)
ssize_t Recv (void *buf, size_t len, int flags) const override
 Return parts of the contents that was provided at construction until it is exhausted and then return 0 (EOF).
bool IsConnected (std::string &) const override
 Check if still connected.
Public Member Functions inherited from ZeroSock
 ZeroSock ()
 ~ZeroSock () override
ssize_t Send (const void *, size_t len, int) const override
 send(2) wrapper.
ssize_t Recv (void *buf, size_t len, int flags) const override
 recv(2) wrapper.
int Connect (const sockaddr *, socklen_t) const override
 connect(2) wrapper.
int Bind (const sockaddr *, socklen_t) const override
 bind(2) wrapper.
int Listen (int) const override
 listen(2) wrapper.
std::unique_ptr< SockAccept (sockaddr *addr, socklen_t *addr_len) const override
 accept(2) wrapper.
int GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
 getsockopt(2) wrapper.
int SetSockOpt (int, int, const void *, socklen_t) const override
 setsockopt(2) wrapper.
int GetSockName (sockaddr *name, socklen_t *name_len) const override
 getsockname(2) wrapper.
bool SetNonBlocking () const override
 Set the non-blocking option on the socket.
bool IsSelectable () const override
 Check if the underlying socket can be used for select(2) (or the Wait() method).
bool Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
 Wait for readiness for input (recv) or output (send).
bool WaitMany (std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
 Same as Wait(), but wait on many sockets within the same timeout.
Public Member Functions inherited from Sock
 Sock ()=delete
 Sock (SOCKET s)
 Take ownership of an existent socket.
 Sock (const Sock &)=delete
 Copy constructor, disabled because closing the same socket twice is undesirable.
 Sock (Sock &&other)
 Move constructor, grab the socket from another object and close ours (if set).
virtual ~Sock ()
 Destructor, close the socket or do nothing if empty.
Sockoperator= (const Sock &)=delete
 Copy assignment operator, disabled because closing the same socket twice is undesirable.
virtual void SendComplete (std::span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Send the given data, retrying on transient errors.
virtual void SendComplete (std::span< const char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Convenience method, equivalent to SendComplete(MakeUCharSpan(data), timeout, interrupt).
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.
bool operator== (SOCKET s) const
 Check if the internal socket is equal to s.

Private Member Functions

StaticContentsSockoperator= (Sock &&other) override
 Move assignment operator, grab the socket from another object and close ours (if set).

Private Attributes

const std::string m_contents
size_t m_consumed {0}

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().
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.
static constexpr Event SEND = 0b010
 If passed to Wait(), then it will wait for readiness to send to the socket.
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.
Protected Attributes inherited from Sock
SOCKET m_socket
 Contained socket.

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 211 of file net.h.

Constructor & Destructor Documentation

◆ StaticContentsSock()

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

Definition at line 243 of file net.cpp.

Here is the caller graph for this function:

Member Function Documentation

◆ IsConnected()

bool StaticContentsSock::IsConnected ( std::string & errmsg) const
inlineoverridevirtual

Check if still connected.

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

Reimplemented from Sock.

Definition at line 222 of file net.h.

◆ operator=()

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

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

Reimplemented from Sock.

Definition at line 258 of file net.cpp.

Here is the call graph for this function:

◆ Recv()

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

Return parts of the contents that was provided at construction until it is exhausted and then return 0 (EOF).

Reimplemented from Sock.

Definition at line 248 of file net.cpp.

Member Data Documentation

◆ m_consumed

size_t StaticContentsSock::m_consumed {0}
mutableprivate

Definition at line 231 of file net.h.

◆ m_contents

const std::string StaticContentsSock::m_contents
private

Definition at line 230 of file net.h.


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