Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_server_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
8
9#include <atomic>
10#include <memory>
11#include <mutex>
12#include <string>
13#include <unordered_map>
14
15namespace kcenon::network::core {
16 class http_server;
17}
18
20
32public:
34 std::string_view session_id,
35 std::string_view client_address,
36 uint16_t client_port,
37 std::weak_ptr<core::http_server> server);
38
39 [[nodiscard]] auto id() const -> std::string_view override;
40 [[nodiscard]] auto is_connected() const -> bool override;
41 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
42 auto close() -> void override;
43
48 auto set_response_data(std::vector<uint8_t> data) -> void;
49
54 [[nodiscard]] auto get_response_data() const -> const std::vector<uint8_t>&;
55
56private:
57 std::string session_id_;
58 std::string client_address_;
59 uint16_t client_port_;
60 std::weak_ptr<core::http_server> server_;
61 std::vector<uint8_t> response_data_;
62 std::atomic<bool> is_connected_{true};
63 mutable std::mutex data_mutex_;
64};
65
99public:
104 explicit http_server_adapter(std::string_view server_id);
105
109 ~http_server_adapter() override;
110
111 // Non-copyable, non-movable
116
117 // =========================================================================
118 // i_network_component interface implementation
119 // =========================================================================
120
121 [[nodiscard]] auto is_running() const -> bool override;
122 auto wait_for_stop() -> void override;
123
124 // =========================================================================
125 // i_protocol_server interface implementation
126 // =========================================================================
127
128 [[nodiscard]] auto start(uint16_t port) -> VoidResult override;
129 [[nodiscard]] auto stop() -> VoidResult override;
130 [[nodiscard]] auto connection_count() const -> size_t override;
131
132 auto set_connection_callback(connection_callback_t callback) -> void override;
133 auto set_disconnection_callback(disconnection_callback_t callback) -> void override;
134 auto set_receive_callback(receive_callback_t callback) -> void override;
135 auto set_error_callback(error_callback_t callback) -> void override;
136
137private:
141 auto setup_internal_routes() -> void;
142
146 static auto make_session_id(const std::string& address, uint16_t port, uint64_t request_id) -> std::string;
147
151 auto get_or_create_session(const std::string& address, uint16_t port)
152 -> std::shared_ptr<http_request_session>;
153
157 auto remove_session(const std::string& session_id) -> void;
158
159 std::string server_id_;
160 std::shared_ptr<core::http_server> server_;
161
167
168 mutable std::mutex sessions_mutex_;
169 std::unordered_map<std::string, std::shared_ptr<http_request_session>> sessions_;
170 std::atomic<uint64_t> request_counter_{0};
171 std::atomic<bool> is_running_{false};
172};
173
174} // namespace kcenon::network::internal::adapters
Unified interface for all protocol server implementations.
std::function< void(std::string_view)> disconnection_callback_t
Callback type for disconnections (session_id)
std::function< void(std::string_view, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data (session_id, data)
std::function< void(std::string_view, std::error_code)> error_callback_t
Callback type for errors (session_id, error)
std::function< void(std::shared_ptr< i_session >)> connection_callback_t
Callback type for new connections.
Interface for a single client session on the server side.
Definition i_session.h:49
Session wrapper for HTTP request connections implementing i_session.
auto is_connected() const -> bool override
Checks if the session is currently connected.
http_request_session(std::string_view session_id, std::string_view client_address, uint16_t client_port, std::weak_ptr< core::http_server > server)
auto id() const -> std::string_view override
Gets the unique identifier for this session.
auto get_response_data() const -> const std::vector< uint8_t > &
Gets the stored response data.
auto set_response_data(std::vector< uint8_t > data) -> void
Stores the response data to be sent back to client.
Adapter that wraps http_server to implement i_protocol_server.
std::unordered_map< std::string, std::shared_ptr< http_request_session > > sessions_
auto wait_for_stop() -> void override
Blocks until the component has stopped.
http_server_adapter(std::string_view server_id)
Constructs an adapter with a unique server ID.
~http_server_adapter() override
Destructor ensures proper cleanup.
auto set_connection_callback(connection_callback_t callback) -> void override
Sets the callback for new connections.
http_server_adapter & operator=(http_server_adapter &&)=delete
auto is_running() const -> bool override
Checks if the component is currently running.
auto setup_internal_routes() -> void
Sets up internal HTTP routes to bridge to i_protocol_server callbacks.
auto remove_session(const std::string &session_id) -> void
Removes a session after request processing.
static auto make_session_id(const std::string &address, uint16_t port, uint64_t request_id) -> std::string
Creates a unique session ID from request info.
auto set_disconnection_callback(disconnection_callback_t callback) -> void override
Sets the callback for disconnections.
auto get_or_create_session(const std::string &address, uint16_t port) -> std::shared_ptr< http_request_session >
Gets or creates a session for the given request.
http_server_adapter(const http_server_adapter &)=delete
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto stop() -> VoidResult override
Stops the server and closes all connections.
auto connection_count() const -> size_t override
Gets the number of active client connections.
http_server_adapter & operator=(const http_server_adapter &)=delete
auto start(uint16_t port) -> VoidResult override
Starts the server and begins listening for connections.
Protocol-specific server interface extending i_server.
std::mutex mutex