]> git.proxmox.com Git - proxmox-backup.git/blame - examples/h2server.rs
use new proxmox-async crate
[proxmox-backup.git] / examples / h2server.rs
CommitLineData
19f5aa25 1use anyhow::Error;
fded1f31 2use futures::*;
19f5aa25 3use hyper::{Body, Request, Response};
fded1f31 4
19f5aa25 5use tokio::net::{TcpListener, TcpStream};
fded1f31 6
d973aa82 7fn main() -> Result<(), Error> {
9a1b24b6 8 proxmox_async::runtime::main(run())
d973aa82
WB
9}
10
11async fn run() -> Result<(), Error> {
19f5aa25 12 let listener = TcpListener::bind(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
fded1f31
DM
13
14 println!("listening on {:?}", listener.local_addr());
15
db0cb9ce
WB
16 loop {
17 let (socket, _addr) = listener.accept().await?;
19f5aa25
FG
18 tokio::spawn(handle_connection(socket).map(|res| {
19 if let Err(err) = res {
20 eprintln!("Error: {}", err);
21 }
22 }));
74be6dc9 23 }
74be6dc9 24}
fded1f31 25
19f5aa25
FG
26async fn handle_connection(socket: TcpStream) -> Result<(), Error> {
27 socket.set_nodelay(true).unwrap();
fded1f31 28
19f5aa25
FG
29 let mut http = hyper::server::conn::Http::new();
30 http.http2_only(true);
31 // increase window size: todo - find optiomal size
32 let max_window_size = (1 << 31) - 2;
33 http.http2_initial_stream_window_size(max_window_size);
34 http.http2_initial_connection_window_size(max_window_size);
fded1f31 35
19f5aa25
FG
36 let service = hyper::service::service_fn(|_req: Request<Body>| {
37 println!("Got request");
38 let buffer = vec![65u8; 4 * 1024 * 1024]; // nonsense [A,A,A,A...]
39 let body = Body::from(buffer);
fded1f31 40
19f5aa25 41 let response = Response::builder()
74be6dc9 42 .status(http::StatusCode::OK)
19f5aa25
FG
43 .header(http::header::CONTENT_TYPE, "application/octet-stream")
44 .body(body)
74be6dc9 45 .unwrap();
19f5aa25
FG
46 future::ok::<_, Error>(response)
47 });
fded1f31 48
19f5aa25
FG
49 http.serve_connection(socket, service)
50 .map_err(Error::from)
51 .await?;
fded1f31 52
19f5aa25 53 println!("H2 connection CLOSE !");
fded1f31
DM
54 Ok(())
55}