diff --git a/prototypes/quic_demo/src/prelude.rs b/prototypes/quic_demo/src/prelude.rs index 0efa892..0315eda 100644 --- a/prototypes/quic_demo/src/prelude.rs +++ b/prototypes/quic_demo/src/prelude.rs @@ -10,7 +10,10 @@ pub use std::{ time::Duration, }; -pub use anyhow::bail; +pub use anyhow::{ + Context, + bail, +}; pub use futures_util::StreamExt; pub use tokio::{ io::{ diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 01dd5be..0b70f9c 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -24,28 +24,13 @@ pub async fn p2_connect_to_p3 ( ) -> anyhow::Result { let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; - let (mut send, mut recv) = new_conn.connection.open_bi ().await?; + let cmd_type = Command::CONNECT_P2_TO_P3.0; - let req_buf = [ - Command::CONNECT_P2_TO_P3.0, - client_id, - 0, - 0, - ]; - send.write_all (&req_buf).await?; + send.write_all (&[cmd_type, client_id, 0, 0]).await?; - let mut resp_buf = [0, 0, 0, 0]; - recv.read_exact (&mut resp_buf).await?; - let expected = [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P3.0, - 0, - 0, - ]; - if resp_buf != expected { - bail! ("P2 didn't get OK response when connecting to P3"); - } + expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await + .context ("P2 didn't get OK response when connecting to P3")?; Ok (new_conn) } @@ -111,28 +96,26 @@ pub async fn p4_connect_to_p3 ( ) -> anyhow::Result { let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; - let (mut send, mut recv) = new_conn.connection.open_bi ().await?; + let cmd_type = Command::CONNECT_P4_TO_P3.0; - let req_buf = [ - Command::CONNECT_P4_TO_P3.0, - server_id, - 0, - 0, - ]; - send.write_all (&req_buf).await?; + send.write_all (&[cmd_type, server_id, 0, 0]).await?; - let mut resp_buf = [0, 0, 0, 0]; - recv.read_exact (&mut resp_buf).await?; - let expected = [ - Command::OKAY.0, - Command::CONNECT_P4_TO_P3.0, - 0, - 0, - ]; - if resp_buf != expected { - bail! ("P4 didn't get OK response when connecting to P3"); - } + expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await + .context ("P4 didn't get OK response when connecting to P3")?; Ok (new_conn) } + +async fn expect_exact_response ( + recv: &mut RecvStream, expected: [u8; 4] +) -> anyhow::Result <()> +{ + let mut resp_buf = [0, 0, 0, 0]; + recv.read_exact (&mut resp_buf).await?; + if resp_buf != expected { + bail! ("Didn't receive exact response"); + } + + Ok (()) +}