diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 0b70f9c..eeb791d 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -40,53 +40,27 @@ pub async fn p2_connect_to_p5 ( server_id: u8, ) -> anyhow::Result <(SendStream, RecvStream)> { - let (mut relay_send, mut relay_recv) = connection.open_bi ().await?; + let (mut send, mut recv) = connection.open_bi ().await?; // Ask P3 if we can connect to P4 - let req_buf = [ - Command::CONNECT_P2_TO_P4.0, - server_id, - 0, - 0, - ]; - relay_send.write_all (&req_buf).await?; + let cmd_type = Command::CONNECT_P2_TO_P4.0; - let mut resp_buf = [0; 4]; - relay_recv.read_exact (&mut resp_buf).await?; - let expected = [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P4.0, - 0, - 0, - ]; - if resp_buf != expected { - bail! ("P2 didn't get OK response when asking P3 to connect P2 to P4"); - } + send.write_all (&[cmd_type, server_id, 0, 0]).await?; + + expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await + .context ("P2 didn't get OK response when asking P3 to connect P2 to P4")?; // Ask P4 if we can connect to P5 - let req_buf = [ - Command::CONNECT_P2_TO_P5.0, - 0, - 0, - 0, - ]; - relay_send.write_all (&req_buf).await?; + let cmd_type = Command::CONNECT_P2_TO_P5.0; - let mut resp_buf = [0; 4]; - relay_recv.read_exact (&mut resp_buf).await?; - let expected = [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P5.0, - 0, - 0, - ]; - if resp_buf != expected { - bail! ("P2 didn't get OK response when asking P4 to connect P2 to P5"); - } + send.write_all (&[cmd_type, 0, 0, 0]).await?; - Ok ((relay_send, relay_recv)) + expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await + .context ("P2 didn't get OK response when asking P4 to connect P2 to P5")?; + + Ok ((send, recv)) } pub async fn p4_connect_to_p3 (