5#define NETWORK_USE_EXPERIMENTAL
69 "] Created with mode=" + std::to_string(
static_cast<int>(
mode)));
87 "Client is already running");
96 const asio::ip::udp::endpoint&) {
105 auto result =
udp_client_->start_client(host, port);
165 "Client is not running");
230 return std::shared_ptr<impl>(
this, [](
impl*) {
241 header.ack_number = 0;
243 header.data_length =
static_cast<uint16_t
>(data.size());
248 std::move(packet), [
this](std::error_code ec, std::size_t) {
266 "Congestion window full");
273 header.ack_number = 0;
275 header.data_length =
static_cast<uint16_t
>(data.size());
282 info.send_time = std::chrono::steady_clock::now();
283 info.retransmit_count = 0;
284 info.sequence_number = seq;
289 std::move(packet), [
this, seq](std::error_code ec, std::size_t) {
311 header.ack_number = 0;
313 header.data_length =
static_cast<uint16_t
>(data.size());
318 std::move(packet), [
this](std::error_code ec, std::size_t) {
329 -> std::vector<uint8_t>
331 std::vector<uint8_t> packet(
sizeof(
packet_header) + payload.size());
333 if (!payload.empty())
335 std::memcpy(packet.data() +
sizeof(
packet_header), payload.data(),
347 "] Received invalid packet (too small)");
372 std::vector<uint8_t> payload(data.begin() +
sizeof(
packet_header), data.end());
410 auto now = std::chrono::steady_clock::now();
411 auto rtt = std::chrono::duration_cast<std::chrono::milliseconds>(
412 now - it->second.send_time)
435 std::to_string(ack_number) +
", RTT=" + std::to_string(rtt) +
"ms");
444 header.ack_number = sequence_number;
446 header.data_length = 0;
450 udp_client_->send(std::move(packet), [
this](std::error_code ec, std::size_t) {
491 "] Buffered out-of-order packet seq=" +
492 std::to_string(sequence_number) +
499 "] Dropped old packet seq=" + std::to_string(sequence_number));
519 "] Dropped old packet in sequenced mode seq=" +
520 std::to_string(sequence_number));
552 "] Failed to get thread pool for retransmission timer");
560 std::this_thread::sleep_for(
579 "] Exception while waiting for retransmission timer to stop");
590 auto now = std::chrono::steady_clock::now();
594 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
595 now - it->second.send_time)
604 "] Packet seq=" + std::to_string(it->first) +
605 " exceeded max retries, dropping");
617 auto packet_copy = it->second.data;
618 it->second.send_time = now;
619 it->second.retransmit_count++;
627 "] Retransmitting seq=" + std::to_string(it->first) +
628 " (attempt " + std::to_string(it->second.retransmit_count) +
632 [](std::error_code, std::size_t) {});
684 : pimpl_(std::make_unique<
impl>(client_id, mode))
692 return pimpl_->start_client(host, port);
699 return pimpl_->send_packet(std::move(data));
705 std::function<
void(
const std::vector<uint8_t>&)> callback) ->
void
707 pimpl_->set_receive_callback(std::move(callback));
713 pimpl_->set_error_callback(std::move(callback));
718 pimpl_->set_congestion_window(packets);
723 pimpl_->set_max_retries(retries);
728 pimpl_->set_retransmission_timeout(timeout_ms);
std::shared_ptr< kcenon::network::integration::thread_pool_interface > get_thread_pool()
Get current thread pool.
static network_context & instance()
Get the singleton instance.
auto client_id() const -> const std::string &
auto create_packet(const packet_header &header, const std::vector< uint8_t > &payload) -> std::vector< uint8_t >
auto send_ack(uint32_t sequence_number) -> void
auto set_congestion_window(size_t packets) -> void
auto set_max_retries(size_t retries) -> void
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
auto send_unreliable(std::vector< uint8_t > &&data) -> VoidResult
std::shared_ptr< messaging_udp_client > udp_client_
reliable_udp_stats stats_
std::function< void(const std::vector< uint8_t > &)> receive_callback_
std::atomic< bool > stop_retransmission_
std::function< void(std::error_code)> error_callback_
auto stop_client() -> VoidResult
auto handle_ordered_delivery(uint32_t sequence_number, std::vector< uint8_t > &&payload) -> void
std::atomic< bool > is_running_
std::future< void > retransmission_future_
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
auto handle_ack(uint32_t ack_number) -> void
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
auto handle_error(std::error_code ec) -> void
auto start_client(std::string_view host, uint16_t port) -> VoidResult
std::map< uint32_t, std::vector< uint8_t > > receive_buffer_
auto send_reliable(std::vector< uint8_t > &&data) -> VoidResult
auto set_retransmission_timeout(uint32_t timeout_ms) -> void
std::mutex callback_mutex_
std::map< uint32_t, packet_info > pending_packets_
auto check_and_retransmit() -> void
std::shared_ptr< integration::thread_pool_interface > thread_pool_
impl(std::string_view client_id, reliability_mode mode)
auto wait_for_stop() -> void
std::mutex pending_mutex_
auto stop_retransmission_timer() -> void
auto handle_sequenced_delivery(uint32_t sequence_number, std::vector< uint8_t > &&payload) -> void
std::mutex receive_mutex_
auto get_stats() const -> reliable_udp_stats
auto send_sequenced(std::vector< uint8_t > &&data) -> VoidResult
size_t congestion_window_
auto shared_from_this() -> std::shared_ptr< impl >
auto handle_received_packet(const std::vector< uint8_t > &data) -> void
auto start_retransmission_timer() -> void
auto deliver_to_application(std::vector< uint8_t > &&payload) -> void
auto mode() const -> reliability_mode
std::atomic< uint32_t > expected_sequence_
uint32_t retransmission_timeout_ms_
std::atomic< uint32_t > next_sequence_
auto is_running() const -> bool
A UDP client with optional reliability layer for configurable delivery guarantees.
auto get_stats() const -> reliable_udp_stats
Returns current connection statistics.
reliable_udp_client(std::string_view client_id, reliability_mode mode=reliability_mode::reliable_ordered)
Constructs a reliable UDP client with specified mode.
auto set_max_retries(size_t retries) -> void
Sets maximum retransmission attempts before giving up.
auto wait_for_stop() -> void
Blocks until the client is stopped.
auto mode() const -> reliability_mode
Returns the current reliability mode.
~reliable_udp_client() noexcept
Destructor. Automatically stops the client if running.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends a packet with reliability handling based on mode.
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Sets callback for received data.
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets callback for connection errors.
auto stop_client() -> VoidResult
Stops the client and releases resources.
auto set_congestion_window(size_t packets) -> void
Sets the congestion window size (maximum unacknowledged packets).
auto client_id() const -> const std::string &
Returns the client identifier.
std::unique_ptr< impl > pimpl_
auto set_retransmission_timeout(uint32_t timeout_ms) -> void
Sets retransmission timeout in milliseconds.
auto is_running() const -> bool
Checks if client is currently running.
Logger system integration interface for network_system.
#define NETWORK_LOG_TRACE(msg)
#define NETWORK_LOG_WARN(msg)
#define NETWORK_LOG_INFO(msg)
#define NETWORK_LOG_ERROR(msg)
#define NETWORK_LOG_DEBUG(msg)
reliability_mode
Defines the reliability level for UDP packet transmission.
constexpr int internal_error
constexpr int already_exists
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Global context for shared network system resources.
std::vector< uint8_t > data
std::chrono::steady_clock::time_point send_time
Statistics for monitoring reliable UDP connection performance.
uint64_t packets_retransmitted
uint64_t packets_received