30#include <condition_variable>
33#define NETWORK_USE_EXPERIMENTAL
54 std::cout <<
"=== QUIC Client Example ===" << std::endl;
58 client_ = std::make_shared<core::messaging_quic_client>(
"quic_demo_client");
67 std::cerr <<
"Failed to start client: " << result.error().message << std::endl;
71 std::cout <<
"Client started, waiting for connection..." << std::endl;
75 std::unique_lock<std::mutex> lock(
mutex_);
76 bool connected =
cv_.wait_for(lock, std::chrono::seconds(10), [
this]() {
82 std::cerr <<
"Connection timeout" << std::endl;
88 std::cout <<
"Connected to server!" << std::endl;
97 std::this_thread::sleep_for(std::chrono::seconds(2));
101 std::cout <<
"\n=== Connection Statistics ===" << std::endl;
102 std::cout <<
"Bytes sent: " << stats.bytes_sent << std::endl;
103 std::cout <<
"Bytes received: " << stats.bytes_received << std::endl;
104 std::cout <<
"Packets sent: " << stats.packets_sent << std::endl;
105 std::cout <<
"Packets received: " << stats.packets_received << std::endl;
106 std::cout <<
"Packets lost: " << stats.packets_lost << std::endl;
109 std::cout <<
"\nStopping client..." << std::endl;
113 std::cout <<
"Client stopped." << std::endl;
122 std::cout <<
"[Callback] Connected to server" << std::endl;
129 std::cout <<
"[Callback] Disconnected from server" << std::endl;
135 std::cerr <<
"[Callback] Error: " << ec.message() << std::endl;
140 std::string msg(data.begin(), data.end());
141 std::cout <<
"[Callback] Received on default stream: " << msg << std::endl;
147 [](uint64_t stream_id,
const std::vector<uint8_t>& data,
bool fin) {
148 std::string msg(data.begin(), data.end());
149 std::cout <<
"[Callback] Stream " << stream_id <<
": "
150 << data.size() <<
" bytes"
151 << (fin ?
" (FIN)" :
"") << std::endl;
152 std::cout <<
" Data: " << msg << std::endl;
158 std::cout <<
"\n=== Sending on Default Stream ===" << std::endl;
164 std::cout <<
"Sent string message on default stream" << std::endl;
168 std::cerr <<
"Failed to send: " << result.error().message << std::endl;
172 std::vector<uint8_t> binary_data = {0x01, 0x02, 0x03, 0x04, 0x05};
176 std::cout <<
"Sent binary data on default stream" << std::endl;
180 std::cerr <<
"Failed to send: " << result.error().message << std::endl;
186 std::cout <<
"\n=== Multi-Stream Demo ===" << std::endl;
190 if (stream_result.is_err())
192 std::cerr <<
"Failed to create stream: " << stream_result.error().message << std::endl;
196 uint64_t stream_id = stream_result.value();
197 std::cout <<
"Created bidirectional stream: " << stream_id << std::endl;
200 std::string msg =
"Hello from stream " + std::to_string(stream_id);
201 std::vector<uint8_t> data(msg.begin(), msg.end());
204 if (send_result.is_ok())
206 std::cout <<
"Sent message on stream " << stream_id << std::endl;
210 std::cerr <<
"Failed to send: " << send_result.error().message << std::endl;
215 if (uni_stream_result.is_ok())
217 uint64_t uni_stream_id = uni_stream_result.value();
218 std::cout <<
"Created unidirectional stream: " << uni_stream_id << std::endl;
220 std::string uni_msg =
"Unidirectional message";
221 std::vector<uint8_t> uni_data(uni_msg.begin(), uni_msg.end());
227 if (uni_send_result.is_ok())
229 std::cout <<
"Sent message on unidirectional stream with FIN" << std::endl;
235 if (close_result.is_ok())
237 std::cout <<
"Closed stream " << stream_id << std::endl;
243 std::shared_ptr<core::messaging_quic_client>
client_;
247 std::condition_variable
cv_;
255 std::cout <<
"\n=== QUIC Client with Custom Config ===" << std::endl;
258 auto client = std::make_shared<core::messaging_quic_client>(
"config_demo_client");
262 config.verify_server =
false;
263 config.alpn_protocols = {
"h3"};
264 config.max_idle_timeout_ms = 60000;
265 config.enable_early_data =
true;
272 auto result = client->start_client(
"example.com", 443,
config);
275 std::cout <<
"Expected failure (no server): " << result.error().message << std::endl;
278 (void)client->stop_client();
281int main(
int argc,
char* argv[])
283 std::string host =
"127.0.0.1";
284 unsigned short port = 4433;
292 port =
static_cast<unsigned short>(std::stoi(argv[2]));
300 std::cout <<
"\nNote: This demo requires a running QUIC server." << std::endl;
301 std::cout <<
"Without a server, connection will timeout." << std::endl;
302 std::cout << std::endl;
304 bool success = demo.
run();
309 return success ? 0 : 1;
std::shared_ptr< core::messaging_quic_client > client_
void demo_send_default_stream()
unsigned short server_port_
QuicClientDemo(const std::string &server_host, unsigned short server_port)
std::condition_variable cv_
std::atomic< bool > message_received_
std::atomic< bool > connected_
auto set_disconnected_callback(interfaces::i_quic_client::disconnected_callback_t callback) -> void override
Sets the callback for disconnection (interface version).
auto wait_for_stop() -> void override
Blocks until stop() is called.
auto create_unidirectional_stream() -> Result< uint64_t > override
Creates a new unidirectional stream (interface version).
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client with default configuration.
auto close_stream(uint64_t stream_id) -> VoidResult override
Closes a stream (interface version).
auto send_on_stream(uint64_t stream_id, std::vector< uint8_t > &&data, bool fin=false) -> VoidResult override
Sends data on a specific stream (interface version).
auto set_receive_callback(interfaces::i_quic_client::receive_callback_t callback) -> void override
Sets the callback for received data on default stream (interface version).
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Send data on the default stream (stream 0).
auto stop_client() -> VoidResult
Stops the client and releases all resources.
auto create_stream() -> Result< uint64_t > override
Creates a new bidirectional stream (interface version).
auto set_connected_callback(interfaces::i_quic_client::connected_callback_t callback) -> void override
Sets the callback for connection established (interface version).
auto set_stream_receive_callback(stream_receive_callback_t callback) -> void
Sets the callback for stream data reception (all streams, legacy version).
auto stats() const -> quic_connection_stats
Get connection statistics.
auto set_error_callback(interfaces::i_quic_client::error_callback_t callback) -> void override
Sets the callback for errors (interface version).
Main namespace for all Network System components.
void demo_with_config()
Example with custom configuration.
Configuration options for QUIC client.