13 : connection_id_(connection_id)
14 , client_(std::make_shared<core::tcp_client>(connection_id))
26 if (!client_ || !client_->is_connected()) {
28 static_cast<int>(std::errc::not_connected),
29 "Connection is not established"
33 std::vector<uint8_t> buffer(data.size());
34 std::memcpy(buffer.data(), data.data(), data.size());
36 return client_->send_packet(std::move(buffer));
41 if (!client_ || !client_->is_connected()) {
43 static_cast<int>(std::errc::not_connected),
44 "Connection is not established"
48 return client_->send_packet(std::move(data));
77 static_cast<int>(std::errc::invalid_argument),
78 "Client is not initialized"
82 if (client_->is_connected() || client_->is_running()) {
84 static_cast<int>(std::errc::already_connected),
85 "Already connected or connecting"
90 std::lock_guard lock(endpoint_mutex_);
91 remote_endpoint_ = endpoint;
94 is_connecting_ =
true;
95 auto result = client_->start_client(endpoint.host, endpoint.port);
97 if (!result.is_ok()) {
98 is_connecting_ =
false;
107 std::string url_str(url);
110 const std::string tcp_prefix =
"tcp://";
111 if (url_str.substr(0, tcp_prefix.size()) == tcp_prefix) {
112 url_str = url_str.substr(tcp_prefix.size());
116 auto colon_pos = url_str.rfind(
':');
117 if (colon_pos == std::string::npos) {
119 static_cast<int>(std::errc::invalid_argument),
120 "URL must contain port number (format: host:port)"
124 std::string host = url_str.substr(0, colon_pos);
125 std::string port_str = url_str.substr(colon_pos + 1);
129 port =
static_cast<uint16_t
>(std::stoi(port_str));
132 static_cast<int>(std::errc::invalid_argument),
133 "Invalid port number in URL"
151 std::lock_guard lock(callbacks_mutex_);
152 callbacks_ = std::move(callbacks);
156 setup_internal_callbacks();
168 options_.connect_timeout = timeout;
179 client_->wait_for_stop();
190 client_->set_receive_callback([
this](
const std::vector<uint8_t>& data) {
191 std::lock_guard lock(callbacks_mutex_);
192 if (callbacks_.on_data) {
193 std::span<const std::byte> byte_span(
194 reinterpret_cast<const std::byte*
>(data.data()),
197 callbacks_.on_data(byte_span);
202 client_->set_connected_callback([
this]() {
203 is_connecting_ =
false;
205 std::lock_guard lock(callbacks_mutex_);
206 if (callbacks_.on_connected) {
207 callbacks_.on_connected();
212 client_->set_disconnected_callback([
this]() {
213 is_connecting_ =
false;
215 std::lock_guard lock(callbacks_mutex_);
216 if (callbacks_.on_disconnected) {
217 callbacks_.on_disconnected();
222 client_->set_error_callback([
this](std::error_code ec) {
223 is_connecting_ =
false;
225 std::lock_guard lock(callbacks_mutex_);
226 if (callbacks_.on_error) {
227 callbacks_.on_error(ec);
auto id() const noexcept -> std::string_view override
Gets the unique identifier for this transport/connection.
endpoint_info local_endpoint_
auto set_callbacks(connection_callbacks callbacks) -> void override
Sets all connection callbacks at once.
std::string connection_id_
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.
auto is_connected() const noexcept -> bool override
Checks if the transport is currently connected.
std::atomic< bool > is_connecting_
std::mutex endpoint_mutex_
std::shared_ptr< core::tcp_client > client_
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
endpoint_info 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.
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.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Callback functions for connection events.
Configuration options for connections.
Network endpoint information (host/port or URL)