diff --git a/Cargo.lock b/Cargo.lock index 875b98f..ccf28ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1105,6 +1105,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", + "web-sys", ] [[package]] @@ -1837,9 +1838,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.118" +version = "2.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" dependencies = [ "proc-macro2", "quote", @@ -2185,6 +2186,16 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c31d56021e873866c968588ed85ccdf56db5c426e44afdb4618c39895104b920" +[[package]] +name = "web-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "web-time" version = "1.1.0" diff --git a/example/server/src/main.rs b/example/server/src/main.rs index 4ccd627..0cb82db 100644 --- a/example/server/src/main.rs +++ b/example/server/src/main.rs @@ -8,6 +8,7 @@ use mtp::type_map::TypeMap; use std::future::Future; use std::path::Path; use std::pin::Pin; +use std::sync::Arc; fn dev_cert_paths() -> (String, String) { let cert = std::env::var("MTP_DEV_CERT").unwrap_or_else(|_| { @@ -41,13 +42,6 @@ async fn handle_pipe_loopback( let mut reader = req.accept().await?; println!(" [loopback] Pipe {pipe_id} accepted, reading data ..."); - let mut buf = Vec::new(); - tokio::io::AsyncReadExt::read_to_end(&mut reader, &mut buf).await?; - println!( - " [loopback] Pipe {pipe_id} read {} bytes, creating return pipe ...", - buf.len() - ); - let handle = conn.create_pipe("loopback").await?; println!( " [loopback] Return pipe created (id={}), waiting for client ...", @@ -56,16 +50,19 @@ async fn handle_pipe_loopback( match handle.wait().await? { Some(mut writer) => { - println!( - " [loopback] Client accepted return pipe, writing {} bytes ...", - buf.len() - ); - tokio::io::AsyncWriteExt::write_all(&mut writer, &buf).await?; + println!(" [loopback] Client accepted return pipe, echoing incoming bytes ..."); + let mut total = 0usize; + let mut buf = [0u8; 16 * 1024]; + loop { + let n = tokio::io::AsyncReadExt::read(&mut reader, &mut buf).await?; + if n == 0 { + break; + } + total += n; + tokio::io::AsyncWriteExt::write_all(&mut writer, &buf[..n]).await?; + } writer.finish().await?; - println!( - " [loopback] Pipe {pipe_id} loopback complete ({} bytes)", - buf.len() - ); + println!(" [loopback] Pipe {pipe_id} loopback complete ({} bytes)", total); } None => { eprintln!(" [loopback] Return pipe denied by client for pipe {pipe_id}"); @@ -86,8 +83,10 @@ async fn main() -> Result<(), Box> { let (_host_id, host_keyring) = keys::load_or_generate_host_keys("host.mk")?; keys::export_host_public_keys(&host_keyring)?; - let decrypt_keyring = mtp::crypto::Keyring::from_bytes(&host_keyring.to_bytes()) - .expect("re-load host keyring for decryption"); + let decrypt_keyring = Arc::new( + mtp::crypto::Keyring::from_bytes(&host_keyring.to_bytes()) + .expect("re-load host keyring for decryption"), + ); let (clients, next_id) = clients::load_client_db("clients.json")?; @@ -152,61 +151,64 @@ async fn main() -> Result<(), Box> { println!("Server listening on {}", host.local_addr()); while let Some(conn) = host.accept().await? { - let desc = conn.description.as_deref().unwrap_or("(no description)"); - println!( - "\n--- New connection (version {}, description: {desc}) ---", - conn.version - ); - println!("Client ID: {}", conn.client_id); + let decrypt_keyring = Arc::clone(&decrypt_keyring); + tokio::spawn(async move { + let desc = conn.description.as_deref().unwrap_or("(no description)"); + println!( + "\n--- New connection (version {}, description: {desc}) ---", + conn.version + ); + println!("Client ID: {}", conn.client_id); - let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap(); + let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap(); - println!("Waiting for messages / pipe requests ..."); - loop { - tokio::select! { - biased; + println!("Waiting for messages / pipe requests ..."); + loop { + tokio::select! { + biased; - pipe_req = conn.receive_pipe() => { - match pipe_req { - Ok(req) => { - println!(" Pipe request: id={} desc={:?}", req.id(), req.description()); - if let Err(e) = handle_pipe_loopback(&conn, req).await { - eprintln!(" Pipe loopback error: {e}"); + pipe_req = conn.receive_pipe() => { + match pipe_req { + Ok(req) => { + println!(" Pipe request: id={} desc={:?}", req.id(), req.description()); + if let Err(e) = handle_pipe_loopback(&conn, req).await { + eprintln!(" Pipe loopback error: {e}"); + } } - } - Err(e) => { - println!("Pipe channel closed: {e}"); - break; - } - } - } - msg = conn.receive() => { - match msg { - Ok(msg) => { - println!("Received: {msg}"); - let response = handlers::process_and_respond( - &msg, - tm, - conn.client_public_key.as_ref(), - &decrypt_keyring, - ); - println!("Sending: {response}"); - if let Err(e) = conn.sender.send(&response).await { - eprintln!("Send error: {e}"); + Err(e) => { + println!("Pipe channel closed: {e}"); break; } } - Err(e) => { - println!("Connection ended: {e}"); - break; + } + msg = conn.receive() => { + match msg { + Ok(msg) => { + println!("Received: {msg}"); + let response = handlers::process_and_respond( + &msg, + tm, + conn.client_public_key.as_ref(), + &decrypt_keyring, + ); + println!("Sending: {response}"); + if let Err(e) = conn.sender.send(&response).await { + eprintln!("Send error: {e}"); + break; + } + } + Err(e) => { + println!("Connection ended: {e}"); + break; + } } } } } - } - conn.sender.close(); - println!("Connection closed\n"); + conn.sender.close(); + println!("Connection closed\n"); + }); } Ok(()) diff --git a/example/web-client/index.html b/example/web-client/index.html index 941b59e..f2fadda 100644 --- a/example/web-client/index.html +++ b/example/web-client/index.html @@ -51,7 +51,7 @@

MTP WebTransport Client

- +