♻️ refactor

main
_ 2021-07-17 06:26:06 +00:00
parent b2fd8a8ede
commit b344e3f8ee
1 changed files with 68 additions and 33 deletions

View File

@ -1,3 +1,5 @@
use tokio::task::JoinHandle;
use quic_demo::prelude::*; use quic_demo::prelude::*;
#[tokio::main] #[tokio::main]
@ -33,15 +35,47 @@ async fn main () -> anyhow::Result <()> {
debug! ("Waiting for client to open bi stream"); debug! ("Waiting for client to open bi stream");
let (mut client_send, mut client_recv) = client_incoming_bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Client didn't open a bi stream"))??; let (client_send, client_recv) = client_incoming_bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Client didn't open a bi stream"))??;
debug! ("Opening bi stream to the end server"); debug! ("Opening bi stream to the end server");
let (mut server_send, mut server_recv) = end_server_conn.open_bi ().await?; let (server_send, server_recv) = end_server_conn.open_bi ().await?;
debug! ("Relaying bytes..."); debug! ("Relaying bytes...");
// Remember to swap tx and rx for patch cables let ptth_conn = PtthNewConnection {
client_send,
client_recv,
server_send,
server_recv,
}.build ();
ptth_conn.uplink_task.await??;
ptth_conn.downlink_task.await??;
Ok (())
}
struct PtthNewConnection {
client_send: quinn::SendStream,
client_recv: quinn::RecvStream,
server_send: quinn::SendStream,
server_recv: quinn::RecvStream,
}
struct PtthConnection {
uplink_task: JoinHandle <anyhow::Result <()>>,
downlink_task: JoinHandle <anyhow::Result <()>>,
}
impl PtthNewConnection {
fn build (self) -> PtthConnection {
let Self {
mut client_send,
mut client_recv,
mut server_send,
mut server_recv,
} = self;
let uplink_task = tokio::spawn (async move { let uplink_task = tokio::spawn (async move {
// Uplink - Client to end server // Uplink - Client to end server
@ -71,8 +105,9 @@ async fn main () -> anyhow::Result <()> {
Ok::<_, anyhow::Error> (()) Ok::<_, anyhow::Error> (())
}); });
uplink_task.await??; PtthConnection {
downlink_task.await??; uplink_task,
downlink_task,
Ok (()) }
}
} }