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
use std::{
    io::{self, Write},
    net::{SocketAddr, TcpListener, TcpStream},
};

use microasync::sync;
use microasync_rt::{
    get_current_runtime,
    io::{read::tcpstream::accept, ReadAsync},
    wait_ms, QueuedRuntime, Runtime,
};

fn main() {
    let mut runtime = QueuedRuntime::new();
    runtime.push(go(("0.0.0.0", 5000)));
    sync(runtime);
}

async fn go(addr: (&str, u16)) {
    let mut listener = TcpListener::bind(addr).unwrap();
    loop {
        get_current_runtime()
            .await
            .push(handle(accept(&mut listener).await.unwrap()));
    }
}

async fn handle((mut stream, _): (TcpStream, SocketAddr)) {
    let mut buf = [0_u8; 10];
    loop {
        let n = stream.read(&mut buf).await.unwrap();
        if n == 0 {
            break;
        }
        print!(
            "{}",
            buf[0..n].iter().map(|x| *x as char).collect::<String>()
        );
        io::stdout().flush().unwrap();
        wait_ms(100).await; // Delay so multiple connections can accumulate, for demonstration
                            // purposes.
    }
}