2020-04-17 15:11:25 +03:00
use anyhow ::{ Error } ;
2019-06-29 11:08:12 +03:00
use futures ::* ;
// Simple H2 server to test H2 speed with h2client.rs
use tokio ::net ::TcpListener ;
2019-08-28 16:50:40 +03:00
use tokio ::io ::{ AsyncRead , AsyncWrite } ;
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
use proxmox_backup ::client ::pipe_to_stream ::PipeToSendStream ;
2019-06-29 11:08:12 +03:00
2020-01-20 14:52:22 +03:00
fn main ( ) -> Result < ( ) , Error > {
proxmox_backup ::tools ::runtime ::main ( run ( ) )
}
async fn run ( ) -> Result < ( ) , Error > {
2019-12-12 17:27:07 +03:00
let mut listener = TcpListener ::bind ( std ::net ::SocketAddr ::from ( ( [ 127 , 0 , 0 , 1 ] , 8008 ) ) ) . await ? ;
2019-06-29 11:08:12 +03:00
println! ( " listening on {:?} " , listener . local_addr ( ) ) ;
2019-12-12 17:27:07 +03:00
loop {
let ( socket , _addr ) = listener . accept ( ) . await ? ;
2019-08-28 16:50:40 +03:00
tokio ::spawn ( handle_connection ( socket )
. map ( | res | {
if let Err ( err ) = res {
eprintln! ( " Error: {} " , err ) ;
}
} ) ) ;
}
}
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
async fn handle_connection < T : AsyncRead + AsyncWrite + Unpin > ( socket : T ) -> Result < ( ) , Error > {
let mut conn = h2 ::server ::handshake ( socket ) . await ? ;
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
println! ( " H2 connection bound " ) ;
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
while let Some ( ( request , mut respond ) ) = conn . try_next ( ) . await ? {
println! ( " GOT request: {:?} " , request ) ;
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
let response = http ::Response ::builder ( )
. status ( http ::StatusCode ::OK )
. body ( ( ) )
. unwrap ( ) ;
2019-06-29 11:08:12 +03:00
2019-08-28 16:50:40 +03:00
let send = respond . send_response ( response , false ) . unwrap ( ) ;
let data = vec! [ 65 u8 ; 1024 * 1024 ] ;
PipeToSendStream ::new ( bytes ::Bytes ::from ( data ) , send ) . await ? ;
println! ( " DATA SENT " ) ;
}
2019-06-29 11:08:12 +03:00
Ok ( ( ) )
}