OpenCBDC Transaction Processor
|
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. | |
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.
|
default |
|
delete |
|
delete |
cbdc::network::connection_manager::~connection_manager | ( | ) |
Definition at line 9 of file connection_manager.cpp.
References close().
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.
sock | a connected socket. |
attempt_reconnect | true if the socket should automatically reconnect if disconnected. |
Definition at line 80 of file connection_manager.cpp.
void cbdc::network::connection_manager::broadcast | ( | const std::shared_ptr< buffer > & | data | ) |
Sends the provided data to all added peers.
data | packet to to send. |
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().
|
inline |
Serialize the data and broadcast it to all peers.
Wraps connection_manager::broadcast.
data | data to serialize and send. |
Definition at line 87 of file connection_manager.hpp.
References broadcast(), and cbdc::make_shared_buffer().
void cbdc::network::connection_manager::close | ( | ) |
Shuts down the network listener and all existing peer connections.
Definition at line 184 of file connection_manager.cpp.
References cbdc::network::tcp_listener::close(), cbdc::network::peer::shutdown(), and cbdc::network::socket_selector::unblock().
Referenced by reset(), cbdc::watchtower::async_client::~async_client(), cbdc::atomizer_client::~atomizer_client(), cbdc::watchtower::blocking_client::~blocking_client(), ~connection_manager(), cbdc::archiver::controller::~controller(), cbdc::atomizer::controller::~controller(), cbdc::shard::controller::~controller(), and cbdc::rpc::tcp_client< Request, Response >::~tcp_client().
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.
endpoints | set of server endpoints to which to establish TCP socket connections. |
error_fatal | true if this function should abort and return false after a single failed connection attempt. |
Definition at line 113 of file connection_manager.cpp.
Referenced by cbdc::rpc::tcp_client< Request, Response >::init().
|
nodiscard |
Determines whether the given peer ID is connected.
peer_id | the peer to check |
Definition at line 264 of file connection_manager.cpp.
|
nodiscard |
Determines if the network is connected to at least one peer.
Definition at line 276 of file connection_manager.cpp.
|
nodiscard |
Collects and return unhandled packets received from connected peers.
Definition at line 49 of file connection_manager.cpp.
|
nodiscard |
Starts a listener to listen for inbound connections on the specified IP address and port.
host | IP address to use. |
port | port to use. |
Definition at line 13 of file connection_manager.cpp.
|
delete |
|
delete |
|
nodiscard |
Returns the number of peers connected to this network.
Definition at line 219 of file connection_manager.cpp.
|
nodiscard |
Listens for and accepts inbound connections.
Definition at line 24 of file connection_manager.cpp.
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().
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.
data | data packet to send. |
peer_id | ID 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().
|
inline |
Serialize the data and transmit it in a packet to the remote host at the specified peer ID.
data | data to serialize and send. |
peer_id | ID of the peer to whom to send data. |
Definition at line 162 of file connection_manager.hpp.
References cbdc::make_shared_buffer(), and send().
|
nodiscard |
Send the provided data to an online peer managed by this network.
data | packet to send. |
Definition at line 236 of file connection_manager.cpp.
Referenced by send_to_one().
|
inlinenodiscard |
Serialize and send the provided data to an online peer managed by this network.
Wraps connection_manager::send_to_one.
data | serializable object to send. |
Definition at line 187 of file connection_manager.hpp.
References cbdc::make_shared_buffer(), and send_to_one().
|
nodiscard |
Connects to the provided endpoints and calls the provided handler for packets received from those endpoints.
endpoints | set of server endpoints to which to establish TCP socket connections. |
handler | function to handle packets from server connections. |
Definition at line 128 of file connection_manager.cpp.
|
nodiscard |
Starts a thread to handle messages from established connections using the specified handler function.
handler | function to handle packets from client connections. |
Definition at line 161 of file connection_manager.cpp.
Referenced by cbdc::rpc::tcp_client< Request, Response >::init().
|
nodiscard |
Establishes a server at the specified endpoint which handles inbound traffic with the specified handler function.
listen_endpoint | the endpoint at which to start the server. |
handler | function to handle packets from client connections. |
Definition at line 138 of file connection_manager.cpp.
|
nodiscard |
Launches a thread that listens for and accepts inbound connections.
Called after listen().
Definition at line 153 of file connection_manager.cpp.