18 std::string_view session_id,
19 std::string_view address,
21 std::weak_ptr<core::messaging_udp_server>
server)
22 : session_id_(session_id)
25 , server_(std::move(
server))
44 auto server = server_.lock();
49 "Server no longer available",
50 "udp_endpoint_session::send"
55 return server->send_to(endpoint, std::move(data));
69 : server_id_(server_id)
70 , server_(std::make_shared<core::messaging_udp_server>(std::string(server_id)))
96 server_->wait_for_stop();
110 "Server not initialized",
111 "udp_server_adapter::start"
114 return server_->start(port);
123 "Server not initialized",
124 "udp_server_adapter::stop"
128 auto result = server_->stop();
132 std::lock_guard<std::mutex> lock(sessions_mutex_);
147 std::lock_guard<std::mutex> lock(callbacks_mutex_);
148 connection_callback_ = std::move(callback);
153 std::lock_guard<std::mutex> lock(callbacks_mutex_);
154 disconnection_callback_ = std::move(callback);
159 std::lock_guard<std::mutex> lock(callbacks_mutex_);
160 receive_callback_ = std::move(callback);
165 std::lock_guard<std::mutex> lock(callbacks_mutex_);
166 error_callback_ = std::move(callback);
182 server_->set_receive_callback(
183 [
this](
const std::vector<uint8_t>& data,
187 auto session = get_or_create_session(endpoint.
address, endpoint.
port);
188 std::string session_id(session->id());
193 std::lock_guard<std::mutex> lock(callbacks_mutex_);
194 callback_copy = receive_callback_;
198 callback_copy(session_id, data);
203 server_->set_error_callback(
204 [
this](std::error_code ec)
209 std::lock_guard<std::mutex> lock(callbacks_mutex_);
210 callback_copy = error_callback_;
214 callback_copy(
"", ec);
221 std::ostringstream oss;
222 oss << address <<
":" << port;
227 -> std::shared_ptr<interfaces::i_session>
229 std::string session_id = make_session_id(address, port);
232 std::lock_guard<std::mutex> lock(sessions_mutex_);
234 auto it = sessions_.find(session_id);
235 if (it != sessions_.end())
241 auto session = std::make_shared<udp_endpoint_session>(
242 session_id, address, port, server_);
243 sessions_[session_id] = session;
248 std::lock_guard<std::mutex> cb_lock(callbacks_mutex_);
249 callback_copy = connection_callback_;
254 callback_copy(session);
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.
auto id() const -> std::string_view override
Gets the unique identifier for this session.
std::weak_ptr< core::messaging_udp_server > server_
auto close() -> void override
Closes the session.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the client.
auto is_connected() const -> bool override
Checks if the session is currently connected.
udp_endpoint_session(std::string_view session_id, std::string_view address, uint16_t port, std::weak_ptr< core::messaging_udp_server > server)
static auto make_session_id(const std::string &address, uint16_t port) -> std::string
Creates a unique session ID from endpoint info.
std::unordered_map< std::string, std::shared_ptr< udp_endpoint_session > > sessions_
auto set_connection_callback(connection_callback_t callback) -> void override
Sets the callback for new connections.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
std::mutex sessions_mutex_
std::shared_ptr< core::messaging_udp_server > server_
~udp_server_adapter() override
Destructor ensures proper cleanup.
auto connection_count() const -> size_t override
Gets the number of active client connections.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge UDP callbacks to i_protocol_server callbacks.
auto is_running() const -> bool override
Checks if the component is currently running.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto set_disconnection_callback(disconnection_callback_t callback) -> void override
Sets the callback for disconnections.
auto start(uint16_t port) -> VoidResult override
Starts the server and begins listening for connections.
udp_server_adapter(std::string_view server_id)
Constructs an adapter with a unique server ID.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto stop() -> VoidResult override
Stops the server and closes all connections.
auto get_or_create_session(const std::string &address, uint16_t port) -> std::shared_ptr< interfaces::i_session >
Gets or creates a session for the given endpoint.
constexpr int internal_error
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Endpoint information for UDP datagrams.
uint16_t port
Port number.
std::string address
IP address string.