♻️ refactor

Remove the old one-shot code and extract `handle_quic_connection`
main
_ 2021-07-17 07:51:47 +00:00
parent 5417fbc77b
commit b75b7c5a74
1 changed files with 143 additions and 181 deletions

View File

@ -16,6 +16,97 @@ async fn main () -> anyhow::Result <()> {
// Each new peer QUIC connection gets its own task
tokio::spawn (async move {
handle_quic_connection (relay_state, conn).await
});
}
Ok (())
}
#[derive (Default)]
struct RelayState {
p4_server_proxies: HashMap <u8, P4State>,
}
struct P4State {
req_channel: mpsc::Sender <RequestP2ToP4>,
}
impl RelayState {
}
struct RequestP2ToP4 {
client_send: quinn::SendStream,
client_recv: quinn::RecvStream,
client_id: u8,
}
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? {
if bytes_read == 0 {
break;
}
let buf_slice = &buf [0..bytes_read];
debug! ("Uplink relaying {} bytes", 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];
debug! ("Downlink relaying {} bytes", bytes_read);
client_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
PtthConnection {
uplink_task,
downlink_task,
}
}
}
async fn handle_quic_connection (
relay_state: Arc <Mutex <RelayState>>,
conn: quinn::Connecting,
) -> anyhow::Result <()> {
let quinn::NewConnection {
connection,
mut bi_streams,
@ -152,133 +243,4 @@ async fn main () -> anyhow::Result <()> {
debug! ("Peer {} disconnected", peer_id);
Ok::<_, anyhow::Error> (())
});
}
if false {
debug! ("Waiting for end server to connect");
let end_server_conn = incoming.next ().await.ok_or_else (|| anyhow::anyhow! ("No end server connection"))?;
let end_server_conn = end_server_conn.await?;
let quinn::NewConnection {
connection: end_server_conn,
..
} = end_server_conn;
debug! ("Waiting for client to connect");
let client_conn = incoming.next ().await.ok_or_else (|| anyhow::anyhow! ("No client connection"))?;
let client_conn = client_conn.await?;
let quinn::NewConnection {
connection: _client_conn,
bi_streams: mut client_incoming_bi_streams,
..
} = client_conn;
debug! ("Waiting for client to open 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");
let (server_send, server_recv) = end_server_conn.open_bi ().await?;
debug! ("Relaying bytes...");
let ptth_conn = PtthNewConnection {
client_send,
client_recv,
server_send,
server_recv,
}.build ();
ptth_conn.uplink_task.await??;
ptth_conn.downlink_task.await??;
}
Ok (())
}
#[derive (Default)]
struct RelayState {
p4_server_proxies: HashMap <u8, P4State>,
}
struct P4State {
req_channel: mpsc::Sender <RequestP2ToP4>,
}
impl RelayState {
}
struct RequestP2ToP4 {
client_send: quinn::SendStream,
client_recv: quinn::RecvStream,
client_id: u8,
}
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? {
if bytes_read == 0 {
break;
}
let buf_slice = &buf [0..bytes_read];
debug! ("Uplink relaying {} bytes", 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];
debug! ("Downlink relaying {} bytes", bytes_read);
client_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
PtthConnection {
uplink_task,
downlink_task,
}
}
}