diff --git a/prototypes/quic_demo/src/bin/quic_demo_client.rs b/prototypes/quic_demo/src/bin/quic_demo_client.rs index 0237d60..2d63a07 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_client.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_client.rs @@ -39,22 +39,7 @@ async fn main () -> anyhow::Result <()> { let client_id = opt.client_id.unwrap_or (42); let server_id = opt.server_id.unwrap_or (43); - let req_buf = [ - Command::CONNECT_P2_TO_P3.0, - client_id, - 0, - 0, - ]; - send.write_all (&req_buf).await?; - - let mut resp_buf = [0, 0, 0, 0]; - recv.read_exact (&mut resp_buf).await?; - assert_eq! (resp_buf, [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P3.0, - 0, - 0, - ]); + protocol::p2_connect_to_p3 (&mut send, &mut recv, client_id).await?; let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?; diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 779510f..a22ef86 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -1,3 +1,10 @@ +use quinn::{ + SendStream, + RecvStream, +}; + +use crate::prelude::*; + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Command (pub u8); @@ -10,4 +17,32 @@ impl Command { pub const OKAY: Command = Command (20); } -// pub fn p2_connect_to_p3 ( +pub async fn p2_connect_to_p3 ( + send: &mut SendStream, + recv: &mut RecvStream, + client_id: u8, +) +-> anyhow::Result <()> +{ + let req_buf = [ + Command::CONNECT_P2_TO_P3.0, + client_id, + 0, + 0, + ]; + send.write_all (&req_buf).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"); + } + + Ok (()) +}