mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-12 12:41:30 +03:00
simplify logic
This commit is contained in:
@@ -43,17 +43,6 @@ pub enum ServiceError {
|
|||||||
|
|
||||||
pub struct ReleaseToken;
|
pub struct ReleaseToken;
|
||||||
|
|
||||||
enum IncomingEvent {
|
|
||||||
Connected {
|
|
||||||
addr: SocketAddr,
|
|
||||||
pos: Position,
|
|
||||||
fingerprint: String,
|
|
||||||
},
|
|
||||||
Disconnected {
|
|
||||||
addr: SocketAddr,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Incoming {
|
pub struct Incoming {
|
||||||
fingerprint: String,
|
fingerprint: String,
|
||||||
@@ -70,7 +59,6 @@ pub struct Service {
|
|||||||
notifies: Rc<Notifies>,
|
notifies: Rc<Notifies>,
|
||||||
pub(crate) config: Rc<Config>,
|
pub(crate) config: Rc<Config>,
|
||||||
pending_frontend_events: Rc<RefCell<VecDeque<FrontendEvent>>>,
|
pending_frontend_events: Rc<RefCell<VecDeque<FrontendEvent>>>,
|
||||||
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>>,
|
||||||
/// keep track of registered connections to avoid duplicate barriers
|
/// keep track of registered connections to avoid duplicate barriers
|
||||||
@@ -83,7 +71,6 @@ pub struct Service {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Notifies {
|
struct Notifies {
|
||||||
incoming: Notify,
|
|
||||||
frontend_event_pending: Notify,
|
frontend_event_pending: Notify,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +106,6 @@ impl Service {
|
|||||||
public_key_fingerprint,
|
public_key_fingerprint,
|
||||||
config: Rc::new(config),
|
config: Rc::new(config),
|
||||||
client_manager,
|
client_manager,
|
||||||
pending_incoming: Default::default(),
|
|
||||||
port,
|
port,
|
||||||
notifies: Default::default(),
|
notifies: Default::default(),
|
||||||
pending_frontend_events: Rc::new(RefCell::new(VecDeque::new())),
|
pending_frontend_events: Rc::new(RefCell::new(VecDeque::new())),
|
||||||
@@ -236,30 +222,19 @@ impl Service {
|
|||||||
frontend_listener.broadcast(event).await;
|
frontend_listener.broadcast(event).await;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ = self.notifies.incoming.notified() => {
|
event = emulation.event() => match event {
|
||||||
while let Some(incoming) = {
|
EmulationEvent::Connected { addr, pos, fingerprint } => {
|
||||||
let incoming = self.pending_incoming.borrow_mut().pop_front();
|
// check if already registered
|
||||||
incoming
|
if self.incoming_conns.borrow_mut().insert(addr) {
|
||||||
} {
|
self.add_incoming(addr, pos, fingerprint.clone(), &capture);
|
||||||
match incoming {
|
self.notify_frontend(FrontendEvent::IncomingConnected(fingerprint, addr, pos));
|
||||||
IncomingEvent::Connected { addr, pos, fingerprint } => {
|
}
|
||||||
// check if already registered
|
}
|
||||||
if self.incoming_conns.borrow_mut().insert(addr) {
|
EmulationEvent::Disconnected { addr } => {
|
||||||
self.add_incoming(addr, pos, fingerprint.clone(), &capture);
|
if let Some(fp) = self.remove_incoming(addr, &capture) {
|
||||||
self.notify_frontend(FrontendEvent::IncomingConnected(fingerprint, addr, pos));
|
self.notify_frontend(FrontendEvent::IncomingDisconnected(fp));
|
||||||
}
|
|
||||||
},
|
|
||||||
IncomingEvent::Disconnected { addr } => {
|
|
||||||
if let Some(fp) = self.remove_incoming(addr, &capture) {
|
|
||||||
self.notify_frontend(FrontendEvent::IncomingDisconnected(fp));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
event = emulation.event() => match event {
|
|
||||||
EmulationEvent::Connected { addr, pos, fingerprint } => self.register_incoming(addr, pos, fingerprint),
|
|
||||||
EmulationEvent::Disconnected { addr } => self.deregister_incoming(addr),
|
|
||||||
EmulationEvent::PortChanged(port) => match port {
|
EmulationEvent::PortChanged(port) => match port {
|
||||||
Ok(port) => {
|
Ok(port) => {
|
||||||
self.port.replace(port);
|
self.port.replace(port);
|
||||||
@@ -490,22 +465,4 @@ impl Service {
|
|||||||
self.client_manager.set_resolving(handle, status);
|
self.client_manager.set_resolving(handle, status);
|
||||||
self.client_updated(handle);
|
self.client_updated(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn register_incoming(&self, addr: SocketAddr, pos: Position, fingerprint: String) {
|
|
||||||
self.pending_incoming
|
|
||||||
.borrow_mut()
|
|
||||||
.push_back(IncomingEvent::Connected {
|
|
||||||
addr,
|
|
||||||
pos,
|
|
||||||
fingerprint,
|
|
||||||
});
|
|
||||||
self.notifies.incoming.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn deregister_incoming(&self, addr: SocketAddr) {
|
|
||||||
self.pending_incoming
|
|
||||||
.borrow_mut()
|
|
||||||
.push_back(IncomingEvent::Disconnected { addr });
|
|
||||||
self.notifies.incoming.notify_one();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user