Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
udp_connection_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
9
10#include <atomic>
11#include <memory>
12#include <mutex>
13#include <string>
14
16
39public:
44 explicit udp_connection_adapter(std::string_view connection_id);
45
49 ~udp_connection_adapter() override;
50
51 // Non-copyable, non-movable (contains mutex)
56
57 // =========================================================================
58 // i_transport interface implementation
59 // =========================================================================
60
61 [[nodiscard]] auto send(std::span<const std::byte> data) -> VoidResult override;
62 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
63 [[nodiscard]] auto is_connected() const noexcept -> bool override;
64 [[nodiscard]] auto id() const noexcept -> std::string_view override;
65 [[nodiscard]] auto remote_endpoint() const noexcept -> endpoint_info override;
66 [[nodiscard]] auto local_endpoint() const noexcept -> endpoint_info override;
67
68 // =========================================================================
69 // i_connection interface implementation
70 // =========================================================================
71
72 [[nodiscard]] auto connect(const endpoint_info& endpoint) -> VoidResult override;
73 [[nodiscard]] auto connect(std::string_view url) -> VoidResult override;
74 auto close() noexcept -> void override;
75 auto set_callbacks(connection_callbacks callbacks) -> void override;
76 auto set_options(connection_options options) -> void override;
77 auto set_timeout(std::chrono::milliseconds timeout) -> void override;
78 [[nodiscard]] auto is_connecting() const noexcept -> bool override;
79 auto wait_for_stop() -> void override;
80
81private:
85 auto setup_internal_callbacks() -> void;
86
87 std::string connection_id_;
88 std::shared_ptr<core::messaging_udp_client> client_;
89
90 mutable std::mutex callbacks_mutex_;
92
93 mutable std::mutex endpoint_mutex_;
96
97 std::atomic<bool> is_connecting_{false};
99};
100
101} // namespace kcenon::network::unified::adapters
Adapter that wraps messaging_udp_client to implement i_connection.
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
udp_connection_adapter & operator=(const udp_connection_adapter &)=delete
auto is_connected() const noexcept -> bool override
Checks if the transport is currently connected.
auto local_endpoint() const noexcept -> endpoint_info override
Gets the local endpoint information.
auto set_callbacks(connection_callbacks callbacks) -> void override
Sets all connection callbacks at once.
auto connect(const endpoint_info &endpoint) -> VoidResult override
Connects to a remote endpoint using host/port.
auto set_options(connection_options options) -> void override
Sets connection options.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge to unified callbacks.
auto set_timeout(std::chrono::milliseconds timeout) -> void override
Sets the connection timeout.
udp_connection_adapter(std::string_view connection_id)
Constructs an adapter with a unique connection ID.
auto close() noexcept -> void override
Closes the connection gracefully.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
std::shared_ptr< core::messaging_udp_client > client_
auto remote_endpoint() const noexcept -> endpoint_info override
Gets the remote endpoint information.
udp_connection_adapter & operator=(udp_connection_adapter &&)=delete
auto is_connecting() const noexcept -> bool override
Checks if the connection is in the process of connecting.
udp_connection_adapter(udp_connection_adapter &&)=delete
~udp_connection_adapter() override
Destructor ensures proper cleanup.
udp_connection_adapter(const udp_connection_adapter &)=delete
Core interface for active network connections.
Core interface for active network connections.
UDP client class.
std::mutex mutex
Callback functions for connection events.
Definition types.h:154
Configuration options for connections.
Definition types.h:198
Network endpoint information (host/port or URL)
Definition types.h:56