OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
cbdc::network::tcp_socket Class Reference

Wrapper for a TCP socket. More...

#include <tcp_socket.hpp>

Inheritance diagram for cbdc::network::tcp_socket:
Collaboration diagram for cbdc::network::tcp_socket:

Public Member Functions

 tcp_socket ()=default
 Constructs an empty, unconnected TCP socket.
 
 ~tcp_socket () override
 
 tcp_socket (const tcp_socket &)=delete
 
auto operator= (const tcp_socket &) -> tcp_socket &=delete
 
 tcp_socket (tcp_socket &&)=delete
 
auto operator= (tcp_socket &&) -> tcp_socket &=delete
 
auto connect (const endpoint_t &ep) -> bool
 Attempts to connect to the given endpoint.
 
auto connect (const ip_address &remote_address, port_number_t remote_port) -> bool
 Attempts to connect to the given remote address/port combination.
 
auto send (const buffer &pkt) const -> bool
 Sends the given packet to the remote host.
 
template<typename T >
auto send (const T &data) const -> bool
 Serialize the data and transmit it in a packet to the remote host.
 
auto receive (buffer &pkt) const -> bool
 Attempts to receive a packet from the remote host this function will block until there is data ready to receive or an error occurs.
 
void disconnect ()
 Closes the connection with the remote host and unblocks any blocking calls to this socket.
 
auto reconnect () -> bool
 Reconnects to the previously connected endpoint.
 
auto connected () const -> bool
 Returns whether the socket successfully connected to an endpoint.
 
- Public Member Functions inherited from cbdc::network::socket
 socket (const socket &)=delete
 
auto operator= (const socket &) -> socket &=delete
 
 socket (socket &&)=delete
 
auto operator= (socket &&) -> socket &=delete
 
virtual ~socket ()=default
 

Detailed Description

Wrapper for a TCP socket.

Manages a raw UNIX TCP socket. Handles sending and receiving discrete packets by providing a protocol for determining packet boundaries. Sends the size of the packet before the packet data. When receiving, reads the packet size and returns a discrete packet once the expected size is read in full.

Definition at line 24 of file tcp_socket.hpp.

Constructor & Destructor Documentation

◆ tcp_socket() [1/3]

cbdc::network::tcp_socket::tcp_socket ( )
default

Constructs an empty, unconnected TCP socket.

◆ ~tcp_socket()

cbdc::network::tcp_socket::~tcp_socket ( )
override

Definition at line 56 of file tcp_socket.cpp.

References disconnect().

Here is the call graph for this function:

◆ tcp_socket() [2/3]

cbdc::network::tcp_socket::tcp_socket ( const tcp_socket & )
delete

◆ tcp_socket() [3/3]

cbdc::network::tcp_socket::tcp_socket ( tcp_socket && )
delete

Member Function Documentation

◆ connect() [1/2]

auto cbdc::network::tcp_socket::connect ( const endpoint_t & ep) -> bool

Attempts to connect to the given endpoint.

Parameters
epthe endpoint to which this socket should connect
Returns
true if the socket connected to the endpoint successfully.

Definition at line 13 of file tcp_socket.cpp.

◆ connect() [2/2]

auto cbdc::network::tcp_socket::connect ( const ip_address & remote_address,
port_number_t remote_port ) -> bool

Attempts to connect to the given remote address/port combination.

Parameters
remote_addressthe IP address of the remote endpoint
remote_portthe port number of the remote endpoint
Returns
true if the socket connected to the endpoint successfully.

Definition at line 17 of file tcp_socket.cpp.

◆ connected()

auto cbdc::network::tcp_socket::connected ( ) const -> bool
nodiscard

Returns whether the socket successfully connected to an endpoint.

Returns
true if connect() call succeeded. False if connect() has not yet been called, the connect() call failed, or following a disconnect() call.

Definition at line 131 of file tcp_socket.cpp.

◆ disconnect()

void cbdc::network::tcp_socket::disconnect ( )

Closes the connection with the remote host and unblocks any blocking calls to this socket.

Definition at line 47 of file tcp_socket.cpp.

Referenced by ~tcp_socket().

◆ operator=() [1/2]

auto cbdc::network::tcp_socket::operator= ( const tcp_socket & ) -> tcp_socket &=delete
delete

◆ operator=() [2/2]

auto cbdc::network::tcp_socket::operator= ( tcp_socket && ) -> tcp_socket &=delete
delete

◆ receive()

auto cbdc::network::tcp_socket::receive ( buffer & pkt) const -> bool
nodiscard

Attempts to receive a packet from the remote host this function will block until there is data ready to receive or an error occurs.

Parameters
pktthe packet to receive into
Returns
true if a packet was received successfully.

Definition at line 90 of file tcp_socket.cpp.

◆ reconnect()

auto cbdc::network::tcp_socket::reconnect ( ) -> bool

Reconnects to the previously connected endpoint.

If connect was never called before, returns an error. Disconnects any previous endpoint before re-connecting.

Returns
true if the socket reconnected successfully.

Definition at line 123 of file tcp_socket.cpp.

◆ send() [1/2]

auto cbdc::network::tcp_socket::send ( const buffer & pkt) const -> bool
nodiscard

Sends the given packet to the remote host.

Parameters
pktthe packet to send.
Returns
true if the packet was sent successfully.

Definition at line 60 of file tcp_socket.cpp.

Referenced by send().

◆ send() [2/2]

template<typename T >
auto cbdc::network::tcp_socket::send ( const T & data) const -> bool
inlinenodiscard

Serialize the data and transmit it in a packet to the remote host.

Parameters
datadata to serialize and send.
Returns
true if the packet was sent successfully.

Definition at line 58 of file tcp_socket.hpp.

References cbdc::make_buffer(), and send().

Here is the call graph for this function:

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