fix release logic

This commit is contained in:
Ferdinand Schober
2024-10-10 17:15:34 +02:00
parent 0277b6b4d3
commit 87304744ad
3 changed files with 15 additions and 13 deletions

View File

@@ -128,7 +128,7 @@ async fn do_capture(
capture.create(handle, to_capture_pos(pos)).await?; capture.create(handle, to_capture_pos(pos)).await?;
} }
let mut state = State::Receiving; let mut state = State::WaitingForAck;
loop { loop {
tokio::select! { tokio::select! {
@@ -152,7 +152,10 @@ async fn do_capture(
state = State::Sending; state = State::Sending;
} }
// client disconnected // 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)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum State { enum State {
Receiving,
WaitingForAck, WaitingForAck,
Sending, Sending,
} }
@@ -213,12 +215,6 @@ async fn handle_capture_event(
let (handle, event) = event; let (handle, event) = event;
log::trace!("({handle}): {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) { if capture.keys_pressed(&server.config.release_bind) {
log::info!("releasing capture: release-bind pressed"); log::info!("releasing capture: release-bind pressed");
return release_capture(capture, server).await; return release_capture(capture, server).await;

View File

@@ -53,7 +53,7 @@ impl Emulation {
match event { match event {
ProtoEvent::Enter(pos) => { ProtoEvent::Enter(pos) => {
if let Some(cert) = listener.get_certificate_fingerprint(addr).await { 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(); service.release_capture();
listener.reply(addr, ProtoEvent::Ack(0)).await; listener.reply(addr, ProtoEvent::Ack(0)).await;
service.register_incoming(addr, to_ipc_pos(pos), cert); service.register_incoming(addr, to_ipc_pos(pos), cert);
@@ -69,6 +69,7 @@ impl Emulation {
} }
} }
addr = release_rx.recv() => { addr = release_rx.recv() => {
// notify the other end that we hit a barrier (should release capture)
let addr = addr.expect("channel closed"); let addr = addr.expect("channel closed");
listener.reply(addr, ProtoEvent::Leave(0)).await; listener.reply(addr, ProtoEvent::Leave(0)).await;
} }

View File

@@ -75,7 +75,6 @@ pub struct Service {
pending_incoming: Rc<RefCell<VecDeque<IncomingEvent>>>, pending_incoming: Rc<RefCell<VecDeque<IncomingEvent>>>,
capture_status: Rc<Cell<Status>>, capture_status: Rc<Cell<Status>>,
pub(crate) emulation_status: Rc<Cell<Status>>, pub(crate) emulation_status: Rc<Cell<Status>>,
pub(crate) should_release: Rc<RefCell<Option<ReleaseToken>>>,
incoming_conns: Rc<RefCell<HashMap<ClientHandle, Incoming>>>, incoming_conns: Rc<RefCell<HashMap<ClientHandle, Incoming>>>,
cert: Certificate, cert: Certificate,
next_trigger_handle: u64, next_trigger_handle: u64,
@@ -89,6 +88,7 @@ struct Notifies {
port_changed: Notify, port_changed: Notify,
frontend_event_pending: Notify, frontend_event_pending: Notify,
cancel: CancellationToken, cancel: CancellationToken,
release: Notify,
} }
impl Service { impl Service {
@@ -136,7 +136,6 @@ impl Service {
capture_status: Default::default(), capture_status: Default::default(),
emulation_status: Default::default(), emulation_status: Default::default(),
incoming_conns: Rc::new(RefCell::new(HashMap::new())), incoming_conns: Rc::new(RefCell::new(HashMap::new())),
should_release: Default::default(),
next_trigger_handle: 0, next_trigger_handle: 0,
}; };
Ok(service) Ok(service)
@@ -208,7 +207,13 @@ impl Service {
} }
} }
}, },
_ = self.notifies.release.notified() => {
self.set_active(None);
capture.release();
}
handle = capture.entered() => { 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) { if let Some(incoming) = self.incoming_conns.borrow().get(&handle) {
emulation.notify_release(incoming.addr); emulation.notify_release(incoming.addr);
} }
@@ -497,7 +502,7 @@ impl Service {
} }
pub(crate) fn release_capture(&self) { 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<ClientHandle>) { pub(crate) fn set_active(&self, handle: Option<ClientHandle>) {