From e4285ec17dddd5bc59ce96436daf1e760f63ae3e Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 10 Oct 2021 18:17:50 +0000 Subject: [PATCH] :bug: bug: fix crash when trying to open the same port twice --- prototypes/ptth_quic_client_gui/src/main.rs | 10 ++++++---- prototypes/quic_demo/src/client_proxy.rs | 17 +++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/prototypes/ptth_quic_client_gui/src/main.rs b/prototypes/ptth_quic_client_gui/src/main.rs index 371195c..4d37f3e 100644 --- a/prototypes/ptth_quic_client_gui/src/main.rs +++ b/prototypes/ptth_quic_client_gui/src/main.rs @@ -53,10 +53,12 @@ impl GuiClient <'_> { let params = self.gui_ports [port_idx].get_params ()?; let _guard = self.rt.enter (); - self.forwarding_instances [port_idx].replace (ForwardingInstance::new ( + let forwarding_instance = self.rt.block_on (ForwardingInstance::new ( connection_p2_p3, params, - )); + ))?; + + self.forwarding_instances [port_idx].replace (forwarding_instance); self.gui_ports [port_idx].set_forwarding (true); self.sync_status (); @@ -259,9 +261,9 @@ impl GuiPort { input_server_id.set_value ("bogus_server"); input_server_port.set_value ("5900"); - but_open.set_trigger (CallbackTrigger::Changed); + but_open.set_trigger (CallbackTrigger::Release); but_open.emit (fltk_tx, Message::OpenPort (port_idx)); - but_close.set_trigger (CallbackTrigger::Changed); + but_close.set_trigger (CallbackTrigger::Release); but_close.emit (fltk_tx, Message::ClosePort (port_idx)); set_active (&mut but_open, true); diff --git a/prototypes/quic_demo/src/client_proxy.rs b/prototypes/quic_demo/src/client_proxy.rs index 101ef79..a36b06b 100644 --- a/prototypes/quic_demo/src/client_proxy.rs +++ b/prototypes/quic_demo/src/client_proxy.rs @@ -12,23 +12,27 @@ pub struct ForwardingInstance { } impl ForwardingInstance { - pub fn new ( + pub async fn new ( connection_p2_p3: quinn::Connection, params: ForwardingParams, - ) -> Self + ) -> anyhow::Result { let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true); + let listener = TcpListener::bind (("127.0.0.1", params.client_tcp_port)).await?; + trace! ("Accepting local TCP connections from P1 on {}", params.client_tcp_port); + let task = tokio::spawn (forward_port ( + listener, connection_p2_p3, params, shutdown_flag_rx )); - Self { + Ok (Self { task, shutdown_flag, - } + }) } pub async fn close (self) -> anyhow::Result <()> { @@ -53,6 +57,7 @@ pub struct ForwardingParams { /// the same client:server port combination pub async fn forward_port ( + listener: TcpListener, connection_p2_p3: quinn::Connection, params: ForwardingParams, mut shutdown_flag_rx: tokio::sync::watch::Receiver , @@ -64,10 +69,6 @@ pub async fn forward_port ( server_tcp_port, } = params; - let listener = TcpListener::bind (("127.0.0.1", client_tcp_port)).await?; - - trace! ("Accepting local TCP connections from P1 on {}", client_tcp_port); - while *shutdown_flag_rx.borrow () { tokio::select! { x = listener.accept () => {