6#ifndef OPENCBDC_TX_SRC_NETWORK_CONNECTION_MANAGER_H_
7#define OPENCBDC_TX_SRC_NETWORK_CONNECTION_MANAGER_H_
17#include <condition_variable>
21#include <shared_mutex>
22#include <sys/socket.h>
76 [[nodiscard]]
auto pump() -> bool;
81 void broadcast(
const std::shared_ptr<buffer>& data);
103 auto add(std::unique_ptr<tcp_socket> sock,
104 bool attempt_reconnect =
true) ->
peer_id_t;
111 bool error_fatal =
true) -> bool;
123 -> std::optional<std::thread>;
135 -> std::optional<std::thread>;
155 void send(
const std::shared_ptr<buffer>& data,
peer_id_t peer_id);
161 template<
typename Ta>
164 return send(pkt, peer_id);
179 [[nodiscard]]
auto send_to_one(
const std::shared_ptr<buffer>& data)
208 ~m_peer_t() =
default;
210 auto operator=(
const m_peer_t& other) -> m_peer_t& =
default;
211 m_peer_t(
const m_peer_t& other) =
default;
213 auto operator=(m_peer_t&& other)
noexcept -> m_peer_t& =
default;
214 m_peer_t(m_peer_t&& other)
noexcept =
default;
216 std::shared_ptr<peer> m_peer;
220 std::vector<m_peer_t> m_peers;
221 std::atomic<peer_id_t> m_next_peer_id{0};
223 std::shared_mutex m_peer_mutex;
225 std::atomic_bool m_running{
true};
227 std::mutex m_async_recv_mut;
228 std::condition_variable m_async_recv_cv;
229 std::vector<std::queue<message_t>> m_async_recv_queues;
230 bool m_async_recv_data{
false};
232 socket_selector m_listen_selector;
234 std::random_device m_r{cbdc::config::random_source};
235 std::default_random_engine m_rnd{m_r()};
Manages a group of peer s.
auto start_handler(const packet_handler_t &handler) -> std::thread
Starts a thread to handle messages from established connections using the specified handler function.
auto pump() -> bool
Listens for and accepts inbound connections.
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 handl...
connection_manager(connection_manager &&)=delete
connection_manager()=default
auto send_to_one(const T &data) -> bool
Serialize and send the provided data to an online peer managed by this network.
void reset()
Resets the network instance to a fresh state.
auto connected(peer_id_t peer_id) -> bool
Determines whether the given peer ID is connected.
auto cluster_connect(const std::vector< endpoint_t > &endpoints, bool error_fatal=true) -> bool
Establishes connections to the provided list of endpoints.
void broadcast(const Ta &data)
Serialize the data and broadcast it to all peers.
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 end...
void close()
Shuts down the network listener and all existing peer connections.
auto start_server_listener() -> std::thread
Launches a thread that listens for and accepts inbound connections.
auto operator=(connection_manager &&) -> connection_manager &=delete
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 send_to_one(const std::shared_ptr< buffer > &data) -> bool
Send the provided data to an online peer managed by this network.
connection_manager(const connection_manager &)=delete
auto peer_count() -> size_t
Returns the number of peers connected to this network.
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 operator=(const connection_manager &) -> connection_manager &=delete
auto handle_messages() -> std::vector< message_t >
Collects and return unhandled packets received from connected peers.
void send(const std::shared_ptr< buffer > &data, peer_id_t peer_id)
Sends the provided data to the specified peer.
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 connected_to_one() -> bool
Determines if the network is connected to at least one peer.
void broadcast(const std::shared_ptr< buffer > &data)
Sends the provided data to all added peers.
Listens for incoming TCP connections on a given endpoint.
Tools for reading options from a configuration file and building application-specific parameter sets ...
size_t peer_id_t
Peer IDs within a connection_manager.
std::function< std::optional< buffer >(message_t &&)> packet_handler_t
Function type for packet handler callbacks.
std::string ip_address
An IP addresses.
std::pair< ip_address, port_number_t > endpoint_t
[host name, port number].
auto make_shared_buffer(const T &obj) -> std::shared_ptr< cbdc::buffer >
Serialize object into std::shared_ptr<cbdc::buffer> using a cbdc::buffer_serializer.
std::shared_ptr< buffer > m_pkt
Packet data.
peer_id_t m_peer_id
Peer ID that sent packet.