Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ConnectionManager Class Reference

Advanced example: Managing accepted connections. More...

Collaboration diagram for ConnectionManager:
Collaboration graph

Public Member Functions

void start (std::unique_ptr< unified::i_listener > listener)
 
void broadcast (std::span< const std::byte > data)
 

Private Member Functions

void handle_new_connection (std::unique_ptr< unified::i_connection > conn)
 

Private Attributes

std::unique_ptr< unified::i_listener > listener_
 
std::map< std::string, std::unique_ptr< unified::i_connection > > connections_
 

Detailed Description

Advanced example: Managing accepted connections.

Shows how to get ownership of accepted connections for custom management.

Definition at line 109 of file unified_server_example.cpp.

Member Function Documentation

◆ broadcast()

void ConnectionManager::broadcast ( std::span< const std::byte > data)
inline

Definition at line 123 of file unified_server_example.cpp.

123 {
124 for (auto& [id, conn] : connections_) {
125 conn->send(data);
126 }
127 }
std::map< std::string, std::unique_ptr< unified::i_connection > > connections_

References connections_.

◆ handle_new_connection()

void ConnectionManager::handle_new_connection ( std::unique_ptr< unified::i_connection > conn)
inlineprivate

Definition at line 130 of file unified_server_example.cpp.

130 {
131 // Get connection ID from endpoint info
132 auto endpoint = conn->remote_endpoint();
133 std::string id = std::string(endpoint.host) + ":" +
134 std::to_string(endpoint.port);
135
136 std::cout << "Managing connection: " << id << "\n";
137
138 // Set up connection-specific callbacks
139 conn->set_callbacks({
140 .on_data = [id](std::span<const std::byte> data) {
141 std::cout << "Data from " << id << "\n";
142 },
143 .on_disconnected = [this, id]() {
144 connections_.erase(id);
145 std::cout << "Removed connection: " << id << "\n";
146 }
147 });
148
149 // Store connection
150 connections_[id] = std::move(conn);
151 }
virtual auto set_callbacks(connection_callbacks callbacks) -> void=0
Sets all connection callbacks at once.
virtual auto remote_endpoint() const noexcept -> endpoint_info=0
Gets the remote endpoint information.

References connections_.

Referenced by start().

Here is the caller graph for this function:

◆ start()

void ConnectionManager::start ( std::unique_ptr< unified::i_listener > listener)
inline

Definition at line 111 of file unified_server_example.cpp.

111 {
112 listener_ = std::move(listener);
113
114 // Use accept callback to get connection ownership
116 [this](std::unique_ptr<unified::i_connection> conn) {
117 handle_new_connection(std::move(conn));
118 });
119
120 listener_->start(8080);
121 }
void handle_new_connection(std::unique_ptr< unified::i_connection > conn)
std::unique_ptr< unified::i_listener > listener_
virtual auto set_accept_callback(accept_callback_t callback) -> void=0
Sets the accept callback for new connections.
virtual auto start(const endpoint_info &bind_address) -> VoidResult=0
Starts listening for incoming connections.

References handle_new_connection(), listener_, kcenon::network::unified::i_listener::set_accept_callback(), and kcenon::network::unified::i_listener::start().

Here is the call graph for this function:

Member Data Documentation

◆ connections_

std::map<std::string, std::unique_ptr<unified::i_connection> > ConnectionManager::connections_
private

Definition at line 154 of file unified_server_example.cpp.

Referenced by broadcast(), and handle_new_connection().

◆ listener_

std::unique_ptr<unified::i_listener> ConnectionManager::listener_
private

Definition at line 153 of file unified_server_example.cpp.

Referenced by start().


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