Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
reliable_udp_client.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7// Experimental API marker - users must opt-in to use this header
10
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <system_error>
17#include <vector>
18
20
22{
34
40 {
41 uint64_t packets_sent{0};
42 uint64_t packets_received{0};
44 uint64_t packets_dropped{0};
45 uint64_t acks_sent{0};
46 uint64_t acks_received{0};
47 double average_rtt_ms{0.0};
48 };
49
101 class reliable_udp_client : public std::enable_shared_from_this<reliable_udp_client>
102 {
103 public:
110 std::string_view client_id,
112
117
127 auto start_client(std::string_view host, uint16_t port) -> VoidResult;
128
135 auto stop_client() -> VoidResult;
136
148 auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
149
153 auto wait_for_stop() -> void;
154
163 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
164
169 auto set_error_callback(std::function<void(std::error_code)> callback) -> void;
170
178 auto set_congestion_window(size_t packets) -> void;
179
187 auto set_max_retries(size_t retries) -> void;
188
195 auto set_retransmission_timeout(uint32_t timeout_ms) -> void;
196
201 auto get_stats() const -> reliable_udp_stats;
202
207 auto is_running() const -> bool;
208
213 auto client_id() const -> const std::string&;
214
219 auto mode() const -> reliability_mode;
220
221 private:
222 class impl;
223 std::unique_ptr<impl> pimpl_;
224 };
225
226} // namespace kcenon::network::core
auto get_stats() const -> reliable_udp_stats
Returns current connection statistics.
auto start_client(std::string_view host, uint16_t port) -> VoidResult
Starts the client and connects to the target endpoint.
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.
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.
Macros and utilities for marking experimental APIs.
#define NETWORK_REQUIRE_EXPERIMENTAL
Enforces opt-in for experimental APIs at compile time.
reliability_mode
Defines the reliability level for UDP packet transmission.
Network-specific error and result type definitions.
Statistics for monitoring reliable UDP connection performance.