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

Manages a group of peer s. More...

#include <connection_manager.hpp>

Public Member Functions

 connection_manager ()=default
 
 connection_manager (const connection_manager &)=delete
 
auto operator= (const connection_manager &) -> connection_manager &=delete
 
 connection_manager (connection_manager &&)=delete
 
auto operator= (connection_manager &&) -> connection_manager &=delete
 
 ~connection_manager ()
 
auto listen (const ip_address &host, unsigned short port) -> bool
 Starts a listener to listen for inbound connections on the specified IP address and port.
 
auto pump () -> bool
 Listens for and accepts inbound connections.
 
void broadcast (const std::shared_ptr< buffer > &data)
 Sends the provided data to all added peers.
 
template<typename Ta >
void broadcast (const Ta &data)
 Serialize the data and broadcast it to all peers.
 
auto handle_messages () -> std::vector< message_t >
 Collects and return unhandled packets received from connected peers.
 
auto add (std::unique_ptr< tcp_socket > sock, bool attempt_reconnect=true) -> peer_id_t
 Registers the provided socket as a peer to which messages can be sent or broadcast.
 
auto cluster_connect (const std::vector< endpoint_t > &endpoints, bool error_fatal=true) -> bool
 Establishes connections to the provided list of endpoints.
 
auto start_cluster_handler (const std::vector< endpoint_t > &endpoints, const packet_handler_t &handler) -> std::optional< std::thread >
 Connects to the provided endpoints and calls the provided handler for packets received from those endpoints.
 
auto start_server (const endpoint_t &listen_endpoint, const packet_handler_t &handler) -> std::optional< std::thread >
 Establishes a server at the specified endpoint which handles inbound traffic with the specified handler function.
 
auto start_server_listener () -> std::thread
 Launches a thread that listens for and accepts inbound connections.
 
auto start_handler (const packet_handler_t &handler) -> std::thread
 Starts a thread to handle messages from established connections using the specified handler function.
 
void close ()
 Shuts down the network listener and all existing peer connections.
 
void send (const std::shared_ptr< buffer > &data, peer_id_t peer_id)
 Sends the provided data to the specified peer.
 
template<typename Ta >
void send (const Ta &data, peer_id_t peer_id)
 Serialize the data and transmit it in a packet to the remote host at the specified peer ID.
 
auto peer_count () -> size_t
 Returns the number of peers connected to this network.
 
void reset ()
 Resets the network instance to a fresh state.
 
auto send_to_one (const std::shared_ptr< buffer > &data) -> bool
 Send the provided data to an online peer managed by this network.
 
template<typename T >
auto send_to_one (const T &data) -> bool
 Serialize and send the provided data to an online peer managed by this network.
 
auto connected (peer_id_t peer_id) -> bool
 Determines whether the given peer ID is connected.
 
auto connected_to_one () -> bool
 Determines if the network is connected to at least one peer.
 

Detailed Description

Manages a group of peer s.

Utility class for managing groups of peers. Handles listening for incoming connections on a TCP socket, connecting to outgoing peers, and passing incoming packets to a handler callback. Supports sending a packet to a specific peer, or broadcasting a packet to all peers.

Definition at line 53 of file connection_manager.hpp.

Constructor & Destructor Documentation

◆ connection_manager() [1/3]

cbdc::network::connection_manager::connection_manager ( )
default

◆ connection_manager() [2/3]

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

◆ connection_manager() [3/3]

cbdc::network::connection_manager::connection_manager ( connection_manager && )
delete

◆ ~connection_manager()

cbdc::network::connection_manager::~connection_manager ( )

Definition at line 9 of file connection_manager.cpp.

References close().

Here is the call graph for this function:

Member Function Documentation

◆ add()

auto cbdc::network::connection_manager::add ( std::unique_ptr< tcp_socket > sock,
bool attempt_reconnect = true ) -> peer_id_t

Registers the provided socket as a peer to which messages can be sent or broadcast.

Parameters
socka connected socket.
attempt_reconnecttrue if the socket should automatically reconnect if disconnected.
Returns
peer ID for this network.

Definition at line 80 of file connection_manager.cpp.

◆ broadcast() [1/2]

void cbdc::network::connection_manager::broadcast ( const std::shared_ptr< buffer > & data)

Sends the provided data to all added peers.

Parameters
datapacket to to send.
See also
connection_manager::add

Definition at line 40 of file connection_manager.cpp.

References cbdc::network::peer::send().

Referenced by broadcast(), cbdc::watchtower::async_client::request_best_block_height(), and cbdc::watchtower::async_client::request_status_update().

Here is the call graph for this function:

◆ broadcast() [2/2]

template<typename Ta >
void cbdc::network::connection_manager::broadcast ( const Ta & data)
inline

Serialize the data and broadcast it to all peers.

Wraps connection_manager::broadcast.

Parameters
datadata to serialize and send.

Definition at line 87 of file connection_manager.hpp.

References broadcast(), and cbdc::make_shared_buffer().

Here is the call graph for this function:

◆ close()

◆ cluster_connect()

auto cbdc::network::connection_manager::cluster_connect ( const std::vector< endpoint_t > & endpoints,
bool error_fatal = true ) -> bool

Establishes connections to the provided list of endpoints.

Parameters
endpointsset of server endpoints to which to establish TCP socket connections.
error_fataltrue if this function should abort and return false after a single failed connection attempt.
Returns
false if any of the connections failed while the error_fatal flag is true.

Definition at line 113 of file connection_manager.cpp.

Referenced by cbdc::rpc::tcp_client< Request, Response >::init().

◆ connected()

auto cbdc::network::connection_manager::connected ( peer_id_t peer_id) -> bool
nodiscard

Determines whether the given peer ID is connected.

Parameters
peer_idthe peer to check
Returns
true if the peer is connected, otherwise false

Definition at line 264 of file connection_manager.cpp.

◆ connected_to_one()

auto cbdc::network::connection_manager::connected_to_one ( ) -> bool
nodiscard

Determines if the network is connected to at least one peer.

Returns
true if at least one peer is connected.

Definition at line 276 of file connection_manager.cpp.

◆ handle_messages()

auto cbdc::network::connection_manager::handle_messages ( ) -> std::vector<message_t>
nodiscard

Collects and return unhandled packets received from connected peers.

Returns
vector of packets to handle.
Note
returned packets may be empty; check before dereferencing.

Definition at line 49 of file connection_manager.cpp.

◆ listen()

auto cbdc::network::connection_manager::listen ( const ip_address & host,
unsigned short port ) -> bool
nodiscard

Starts a listener to listen for inbound connections on the specified IP address and port.

Parameters
hostIP address to use.
portport to use.
Returns
true if creating the listener succeeded. False if port or IP is unavailable.

Definition at line 13 of file connection_manager.cpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ peer_count()

auto cbdc::network::connection_manager::peer_count ( ) -> size_t
nodiscard

Returns the number of peers connected to this network.

Returns
number of peers connected to this network.

Definition at line 219 of file connection_manager.cpp.

◆ pump()

auto cbdc::network::connection_manager::pump ( ) -> bool
nodiscard

Listens for and accepts inbound connections.

Returns
true on a clean shutdown. False upon a socket accept failure.

Definition at line 24 of file connection_manager.cpp.

◆ reset()

void cbdc::network::connection_manager::reset ( )

Resets the network instance to a fresh state.

Callers must close() the network and join() any handler threads before re-using the instance with this function.

Definition at line 224 of file connection_manager.cpp.

References close().

Here is the call graph for this function:

◆ send() [1/2]

void cbdc::network::connection_manager::send ( const std::shared_ptr< buffer > & data,
peer_id_t peer_id )

Sends the provided data to the specified peer.

Conducts an O(n) search for the target peer.

Parameters
datadata packet to send.
peer_idID of the peer to whom to send data.

Definition at line 201 of file connection_manager.cpp.

References cbdc::network::peer::send().

Referenced by send().

Here is the call graph for this function:

◆ send() [2/2]

template<typename Ta >
void cbdc::network::connection_manager::send ( const Ta & data,
peer_id_t peer_id )
inline

Serialize the data and transmit it in a packet to the remote host at the specified peer ID.

Parameters
datadata to serialize and send.
peer_idID of the peer to whom to send data.

Definition at line 162 of file connection_manager.hpp.

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

Here is the call graph for this function:

◆ send_to_one() [1/2]

auto cbdc::network::connection_manager::send_to_one ( const std::shared_ptr< buffer > & data) -> bool
nodiscard

Send the provided data to an online peer managed by this network.

Parameters
datapacket to send.
Returns
flag to indicate whether the packet was sent to a peer.

Definition at line 236 of file connection_manager.cpp.

Referenced by send_to_one().

◆ send_to_one() [2/2]

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

Serialize and send the provided data to an online peer managed by this network.

Wraps connection_manager::send_to_one.

Parameters
dataserializable object to send.
Returns
flag to indicate whether the packet was sent to a peer.

Definition at line 187 of file connection_manager.hpp.

References cbdc::make_shared_buffer(), and send_to_one().

Here is the call graph for this function:

◆ start_cluster_handler()

auto cbdc::network::connection_manager::start_cluster_handler ( const std::vector< endpoint_t > & endpoints,
const packet_handler_t & handler ) -> std::optional<std::thread>
nodiscard

Connects to the provided endpoints and calls the provided handler for packets received from those endpoints.

Parameters
endpointsset of server endpoints to which to establish TCP socket connections.
handlerfunction to handle packets from server connections.
Returns
the thread on which the handler will be called, or nullopt if any client connection fails. May be joined by callers.
Note
Calling this method and start_server on the same connection_manager instance will result in two handler threads.

Definition at line 128 of file connection_manager.cpp.

◆ start_handler()

auto cbdc::network::connection_manager::start_handler ( const packet_handler_t & handler) -> std::thread
nodiscard

Starts a thread to handle messages from established connections using the specified handler function.

Parameters
handlerfunction to handle packets from client connections.
Returns
the thread on which the handler will be called.

Definition at line 161 of file connection_manager.cpp.

Referenced by cbdc::rpc::tcp_client< Request, Response >::init().

◆ start_server()

auto cbdc::network::connection_manager::start_server ( const endpoint_t & listen_endpoint,
const packet_handler_t & handler ) -> std::optional<std::thread>
nodiscard

Establishes a server at the specified endpoint which handles inbound traffic with the specified handler function.

Parameters
listen_endpointthe endpoint at which to start the server.
handlerfunction to handle packets from client connections.
Returns
the thread on which the handler will be called, or nullopt if the server fails to start. May be joined by callers.
Note
Calling this method and start_cluster_handler on the same connection_manager instance will result in two handler threads.

Definition at line 138 of file connection_manager.cpp.

◆ start_server_listener()

auto cbdc::network::connection_manager::start_server_listener ( ) -> std::thread
nodiscard

Launches a thread that listens for and accepts inbound connections.

Called after listen().

Returns
listener thread.

Definition at line 153 of file connection_manager.cpp.


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