diff --git a/prototypes/quic_demo/src/bin/quic_demo_client.rs b/prototypes/quic_demo/src/bin/quic_demo_client.rs index 1060751..64e252d 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_client.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_client.rs @@ -25,62 +25,86 @@ async fn main () -> anyhow::Result <()> { let opt = Opt::from_args (); let conf = opt.into_config ().await?; - let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?; - - debug! ("Connecting to relay server"); - - let quinn::NewConnection { - connection, - .. - } = protocol::p2_connect_to_p3 (&endpoint, &conf.relay_addr, &conf.client_id).await?; - - let listener = TcpListener::bind (("127.0.0.1", conf.client_tcp_port)).await?; - - debug! ("Accepting local TCP connections from P1 at {}", conf.client_tcp_port); - - // End of per-port stuff - // Beginning of per-connection stuff - - let task_tcp_server = tokio::spawn (async move { - let running = true; - while running { - let (tcp_socket, _) = listener.accept ().await?; - let connection = connection.clone (); - let server_id = conf.server_id.clone (); - let server_tcp_port = conf.server_tcp_port; - - tokio::spawn (async move { - let (local_recv, local_send) = tcp_socket.into_split (); - - debug! ("Starting PTTH connection"); - - let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id, server_tcp_port).await?; - - trace! ("Relaying bytes..."); - - let ptth_conn = quic_demo::connection::NewConnection { - local_send, - local_recv, - relay_send, - relay_recv, - }.build (); - - ptth_conn.wait_for_close ().await?; - - debug! ("Ended PTTH connection"); - - Ok::<_, anyhow::Error> (()) - }); - } - - Ok::<_, anyhow::Error> (()) - }); - - task_tcp_server.await??; + let client = P2Client::connect (conf)?; + client.run ().await?; Ok (()) } +pub struct P2Client { + endpoint: quinn::Endpoint, + conf: Arc , +} + +impl P2Client { + pub fn connect (conf: Config) -> anyhow::Result { + let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?; + let conf = Arc::new (conf); + + Ok (Self { + endpoint, + conf, + }) + } + + pub async fn run (&self) -> anyhow::Result <()> { + debug! ("P2 client connecting to P3 relay server"); + + let conf = Arc::clone (&self.conf); + + let quinn::NewConnection { + connection, + .. + } = protocol::p2_connect_to_p3 (&self.endpoint, &conf.relay_addr, &conf.client_id).await?; + + let listener = TcpListener::bind (("127.0.0.1", conf.client_tcp_port)).await?; + + debug! ("Accepting local TCP connections from P1 at {}", conf.client_tcp_port); + + // End of per-port stuff + // Beginning of per-connection stuff + + let task_tcp_server = tokio::spawn (async move { + let running = true; + while running { + let (tcp_socket, _) = listener.accept ().await?; + let connection = connection.clone (); + let server_id = conf.server_id.clone (); + let server_tcp_port = conf.server_tcp_port; + + tokio::spawn (async move { + let (local_recv, local_send) = tcp_socket.into_split (); + + debug! ("Starting PTTH connection"); + + let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id, server_tcp_port).await?; + + trace! ("Relaying bytes..."); + + let ptth_conn = quic_demo::connection::NewConnection { + local_send, + local_recv, + relay_send, + relay_recv, + }.build (); + + ptth_conn.wait_for_close ().await?; + + debug! ("Ended PTTH connection"); + + Ok::<_, anyhow::Error> (()) + }); + } + + Ok::<_, anyhow::Error> (()) + }); + + task_tcp_server.await??; + + Ok (()) + } +} + /// A filled-out config for constructing a P2 client #[derive (Clone)] pub struct Config { diff --git a/prototypes/quic_demo/src/executable_end_server.rs b/prototypes/quic_demo/src/executable_end_server.rs index 43d7833..769eda1 100644 --- a/prototypes/quic_demo/src/executable_end_server.rs +++ b/prototypes/quic_demo/src/executable_end_server.rs @@ -25,7 +25,7 @@ pub async fn main (args: &[OsString], shutdown_rx: Option , @@ -95,7 +94,7 @@ pub struct P4EndServer { } impl P4EndServer { - pub async fn connect (conf: Config) -> anyhow::Result { + pub fn connect (conf: Config) -> anyhow::Result { trace! ("P4 end server making its QUIC endpoint"); let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;