🐛 bug: P3 no longer closes when the PTTH connection closes

main
_ 2021-07-17 20:36:29 +00:00
parent c8e44bb740
commit 3442bf220b
2 changed files with 96 additions and 57 deletions

View File

@ -29,7 +29,7 @@ async fn main () -> anyhow::Result <()> {
let listener = TcpListener::bind ("127.0.0.1:30381").await?;
debug! ("Accepting local TCP connections");
debug! ("Accepting local TCP connections from P1");
loop {
let (tcp_socket, _) = listener.accept ().await?;

View File

@ -26,10 +26,12 @@ async fn main () -> anyhow::Result <()> {
let mut resp_buf = [0u8, 0, 0, 0];
recv.read_exact (&mut resp_buf).await?;
debug! ("Waiting for relay server to forward a bi stream");
debug! ("Accepting bi streams from P3");
loop {
let (mut relay_send, mut relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
tokio::spawn (async move {
let mut req_buf = [0, 0, 0, 0];
relay_recv.read_exact (&mut req_buf).await?;
@ -38,7 +40,7 @@ async fn main () -> anyhow::Result <()> {
debug! ("Connecting to TCP end server");
let stream = TcpStream::connect ("127.0.0.1:30382").await?;
let (mut local_recv, mut local_send) = stream.into_split ();
let (local_recv, local_send) = stream.into_split ();
let resp_buf = [20, 0, 0, 0];
relay_send.write_all (&resp_buf).await?;
@ -46,6 +48,42 @@ async fn main () -> anyhow::Result <()> {
debug! ("Relaying bytes...");
let ptth_conn = PtthNewConnection {
local_send,
local_recv,
relay_send,
relay_recv,
}.build ();
ptth_conn.uplink_task.await??;
ptth_conn.downlink_task.await??;
Ok::<_, anyhow::Error> (())
});
}
}
struct PtthNewConnection {
local_send: tokio::net::tcp::OwnedWriteHalf,
local_recv: tokio::net::tcp::OwnedReadHalf,
relay_send: quinn::SendStream,
relay_recv: quinn::RecvStream,
}
struct PtthConnection {
uplink_task: JoinHandle <anyhow::Result <()>>,
downlink_task: JoinHandle <anyhow::Result <()>>,
}
impl PtthNewConnection {
fn build (self) -> PtthConnection {
let Self {
mut local_send,
mut local_recv,
mut relay_send,
mut relay_recv,
} = self;
let uplink_task = tokio::spawn (async move {
// Downlink - Relay server to local client
@ -80,8 +118,9 @@ async fn main () -> anyhow::Result <()> {
Ok::<_, anyhow::Error> (())
});
uplink_task.await??;
downlink_task.await??;
Ok (())
PtthConnection {
uplink_task,
downlink_task,
}
}
}