Skip to content

Commit

Permalink
simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber committed Oct 29, 2024
1 parent ecf2b12 commit 2c067b2
Showing 1 changed file with 11 additions and 54 deletions.
65 changes: 11 additions & 54 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ pub enum ServiceError {

pub struct ReleaseToken;

enum IncomingEvent {
Connected {
addr: SocketAddr,
pos: Position,
fingerprint: String,
},
Disconnected {
addr: SocketAddr,
},
}

#[derive(Debug)]
pub struct Incoming {
fingerprint: String,
Expand All @@ -70,7 +59,6 @@ pub struct Service {
notifies: Rc<Notifies>,
pub(crate) config: Rc<Config>,
pending_frontend_events: Rc<RefCell<VecDeque<FrontendEvent>>>,
pending_incoming: Rc<RefCell<VecDeque<IncomingEvent>>>,
capture_status: Rc<Cell<Status>>,
pub(crate) emulation_status: Rc<Cell<Status>>,
/// keep track of registered connections to avoid duplicate barriers
Expand All @@ -83,7 +71,6 @@ pub struct Service {

#[derive(Default)]
struct Notifies {
incoming: Notify,
frontend_event_pending: Notify,
}

Expand Down Expand Up @@ -119,7 +106,6 @@ impl Service {
public_key_fingerprint,
config: Rc::new(config),
client_manager,
pending_incoming: Default::default(),
port,
notifies: Default::default(),
pending_frontend_events: Rc::new(RefCell::new(VecDeque::new())),
Expand Down Expand Up @@ -236,30 +222,19 @@ impl Service {
frontend_listener.broadcast(event).await;
}
},
_ = self.notifies.incoming.notified() => {
while let Some(incoming) = {
let incoming = self.pending_incoming.borrow_mut().pop_front();
incoming
} {
match incoming {
IncomingEvent::Connected { addr, pos, fingerprint } => {
// check if already registered
if self.incoming_conns.borrow_mut().insert(addr) {
self.add_incoming(addr, pos, fingerprint.clone(), &capture);
self.notify_frontend(FrontendEvent::IncomingConnected(fingerprint, addr, pos));
}
},
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 } => {
// check if already registered
if self.incoming_conns.borrow_mut().insert(addr) {
self.add_incoming(addr, pos, fingerprint.clone(), &capture);
self.notify_frontend(FrontendEvent::IncomingConnected(fingerprint, addr, pos));
}
}
EmulationEvent::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 {
Ok(port) => {
self.port.replace(port);
Expand Down Expand Up @@ -490,22 +465,4 @@ impl Service {
self.client_manager.set_resolving(handle, status);
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();
}
}

0 comments on commit 2c067b2

Please sign in to comment.