1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#[cfg(test)]
mod tests {
use voidio::*;
use voidio::net::{DispatchMode, QuicConnectionEvent, QuicServer};
fn handle_connections(event: QuicConnectionEvent) {
let client = event.connection;
println!("New connection from: {}", client.address());
client.on_stream(|stream| {
println!("New stream: {}", stream);
stream.on_data(|data| {
println!("Received data on stream: {:?}", data);
});
stream.on_close(|| {
println!("Stream closed");
});
});
client.on_message(|msg| {
println!("Received message: {}", msg);
});
client.on_datagram(|datagram| {
println!("Received datagram: {}", datagram);
});
client.on_close(|conn| {
println!("Connection closed: {}", conn.address());
});
}
#[test]
fn test_quic_server() {
std::thread::sleep(std::time::Duration::from_millis(1));
let address = "0.0.0.0:4433".parse().unwrap(); // 42070
{
println!("Starting the QUIC server on {}", address);
let mut server = QuicServer::new(address);
// Direct dispatch mode toggle (< 30ns overhead, useful for packets that purely modify memory or tick state such as gaming servers), no async processing tax.
server.set_datagram_dispatch_mode(DispatchMode::Direct);
// This is always directly dispatched by one of the worker threads initiated in the .start(N) method upon decrypting the Crypto frame of the Initial packet.
server.on_connection(handle_connections);
server.start(32); // Start the server with 32 worker threads.
}
std::thread::sleep(std::time::Duration::from_secs(30));
println!("Stopping the QUIC server...");
/*
let mut client = QuicClient::new();
client.set_address(address);
client.on_open(move |conn| {
println!("Client connected to: {}", address);
let stream = conn.open_bistream().unwrap();
stream.write(b"Hello from client!").unwrap();
});
client.connect().unwrap(); // will be changed to async in the future
*/
// wait for 30 seconds before stopping the sketch test server
}
}