12 : client_id_(client_id)
13 , client_(std::make_shared<core::messaging_udp_client>(std::string(client_id)))
39 client_->wait_for_stop();
53 "Client not initialized",
54 "udp_client_adapter::start"
58 auto result = client_->start(host, port);
61 is_connected_.store(
true, std::memory_order_release);
64 std::shared_ptr<interfaces::connection_observer> observer_copy;
67 std::lock_guard<std::mutex> lock(callbacks_mutex_);
68 observer_copy = observer_;
69 callback_copy = connected_callback_;
74 observer_copy->on_connected();
91 "Client not initialized",
92 "udp_client_adapter::stop"
96 bool was_connected = is_connected_.exchange(
false, std::memory_order_acq_rel);
97 auto result = client_->stop();
102 std::shared_ptr<interfaces::connection_observer> observer_copy;
105 std::lock_guard<std::mutex> lock(callbacks_mutex_);
106 observer_copy = observer_;
107 callback_copy = disconnected_callback_;
112 observer_copy->on_disconnected();
129 "Client not initialized",
130 "udp_client_adapter::send"
134 if (!is_connected_.load(std::memory_order_acquire))
138 "Client is not connected - call start() first",
139 "udp_client_adapter::send"
143 return client_->send(std::move(data));
153 std::lock_guard<std::mutex> lock(callbacks_mutex_);
154 observer_ = std::move(observer);
159 std::lock_guard<std::mutex> lock(callbacks_mutex_);
160 receive_callback_ = std::move(callback);
165 std::lock_guard<std::mutex> lock(callbacks_mutex_);
166 connected_callback_ = std::move(callback);
171 std::lock_guard<std::mutex> lock(callbacks_mutex_);
172 disconnected_callback_ = std::move(callback);
177 std::lock_guard<std::mutex> lock(callbacks_mutex_);
178 error_callback_ = std::move(callback);
194 client_->set_receive_callback(
195 [
this](
const std::vector<uint8_t>& data,
198 std::shared_ptr<interfaces::connection_observer> observer_copy;
201 std::lock_guard<std::mutex> lock(callbacks_mutex_);
202 observer_copy = observer_;
203 callback_copy = receive_callback_;
208 observer_copy->on_receive(data);
217 client_->set_error_callback(
218 [
this](std::error_code ec)
220 std::shared_ptr<interfaces::connection_observer> observer_copy;
223 std::lock_guard<std::mutex> lock(callbacks_mutex_);
224 observer_copy = observer_;
225 callback_copy = error_callback_;
230 observer_copy->on_error(ec);
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.
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.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the connected server.
~udp_client_adapter() override
Destructor ensures proper cleanup.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
std::atomic< bool > is_connected_
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.
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.
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_
constexpr int invalid_argument
constexpr int internal_error
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Endpoint information for UDP datagrams.