From 4820722ec26d5e02e7150485dd14226f5de0bb5f Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 18 Jul 2021 18:33:19 +0000 Subject: [PATCH] :recycle: refactor --- .../quic_demo/src/bin/quic_demo_end_server.rs | 25 ++------------ prototypes/quic_demo/src/protocol.rs | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/prototypes/quic_demo/src/bin/quic_demo_end_server.rs b/prototypes/quic_demo/src/bin/quic_demo_end_server.rs index ca74902..da535d0 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_end_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_end_server.rs @@ -27,31 +27,12 @@ async fn main () -> anyhow::Result <()> { trace! ("Connecting to relay server"); + let server_id = opt.server_id.unwrap_or (43); + let quinn::NewConnection { - connection, mut bi_streams, .. - } = endpoint.connect (&relay_addr, "localhost")?.await?; - - let (mut send, mut recv) = connection.open_bi ().await?; - - let our_id = opt.server_id.unwrap_or (43); - let req_buf = [ - Command::CONNECT_P4_TO_P3.0, - our_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_P4_TO_P3.0, - 0, - 0, - ]); + } = protocol::p4_connect_to_p3 (&endpoint, &relay_addr, server_id).await?; trace! ("Accepting bi streams from P3"); diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 32dc2ae..01dd5be 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -103,3 +103,36 @@ pub async fn p2_connect_to_p5 ( Ok ((relay_send, relay_recv)) } + +pub async fn p4_connect_to_p3 ( + endpoint: &quinn::Endpoint, + relay_addr: &std::net::SocketAddr, + server_id: u8, +) -> anyhow::Result +{ + let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; + + let (mut send, mut recv) = new_conn.connection.open_bi ().await?; + + let req_buf = [ + Command::CONNECT_P4_TO_P3.0, + server_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_P4_TO_P3.0, + 0, + 0, + ]; + if resp_buf != expected { + bail! ("P4 didn't get OK response when connecting to P3"); + } + + Ok (new_conn) +}