13 : connection_id_(connection_id)
14 , client_(std::make_shared<core::messaging_udp_client>(connection_id))
26 if (!client_ || !client_->is_running()) {
28 static_cast<int>(std::errc::not_connected),
29 "UDP client is not running"
33 std::vector<uint8_t> buffer(data.size());
34 std::memcpy(buffer.data(), data.data(), data.size());
36 return client_->send(std::move(buffer));
41 if (!client_ || !client_->is_running()) {
43 static_cast<int>(std::errc::not_connected),
44 "UDP client is not running"
48 return client_->send(std::move(data));
78 static_cast<int>(std::errc::invalid_argument),
79 "Client is not initialized"
83 if (client_->is_running()) {
85 static_cast<int>(std::errc::already_connected),
91 std::lock_guard lock(endpoint_mutex_);
92 remote_endpoint_ = endpoint;
95 is_connecting_ =
true;
96 auto result = client_->start_client(endpoint.host, endpoint.port);
99 is_connecting_ =
false;
101 std::lock_guard lock(callbacks_mutex_);
102 if (callbacks_.on_connected) {
103 callbacks_.on_connected();
106 is_connecting_ =
false;
115 std::string url_str(url);
118 const std::string udp_prefix =
"udp://";
119 if (url_str.substr(0, udp_prefix.size()) == udp_prefix) {
120 url_str = url_str.substr(udp_prefix.size());
124 auto colon_pos = url_str.rfind(
':');
125 if (colon_pos == std::string::npos) {
127 static_cast<int>(std::errc::invalid_argument),
128 "URL must contain port number (format: host:port)"
132 std::string host = url_str.substr(0, colon_pos);
133 std::string port_str = url_str.substr(colon_pos + 1);
137 port =
static_cast<uint16_t
>(std::stoi(port_str));
140 static_cast<int>(std::errc::invalid_argument),
141 "Invalid port number in URL"
164 std::lock_guard lock(callbacks_mutex_);
165 callbacks_ = std::move(callbacks);
169 setup_internal_callbacks();
179 options_.connect_timeout = timeout;
190 client_->wait_for_stop();
201 client_->set_receive_callback(
202 [
this](
const std::vector<uint8_t>& data,
206 std::lock_guard ep_lock(endpoint_mutex_);
210 std::lock_guard lock(callbacks_mutex_);
211 if (callbacks_.on_data) {
212 std::span<const std::byte> byte_span(
213 reinterpret_cast<const std::byte*
>(data.data()),
216 callbacks_.on_data(byte_span);
221 client_->set_error_callback([
this](std::error_code ec) {
222 is_connecting_ =
false;
224 std::lock_guard lock(callbacks_mutex_);
225 if (callbacks_.on_error) {
226 callbacks_.on_error(ec);
std::mutex callbacks_mutex_
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
endpoint_info local_endpoint_
auto id() const noexcept -> std::string_view override
Gets the unique identifier for this transport/connection.
std::atomic< bool > is_connecting_
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.
endpoint_info remote_endpoint_
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.
std::string connection_id_
auto close() noexcept -> void override
Closes the connection gracefully.
connection_callbacks callbacks_
std::mutex endpoint_mutex_
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.
auto is_connecting() const noexcept -> bool override
Checks if the connection is in the process of connecting.
~udp_connection_adapter() override
Destructor ensures proper cleanup.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Endpoint information for UDP datagrams.
Callback functions for connection events.
std::function< void()> on_disconnected
Called when connection is closed.
Configuration options for connections.
Network endpoint information (host/port or URL)