fix unexpected return via whitelist block by raii

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-06-13 15:46:16 +08:00
parent 6cb35ae01e
commit 59cfb44c08

View File

@ -241,12 +241,12 @@ impl Connection {
id: i32, id: i32,
server: super::ServerPtrWeak, server: super::ServerPtrWeak,
) { ) {
let _raii_id = raii::ConnectionID::new(id);
let hash = Hash { let hash = Hash {
salt: Config::get_salt(), salt: Config::get_salt(),
challenge: Config::get_auto_password(6), challenge: Config::get_auto_password(6),
..Default::default() ..Default::default()
}; };
ALIVE_CONNS.lock().unwrap().push(id);
let (tx_from_cm_holder, mut rx_from_cm) = mpsc::unbounded_channel::<ipc::Data>(); let (tx_from_cm_holder, mut rx_from_cm) = mpsc::unbounded_channel::<ipc::Data>();
// holding tx_from_cm_holder to avoid cpu burning of rx_from_cm.recv when all sender closed // holding tx_from_cm_holder to avoid cpu burning of rx_from_cm.recv when all sender closed
let tx_from_cm = tx_from_cm_holder.clone(); let tx_from_cm = tx_from_cm_holder.clone();
@ -627,22 +627,12 @@ impl Connection {
conn.post_conn_audit(json!({ conn.post_conn_audit(json!({
"action": "close", "action": "close",
})); }));
let mut active_conns_lock = ALIVE_CONNS.lock().unwrap();
active_conns_lock.retain(|&c| c != id);
if let Some(s) = conn.server.upgrade() { if let Some(s) = conn.server.upgrade() {
let mut s = s.write().unwrap(); let mut s = s.write().unwrap();
s.remove_connection(&conn.inner); s.remove_connection(&conn.inner);
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
try_stop_record_cursor_pos(); try_stop_record_cursor_pos();
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if active_conns_lock.is_empty() {
video_service::reset_resolutions();
}
#[cfg(all(windows, feature = "virtual_display_driver"))]
if active_conns_lock.is_empty() {
video_service::try_plug_out_virtual_display();
}
log::info!("#{} connection loop exited", id); log::info!("#{} connection loop exited", id);
} }
@ -2538,3 +2528,30 @@ impl Drop for Connection {
self.release_pressed_modifiers(); self.release_pressed_modifiers();
} }
} }
mod raii {
use super::*;
pub struct ConnectionID(i32);
impl ConnectionID {
pub fn new(id: i32) -> Self {
ALIVE_CONNS.lock().unwrap().push(id);
Self(id)
}
}
impl Drop for ConnectionID {
fn drop(&mut self) {
let mut active_conns_lock = ALIVE_CONNS.lock().unwrap();
active_conns_lock.retain(|&c| c != self.0);
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if active_conns_lock.is_empty() {
video_service::reset_resolutions();
}
#[cfg(all(windows, feature = "virtual_display_driver"))]
if active_conns_lock.is_empty() {
video_service::try_plug_out_virtual_display();
}
}
}
}