Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
udp_client_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
14namespace kcenon::network::core {
15 class messaging_udp_client;
16}
17
19
47public:
52 explicit udp_client_adapter(std::string_view client_id);
53
57 ~udp_client_adapter() override;
58
59 // Non-copyable, non-movable
64
65 // =========================================================================
66 // i_network_component interface implementation
67 // =========================================================================
68
69 [[nodiscard]] auto is_running() const -> bool override;
70 auto wait_for_stop() -> void override;
71
72 // =========================================================================
73 // i_protocol_client interface implementation
74 // =========================================================================
75
76 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
77 [[nodiscard]] auto stop() -> VoidResult override;
78 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
79 [[nodiscard]] auto is_connected() const -> bool override;
80
81 auto set_observer(std::shared_ptr<interfaces::connection_observer> observer) -> void override;
82
83 auto set_receive_callback(receive_callback_t callback) -> void override;
84 auto set_connected_callback(connected_callback_t callback) -> void override;
85 auto set_disconnected_callback(disconnected_callback_t callback) -> void override;
86 auto set_error_callback(error_callback_t callback) -> void override;
87
88private:
92 auto setup_internal_callbacks() -> void;
93
94 std::string client_id_;
95 std::shared_ptr<core::messaging_udp_client> client_;
96 std::atomic<bool> is_connected_{false};
97
98 mutable std::mutex callbacks_mutex_;
99 std::shared_ptr<interfaces::connection_observer> observer_;
104};
105
106} // namespace kcenon::network::internal::adapters
Unified interface for all protocol client implementations.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
Adapter that wraps messaging_udp_client to implement i_protocol_client.
auto is_connected() const -> bool override
Checks if the client is connected to the server.
auto stop() -> VoidResult override
Stops the client and closes the connection.
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server.
auto is_running() const -> bool override
Checks if the component is currently running.
udp_client_adapter & operator=(udp_client_adapter &&)=delete
~udp_client_adapter() override
Destructor ensures proper cleanup.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
std::shared_ptr< interfaces::connection_observer > observer_
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
udp_client_adapter(std::string_view client_id)
Constructs an adapter with a unique client ID.
udp_client_adapter & operator=(const udp_client_adapter &)=delete
auto wait_for_stop() -> void override
Blocks until the component has stopped.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge UDP callbacks to i_protocol_client callbacks.
udp_client_adapter(const udp_client_adapter &)=delete
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
std::shared_ptr< core::messaging_udp_client > client_
Protocol-specific client interface extending i_client.