use slab instead of reinventing the wheel (#112)

This commit is contained in:
Ferdinand Schober
2024-04-26 00:10:04 +02:00
committed by GitHub
parent 279e582698
commit 3e96b42067
14 changed files with 83 additions and 115 deletions

View File

@@ -84,8 +84,8 @@ async fn handle_capture_event(
pressed_keys: &mut HashSet<scancode::Linux>,
release_bind: &[scancode::Linux],
) -> Result<()> {
let (c, mut e) = event;
log::trace!("({c}) {e:?}");
let (handle, mut e) = event;
log::trace!("({handle}) {e:?}");
if let Event::Keyboard(KeyboardEvent::Key { key, state, .. }) = e {
update_pressed_keys(pressed_keys, key, state);
@@ -107,7 +107,7 @@ async fn handle_capture_event(
// get client state for handle
let mut client_manager = server.client_manager.borrow_mut();
let client_state = match client_manager.get_mut(c) {
let client_state = match client_manager.get_mut(handle) {
Some(state) => state,
None => {
// should not happen
@@ -123,10 +123,8 @@ async fn handle_capture_event(
// we get a leave event
if let Event::Enter() = e {
server.state.replace(State::AwaitingLeave);
server
.active_client
.replace(Some(client_state.client.handle));
log::trace!("Active client => {}", client_state.client.handle);
server.active_client.replace(Some(handle));
log::trace!("Active client => {}", handle);
start_timer = true;
log::trace!("STATE ===> AwaitingLeave");
enter = true;

View File

@@ -65,7 +65,7 @@ pub fn new(
.client_manager
.borrow()
.get_client_states()
.map(|s| s.client.handle)
.map(|(h, _)| h)
.collect::<Vec<_>>();
for client in clients {
release_keys(&server, &mut emulate, client).await;

View File

@@ -137,7 +137,7 @@ async fn handle_frontend_event(
.client_manager
.borrow()
.get_client_states()
.map(|s| (s.client.clone(), s.active))
.map(|(h, s)| (h, s.client.clone(), s.active))
.collect();
notify_all(frontend, FrontendEvent::Enumerate(clients)).await;
}
@@ -199,7 +199,7 @@ pub async fn add_client(
.unwrap()
.client
.clone();
notify_all(frontend, FrontendEvent::Created(client)).await;
notify_all(frontend, FrontendEvent::Created(handle, client)).await;
}
pub async fn deactivate_client(
@@ -207,12 +207,12 @@ pub async fn deactivate_client(
frontend: &mut FrontendListener,
capture: &Sender<CaptureEvent>,
emulate: &Sender<EmulationEvent>,
client: ClientHandle,
handle: ClientHandle,
) {
let (client, _) = match server.client_manager.borrow_mut().get_mut(client) {
let (client, _) = match server.client_manager.borrow_mut().get_mut(handle) {
Some(state) => {
state.active = false;
(state.client.handle, state.client.pos)
(handle, state.client.pos)
}
None => return,
};
@@ -265,24 +265,24 @@ pub async fn remove_client(
frontend: &mut FrontendListener,
capture: &Sender<CaptureEvent>,
emulate: &Sender<EmulationEvent>,
client: ClientHandle,
handle: ClientHandle,
) {
let Some((client, active)) = server
let Some(active) = server
.client_manager
.borrow_mut()
.remove_client(client)
.map(|s| (s.client.handle, s.active))
.remove_client(handle)
.map(|s| s.active)
else {
return;
};
if active {
let destroy = ClientEvent::Destroy(client);
let destroy = ClientEvent::Destroy(handle);
let _ = capture.send(CaptureEvent::ClientEvent(destroy)).await;
let _ = emulate.send(EmulationEvent::ClientEvent(destroy)).await;
}
let event = FrontendEvent::Deleted(client);
let event = FrontendEvent::Deleted(handle);
notify_all(frontend, event).await;
}
@@ -296,7 +296,7 @@ async fn update_client(
) {
let (handle, hostname, port, pos) = client_update;
let mut changed = false;
let (hostname, handle, active) = {
let (hostname, active) = {
// retrieve state
let mut client_manager = server.client_manager.borrow_mut();
let Some(state) = client_manager.get_mut(handle) else {
@@ -325,11 +325,7 @@ async fn update_client(
}
log::debug!("client updated: {:?}", state);
(
state.client.hostname.clone(),
state.client.handle,
state.active,
)
(state.client.hostname.clone(), state.active)
};
// resolve dns if something changed
@@ -358,5 +354,5 @@ async fn update_client(
.unwrap()
.client
.clone();
notify_all(frontend, FrontendEvent::Updated(client)).await;
notify_all(frontend, FrontendEvent::Updated(handle, client)).await;
}

View File

@@ -34,8 +34,8 @@ pub fn new(
// if receiving we care about clients with pressed keys
client_manager
.get_client_states_mut()
.filter(|s| !s.pressed_keys.is_empty())
.map(|s| s.client.handle)
.filter(|(_, s)| !s.pressed_keys.is_empty())
.map(|(h, _)| h)
.collect()
} else {
// if sending we care about the active client
@@ -64,7 +64,7 @@ pub fn new(
};
// reset alive
for state in client_manager.get_client_states_mut() {
for (_, state) in client_manager.get_client_states_mut() {
state.alive = false;
}