mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-21 03:03:20 +03:00
use a guard struct to send enable / disable events
This commit is contained in:
@@ -165,9 +165,12 @@ async fn do_capture(
|
|||||||
r = InputCapture::new(backend) => r?,
|
r = InputCapture::new(backend) => r?,
|
||||||
_ = wait_for_termination(request_rx) => return Ok(()),
|
_ = wait_for_termination(request_rx) => return Ok(()),
|
||||||
};
|
};
|
||||||
event_tx
|
|
||||||
.send(ICaptureEvent::CaptureEnabled)
|
let _capture_guard = DropGuard::new(
|
||||||
.expect("channel closed");
|
event_tx,
|
||||||
|
ICaptureEvent::CaptureEnabled,
|
||||||
|
ICaptureEvent::CaptureDisabled,
|
||||||
|
);
|
||||||
|
|
||||||
let clients = service.client_manager.active_clients();
|
let clients = service.client_manager.active_clients();
|
||||||
let clients = clients.iter().copied().map(|handle| {
|
let clients = clients.iter().copied().map(|handle| {
|
||||||
@@ -190,17 +193,8 @@ async fn do_capture(
|
|||||||
|
|
||||||
let res = do_capture_session(active, &mut capture, conn, event_tx, request_rx, service).await;
|
let res = do_capture_session(active, &mut capture, conn, event_tx, request_rx, service).await;
|
||||||
// FIXME replace with async drop when stabilized
|
// FIXME replace with async drop when stabilized
|
||||||
let res1 = capture.terminate().await;
|
capture.terminate().await?;
|
||||||
|
res
|
||||||
// handle errors
|
|
||||||
res?;
|
|
||||||
res1?;
|
|
||||||
|
|
||||||
event_tx
|
|
||||||
.send(ICaptureEvent::CaptureDisabled)
|
|
||||||
.expect("channel closed");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn do_capture_session(
|
async fn do_capture_session(
|
||||||
@@ -411,3 +405,24 @@ async fn wait_for_termination(rx: &mut Receiver<CaptureRequest>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DropGuard<'a, T> {
|
||||||
|
tx: &'a Sender<T>,
|
||||||
|
on_drop: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> DropGuard<'a, T> {
|
||||||
|
fn new(tx: &'a Sender<T>, on_new: T, on_drop: T) -> Self {
|
||||||
|
tx.send(on_new).expect("channel closed");
|
||||||
|
let on_drop = Some(on_drop);
|
||||||
|
Self { tx, on_drop }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Drop for DropGuard<'a, T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.tx
|
||||||
|
.send(self.on_drop.take().expect("item"))
|
||||||
|
.expect("channel closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -282,9 +282,13 @@ impl EmulationProxy {
|
|||||||
// allow termination event while requesting input emulation
|
// allow termination event while requesting input emulation
|
||||||
_ = wait_for_termination(request_rx) => return Ok(()),
|
_ = wait_for_termination(request_rx) => return Ok(()),
|
||||||
};
|
};
|
||||||
event_tx
|
|
||||||
.send(EmulationEvent::EmulationEnabled)
|
// used to send enabled and disabled events
|
||||||
.expect("channel closed");
|
let _emulation_guard = DropGuard::new(
|
||||||
|
&event_tx,
|
||||||
|
EmulationEvent::EmulationEnabled,
|
||||||
|
EmulationEvent::EmulationDisabled,
|
||||||
|
);
|
||||||
|
|
||||||
// create active handles
|
// create active handles
|
||||||
for &handle in handles.values() {
|
for &handle in handles.values() {
|
||||||
@@ -297,10 +301,6 @@ impl EmulationProxy {
|
|||||||
let res = Self::do_emulation_session(&mut emulation, handles, next_id, request_rx).await;
|
let res = Self::do_emulation_session(&mut emulation, handles, next_id, request_rx).await;
|
||||||
// FIXME replace with async drop when stabilized
|
// FIXME replace with async drop when stabilized
|
||||||
emulation.terminate().await;
|
emulation.terminate().await;
|
||||||
|
|
||||||
event_tx
|
|
||||||
.send(EmulationEvent::EmulationDisabled)
|
|
||||||
.expect("channel closed");
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,3 +372,24 @@ async fn wait_for_termination(rx: &mut Receiver<ProxyRequest>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DropGuard<'a, T> {
|
||||||
|
tx: &'a Sender<T>,
|
||||||
|
on_drop: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> DropGuard<'a, T> {
|
||||||
|
fn new(tx: &'a Sender<T>, on_new: T, on_drop: T) -> Self {
|
||||||
|
tx.send(on_new).expect("channel closed");
|
||||||
|
let on_drop = Some(on_drop);
|
||||||
|
Self { tx, on_drop }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Drop for DropGuard<'a, T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.tx
|
||||||
|
.send(self.on_drop.take().expect("item"))
|
||||||
|
.expect("channel closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user