-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecho.rs
60 lines (54 loc) · 1.56 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
57
58
59
60
use std::io;
use std::net;
use std::time::Duration;
use futures::prelude::*;
use tokio_uring_reactor::io::{
SocketRead,
SocketWrite,
};
use tokio_timer::Timeout;
pub fn main() {
env_logger::init();
println!("Starting echo server");
let l = net::TcpListener::bind("[::]:22").expect("bind");
let l = tokio_uring_reactor::net::TcpListener::from(l);
let mut runtime = tokio_uring::Runtime::new().expect("new runtime");
let handle = runtime.reactor_handle();
let connection_handler = move |(c, a): (tokio_uring_reactor::net::TcpStream, net::SocketAddr)| {
println!("Connection from {}", a);
let mut buf: Vec<u8> = Vec::new();
let whandle = handle.clone();
buf.resize_with(512, Default::default);
tokio_current_thread::spawn(
Timeout::new(c.read(&handle, buf).from_err(), Duration::from_secs(3))
.map_err(|e| {
eprintln!("timout/read error");
if e.is_inner() {
e.into_inner().expect("inner")
} else {
io::Error::new(io::ErrorKind::TimedOut, "timeout")
}
})
.and_then(move |(n, mut buf, c)| {
buf.truncate(n);
println!("Echoing: {:?}", buf);
c.write(&whandle, buf).from_err()
})
.map(|(_,_,_)| println!("connection done"))
.map_err(|e| eprintln!("Connection error: {}", e))
);
Ok(())
};
let handle = runtime.reactor_handle();
runtime.spawn(
Timeout::new(l.incoming(&handle), Duration::from_secs(30))
.map_err(|e| {
if e.is_inner() {
panic!(e.into_inner().expect("inner"));
}
})
.for_each(connection_handler)
.map(|()| eprintln!("listening done"))
);
runtime.run().expect("runtime run");
}