-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.rs
56 lines (52 loc) · 1.88 KB
/
echo.rs
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
56
//! A minimum echo server.
//!
//! Run `cargo run --example echo -- --help` to get usage message.
mod echo {
// All this module are runtime agnostic.
use async_net::*;
use futures_lite::io;
pub async fn echo_server(port: u16) {
let listener = TcpListener::bind(("127.0.0.1", port)).await.unwrap();
println!("Listen on port: {}", listener.local_addr().unwrap().port());
let mut echos = vec![];
let mut id_counter = 0;
loop {
let (stream, remote_addr) = listener.accept().await.unwrap();
id_counter += 1;
let id = id_counter;
let handle = spawns::spawn(async move {
eprintln!("{:010}[{}]: serving", id, remote_addr);
let (reader, writer) = io::split(stream);
match io::copy(reader, writer).await {
Ok(_) => eprintln!("{:010}[{}]: closed", id, remote_addr),
Err(err) => eprintln!("{:010}[{}]: {:?}", id, remote_addr, err),
}
})
.attach();
echos.push(handle);
}
}
}
fn main() {
use clap::*;
let cmd = Command::new("echo")
.arg(
Arg::new("port")
.long("port")
.help("Port to listen. Defaults to 0 to choose an random one")
.value_parser(value_parser!(u16)),
)
.arg(
Arg::new("parallelism")
.long("parallelism")
.help("Concurrent parallelism. Defaults to 0 to utilize all cores")
.value_parser(value_parser!(usize)),
);
let matches = cmd.get_matches();
let port = matches.get_one::<u16>("port").copied().unwrap_or(0);
let parallelism = matches
.get_one::<usize>("parallelism")
.copied()
.unwrap_or(0);
spawns::Blocking::new(parallelism).block_on(echo::echo_server(port));
}