From 87304744adee6a9e165b86735ed5f0b4420949b7 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Thu, 10 Oct 2024 17:15:34 +0200 Subject: [PATCH] fix release logic --- src/capture.rs | 14 +++++--------- src/emulation.rs | 3 ++- src/service.rs | 11 ++++++++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/capture.rs b/src/capture.rs index 3a9e04e..0f4bdfa 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -128,7 +128,7 @@ async fn do_capture( capture.create(handle, to_capture_pos(pos)).await?; } - let mut state = State::Receiving; + let mut state = State::WaitingForAck; loop { tokio::select! { @@ -152,7 +152,10 @@ async fn do_capture( state = State::Sending; } // client disconnected - ProtoEvent::Leave(_) => release_capture(&mut capture, server).await?, + ProtoEvent::Leave(_) => { + log::info!("releasing capture: Server notified Leave"); + release_capture(&mut capture, server).await?; + }, _ => {} } }, @@ -197,7 +200,6 @@ macro_rules! debounce { #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum State { - Receiving, WaitingForAck, Sending, } @@ -213,12 +215,6 @@ async fn handle_capture_event( let (handle, event) = event; log::trace!("({handle}): {event:?}"); - if server.should_release.borrow_mut().take().is_some() && *state != State::Receiving { - log::info!("releasing capture: a client entered the device"); - *state = State::Receiving; - return release_capture(capture, server).await; - } - if capture.keys_pressed(&server.config.release_bind) { log::info!("releasing capture: release-bind pressed"); return release_capture(capture, server).await; diff --git a/src/emulation.rs b/src/emulation.rs index 3dff0d9..dbdf7f0 100644 --- a/src/emulation.rs +++ b/src/emulation.rs @@ -53,7 +53,7 @@ impl Emulation { match event { ProtoEvent::Enter(pos) => { if let Some(cert) = listener.get_certificate_fingerprint(addr).await { - log::info!("{addr} entered this device"); + log::info!("releasing capture: {addr} entered this device"); service.release_capture(); listener.reply(addr, ProtoEvent::Ack(0)).await; service.register_incoming(addr, to_ipc_pos(pos), cert); @@ -69,6 +69,7 @@ impl Emulation { } } addr = release_rx.recv() => { + // notify the other end that we hit a barrier (should release capture) let addr = addr.expect("channel closed"); listener.reply(addr, ProtoEvent::Leave(0)).await; } diff --git a/src/service.rs b/src/service.rs index e0d6394..2dcc438 100644 --- a/src/service.rs +++ b/src/service.rs @@ -75,7 +75,6 @@ pub struct Service { pending_incoming: Rc>>, capture_status: Rc>, pub(crate) emulation_status: Rc>, - pub(crate) should_release: Rc>>, incoming_conns: Rc>>, cert: Certificate, next_trigger_handle: u64, @@ -89,6 +88,7 @@ struct Notifies { port_changed: Notify, frontend_event_pending: Notify, cancel: CancellationToken, + release: Notify, } impl Service { @@ -136,7 +136,6 @@ impl Service { capture_status: Default::default(), emulation_status: Default::default(), incoming_conns: Rc::new(RefCell::new(HashMap::new())), - should_release: Default::default(), next_trigger_handle: 0, }; Ok(service) @@ -208,7 +207,13 @@ impl Service { } } }, + _ = self.notifies.release.notified() => { + self.set_active(None); + capture.release(); + } handle = capture.entered() => { + // we entered the capture zone for an incoming connection + // => notify it that its capture should be released if let Some(incoming) = self.incoming_conns.borrow().get(&handle) { emulation.notify_release(incoming.addr); } @@ -497,7 +502,7 @@ impl Service { } pub(crate) fn release_capture(&self) { - self.should_release.replace(Some(ReleaseToken)); + self.notifies.release.notify_one(); } pub(crate) fn set_active(&self, handle: Option) {