Skip to main content

sharded_echo/
sharded_echo.rs

1//! Echo server using a fixed Linux receive-socket group.
2#[cfg(target_os = "linux")]
3#[tokio::main]
4async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
5    use rust_raknet::{RaknetListener, Reliability};
6    let address = std::env::args().nth(1).ok_or("ADDRESS required")?.parse()?;
7    let count = std::env::args()
8        .nth(2)
9        .unwrap_or_else(|| "4".into())
10        .parse()?;
11    let mut listener = RaknetListener::bind_with_socket_shards(&address, count).await?;
12    listener.listen().await;
13    println!("Sharded echo listening on {}", listener.local_addr()?);
14    loop {
15        let client = listener.accept().await?;
16        tokio::spawn(async move {
17            while let Ok(payload) = client.recv().await {
18                if client
19                    .send(&payload, Reliability::ReliableOrdered)
20                    .await
21                    .is_err()
22                {
23                    break;
24                }
25            }
26        });
27    }
28}
29
30#[cfg(not(target_os = "linux"))]
31fn main() {
32    eprintln!("Socket sharding requires Linux");
33}