♻️ 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,46 +35,79 @@ 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 ();
let uplink_task = tokio::spawn (async move { ptth_conn.uplink_task.await??;
// Uplink - Client to end server ptth_conn.downlink_task.await??;
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = client_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
server_send.write_all (buf_slice).await?;
}
debug! ("Uplink closed");
Ok::<_, anyhow::Error> (())
});
let downlink_task = tokio::spawn (async move {
// Downlink - End server to client
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = server_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
client_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
uplink_task.await??;
downlink_task.await??;
Ok (()) 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 {
// Uplink - Client to end server
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = client_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
server_send.write_all (buf_slice).await?;
}
debug! ("Uplink closed");
Ok::<_, anyhow::Error> (())
});
let downlink_task = tokio::spawn (async move {
// Downlink - End server to client
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = server_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
client_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
PtthConnection {
uplink_task,
downlink_task,
}
}
}