Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
tcp_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
32public:
37 explicit tcp_connection_adapter(std::string_view connection_id);
38
42 ~tcp_connection_adapter() override;
43
44 // Non-copyable, non-movable (contains mutex)
49
50 // =========================================================================
51 // i_transport interface implementation
52 // =========================================================================
53
54 [[nodiscard]] auto send(std::span<const std::byte> data) -> VoidResult override;
55 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
56 [[nodiscard]] auto is_connected() const noexcept -> bool override;
57 [[nodiscard]] auto id() const noexcept -> std::string_view override;
58 [[nodiscard]] auto remote_endpoint() const noexcept -> endpoint_info override;
59 [[nodiscard]] auto local_endpoint() const noexcept -> endpoint_info override;
60
61 // =========================================================================
62 // i_connection interface implementation
63 // =========================================================================
64
65 [[nodiscard]] auto connect(const endpoint_info& endpoint) -> VoidResult override;
66 [[nodiscard]] auto connect(std::string_view url) -> VoidResult override;
67 auto close() noexcept -> void override;
68 auto set_callbacks(connection_callbacks callbacks) -> void override;
69 auto set_options(connection_options options) -> void override;
70 auto set_timeout(std::chrono::milliseconds timeout) -> void override;
71 [[nodiscard]] auto is_connecting() const noexcept -> bool override;
72 auto wait_for_stop() -> void override;
73
74private:
78 auto setup_internal_callbacks() -> void;
79
80 std::string connection_id_;
81 std::shared_ptr<core::tcp_client> client_;
82
83 mutable std::mutex callbacks_mutex_;
85
86 mutable std::mutex endpoint_mutex_;
89
90 std::atomic<bool> is_connecting_{false};
92};
93
94} // namespace kcenon::network::unified::adapters
Adapter that wraps unified_messaging_client to implement i_connection.
tcp_connection_adapter & operator=(tcp_connection_adapter &&)=delete
tcp_connection_adapter & operator=(const tcp_connection_adapter &)=delete
auto set_callbacks(connection_callbacks callbacks) -> void override
Sets all connection callbacks at once.
tcp_connection_adapter(std::string_view connection_id)
Constructs an adapter with a unique connection ID.
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 connect(const endpoint_info &endpoint) -> VoidResult override
Connects to a remote endpoint using host/port.
auto close() noexcept -> void override
Closes the connection gracefully.
auto is_connecting() const noexcept -> bool override
Checks if the connection is in the process of connecting.
tcp_connection_adapter(const tcp_connection_adapter &)=delete
auto is_connected() const noexcept -> bool override
Checks if the transport is currently connected.
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
auto set_timeout(std::chrono::milliseconds timeout) -> void override
Sets the connection timeout.
~tcp_connection_adapter() override
Destructor ensures proper cleanup.
auto remote_endpoint() const noexcept -> endpoint_info override
Gets the remote endpoint information.
tcp_connection_adapter(tcp_connection_adapter &&)=delete
auto wait_for_stop() -> void override
Blocks until the component has stopped.
auto local_endpoint() const noexcept -> endpoint_info override
Gets the local endpoint information.
Core interface for active network connections.
Core interface for active network connections.
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