Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_session_manager.h
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2025, kcenon
5All rights reserved.
6*****************************************************************************/
7
8#pragma once
9
11
12#include <atomic>
13#include <chrono>
14#include <functional>
15#include <memory>
16#include <shared_mutex>
17#include <string>
18#include <unordered_map>
19#include <vector>
20
21namespace kcenon::network::core {
22
28 size_t max_sessions{1000};
29 std::chrono::milliseconds idle_timeout{std::chrono::minutes(5)};
30 std::chrono::milliseconds cleanup_interval{std::chrono::seconds(30)};
33};
34
85public:
90
96
98
99 // Non-copyable, non-movable
104
105 // =========================================================================
106 // Connection Acceptance
107 // =========================================================================
108
113 [[nodiscard]] auto can_accept_connection() const -> bool;
114
119 [[nodiscard]] auto is_backpressure_active() const -> bool;
120
121 // =========================================================================
122 // Session CRUD - Type-Erased API
123 // =========================================================================
124
135 template <typename SessionType>
136 auto add_session(std::shared_ptr<SessionType> session,
137 const std::string& session_id = "") -> bool {
138 if (!can_accept_connection()) {
140 return false;
141 }
142
143 auto handle = session_handle(std::move(session));
144 return add_session_impl(std::move(handle), session_id);
145 }
146
153 auto add_session(session_handle handle, const std::string& session_id = "")
154 -> bool;
155
163 template <typename SessionType>
164 auto add_session_with_id(std::shared_ptr<SessionType> session,
165 const std::string& session_id = "") -> std::string {
166 if (!can_accept_connection()) {
168 return "";
169 }
170
171 auto handle = session_handle(std::move(session));
172 return add_session_with_id_impl(std::move(handle), session_id);
173 }
174
180 auto remove_session(const std::string& session_id) -> bool;
181
190 [[nodiscard]] auto get_session(const std::string& session_id) -> session_handle*;
191
197 [[nodiscard]] auto get_session(const std::string& session_id) const
198 -> const session_handle*;
199
209 auto with_session(const std::string& session_id,
210 const std::function<void(session_handle&)>& callback) -> bool;
211
217 [[nodiscard]] auto has_session(const std::string& session_id) const -> bool;
218
223 [[nodiscard]] auto get_all_session_ids() const -> std::vector<std::string>;
224
225 // =========================================================================
226 // Iteration
227 // =========================================================================
228
236 auto for_each(const std::function<void(session_handle&)>& callback) -> void;
237
242 auto for_each(const std::function<void(const session_handle&)>& callback) const
243 -> void;
244
250 auto broadcast(std::vector<uint8_t> data) -> size_t;
251
252 // =========================================================================
253 // Activity Tracking & Cleanup
254 // =========================================================================
255
261 auto update_activity(const std::string& session_id) -> bool;
262
267 auto cleanup_idle_sessions() -> size_t;
268
269 // =========================================================================
270 // Lifecycle Management
271 // =========================================================================
272
278 auto clear_all_sessions() -> void;
279
283 auto stop_all_sessions() -> void;
284
285 // =========================================================================
286 // Metrics
287 // =========================================================================
288
293 [[nodiscard]] auto get_session_count() const -> size_t;
294
299 [[nodiscard]] auto get_total_accepted() const -> uint64_t;
300
305 [[nodiscard]] auto get_total_rejected() const -> uint64_t;
306
311 [[nodiscard]] auto get_total_cleaned_up() const -> uint64_t;
312
317 [[nodiscard]] auto get_utilization() const -> double;
318
319 // =========================================================================
320 // Configuration
321 // =========================================================================
322
327 auto set_max_sessions(size_t max_sessions) -> void;
328
333 [[nodiscard]] auto get_config() const -> const unified_session_config&;
334
335 // =========================================================================
336 // Statistics
337 // =========================================================================
338
343 struct stats {
351 std::chrono::milliseconds idle_timeout;
352 };
353
358 [[nodiscard]] auto get_stats() const -> stats;
359
360 // =========================================================================
361 // ID Generation
362 // =========================================================================
363
369 static auto generate_id(const std::string& prefix = "session_") -> std::string;
370
371private:
372 class impl;
373 std::unique_ptr<impl> impl_;
374
375 auto add_session_impl(session_handle handle, const std::string& session_id)
376 -> bool;
378 const std::string& session_id) -> std::string;
379 auto increment_rejected() -> void;
380};
381
382} // namespace kcenon::network::core
Value-semantic wrapper for type-erased sessions.
PIMPL implementation hiding all template-related code.
Type-erased session manager that handles any session type.
auto get_total_cleaned_up() const -> uint64_t
Get total cleaned up sessions.
auto is_backpressure_active() const -> bool
Check if backpressure should be applied.
auto get_all_session_ids() const -> std::vector< std::string >
Get all session IDs.
auto add_session_with_id_impl(session_handle handle, const std::string &session_id) -> std::string
unified_session_manager & operator=(unified_session_manager &&)=delete
auto broadcast(std::vector< uint8_t > data) -> size_t
Broadcast data to all connected sessions.
auto cleanup_idle_sessions() -> size_t
Cleanup idle sessions that exceeded idle_timeout.
unified_session_manager & operator=(const unified_session_manager &)=delete
auto update_activity(const std::string &session_id) -> bool
Update activity timestamp for a session.
auto with_session(const std::string &session_id, const std::function< void(session_handle &)> &callback) -> bool
Execute a callback with a session (safer than get_session)
auto get_total_accepted() const -> uint64_t
Get total accepted connections.
auto get_stats() const -> stats
Get comprehensive statistics.
auto stop_all_sessions() -> void
Stop all sessions (alias for clear_all_sessions)
auto add_session(std::shared_ptr< SessionType > session, const std::string &session_id="") -> bool
Add a session using type erasure.
auto set_max_sessions(size_t max_sessions) -> void
Set maximum sessions.
auto get_utilization() const -> double
Get current session utilization.
auto has_session(const std::string &session_id) const -> bool
Check if a session exists.
unified_session_manager()
Constructs a unified session manager with default configuration.
auto remove_session(const std::string &session_id) -> bool
Remove session by ID.
auto for_each(const std::function< void(session_handle &)> &callback) -> void
Execute a callback for each session.
auto add_session_with_id(std::shared_ptr< SessionType > session, const std::string &session_id="") -> std::string
Add a session and return the assigned ID.
unified_session_manager(unified_session_manager &&)=delete
unified_session_manager(const unified_session_manager &)=delete
auto get_total_rejected() const -> uint64_t
Get total rejected connections.
auto add_session_impl(session_handle handle, const std::string &session_id) -> bool
auto get_session(const std::string &session_id) -> session_handle *
Get session handle by ID (non-owning reference)
auto can_accept_connection() const -> bool
Check if new connection can be accepted.
auto get_session_count() const -> size_t
Get current session count.
auto get_config() const -> const unified_session_config &
Get current configuration.
static auto generate_id(const std::string &prefix="session_") -> std::string
Generate a unique session ID.
tracing_config config
Definition exporters.cpp:29
Configuration for unified session management.