diff --git a/libs/hbb_common/protos/message.proto b/libs/hbb_common/protos/message.proto index d89370cc7..e67a5d3b1 100644 --- a/libs/hbb_common/protos/message.proto +++ b/libs/hbb_common/protos/message.proto @@ -97,7 +97,6 @@ message PeerInfo { int32 current_display = 5; bool sas_enabled = 6; string version = 7; - int32 conn_id = 8; Features features = 9; SupportedEncoding encoding = 10; SupportedResolutions resolutions = 11; diff --git a/src/client.rs b/src/client.rs index a0a5e6cc0..cce681925 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1073,9 +1073,8 @@ pub struct LoginConfigHandler { config: PeerConfig, pub port_forward: (String, i32), pub version: i64, - pub conn_id: i32, features: Option, - session_id: u64, + pub session_id: u64, // used for local <-> server communication pub supported_encoding: SupportedEncoding, pub restarting_remote_device: bool, pub force_relay: bool, @@ -1123,7 +1122,11 @@ impl LoginConfigHandler { let config = self.load_config(); self.remember = !config.password.is_empty(); self.config = config; - self.session_id = rand::random(); + let mut sid = rand::random(); + if sid == 0 { // you won the lottery + sid = 1; + } + self.session_id = sid; self.supported_encoding = Default::default(); self.restarting_remote_device = false; self.force_relay = !self.get_option("force-always-relay").is_empty() || force_relay; @@ -1669,7 +1672,6 @@ impl LoginConfigHandler { config.keyboard_mode = KeyboardMode::Legacy.to_string(); } } - self.conn_id = pi.conn_id; // no matter if change, for update file time self.save_config(config); self.supported_encoding = pi.encoding.clone().unwrap_or_default(); diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index 3a7c906c6..c5690c9aa 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -1415,11 +1415,8 @@ impl Remote { } } } - Some(message::Union::PeerInfo(pi)) => match pi.conn_id { - crate::SYNC_PEER_INFO_DISPLAYS => { - self.handler.set_displays(&pi.displays); - } - _ => {} + Some(message::Union::PeerInfo(pi)) => { + self.handler.set_displays(&pi.displays); }, _ => {} } diff --git a/src/common.rs b/src/common.rs index ab3e087a3..ef96f0a37 100644 --- a/src/common.rs +++ b/src/common.rs @@ -39,8 +39,6 @@ pub type NotifyMessageBox = fn(String, String, String, String) -> dyn Future ServerPtr { let mut server = Server { connections: HashMap::new(), services: HashMap::new(), - id_count: 0, + id_count: hbb_common::rand::random::() % 1000, }; server.add_service(Box::new(audio_service::new())); server.add_service(Box::new(video_service::new())); diff --git a/src/server/connection.rs b/src/server/connection.rs index f747e47c1..f276be439 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -192,6 +192,7 @@ pub struct Connection { #[cfg(all(target_os = "linux", feature = "linux_headless"))] #[cfg(not(any(feature = "flatpak", feature = "appimage")))] tx_desktop_ready: mpsc::Sender<()>, + closed: bool, } impl ConnInner { @@ -320,8 +321,10 @@ impl Connection { #[cfg(all(target_os = "linux", feature = "linux_headless"))] #[cfg(not(any(feature = "flatpak", feature = "appimage")))] tx_desktop_ready: _tx_desktop_ready, + closed: false, }; if !conn.on_open(addr).await { + conn.closed = true; // sleep to ensure msg got received. sleep(1.).await; return; @@ -563,7 +566,7 @@ impl Connection { match &m.union { Some(misc::Union::StopService(_)) => { conn.send_close_reason_no_retry("").await; - conn.on_close("stop service", true).await; + conn.on_close("stop service", false).await; break; } _ => {}, @@ -634,6 +637,7 @@ impl Connection { #[cfg(not(any(target_os = "android", target_os = "ios")))] try_stop_record_cursor_pos(); } + conn.on_close("End", true).await; log::info!("#{} connection loop exited", id); } @@ -867,6 +871,7 @@ impl Connection { v["id"] = json!(Config::get_id()); v["uuid"] = json!(crate::encode64(hbb_common::get_uuid())); v["conn_id"] = json!(self.inner.id); + v["session_id"] = json!(self.lr.session_id); tokio::spawn(async move { allow_err!(Self::post_audit_async(url, v).await); }); @@ -948,7 +953,6 @@ impl Connection { let mut res = LoginResponse::new(); let mut pi = PeerInfo { username: username.clone(), - conn_id: self.inner.id, version: VERSION.to_owned(), ..Default::default() }; @@ -1228,6 +1232,7 @@ impl Connection { .lock() .unwrap() .retain(|_, s| s.last_recv_time.lock().unwrap().elapsed() < SESSION_TIMEOUT); + // last_recv_time is a mutex variable shared with connection, can be updated lively. if let Some(session) = session { if session.name == self.lr.my_name && session.session_id == self.lr.session_id @@ -2179,6 +2184,10 @@ impl Connection { } async fn on_close(&mut self, reason: &str, lock: bool) { + if self.closed { + return; + } + self.closed = true; log::info!("#{} Connection closed: {}", self.inner.id(), reason); if lock && self.lock_after_session_end && self.keyboard { #[cfg(not(any(target_os = "android", target_os = "ios")))] diff --git a/src/server/video_service.rs b/src/server/video_service.rs index ada41e266..9478a19f0 100644 --- a/src/server/video_service.rs +++ b/src/server/video_service.rs @@ -484,7 +484,6 @@ fn check_get_displays_changed_msg() -> Option { let displays = check_displays_new()?; let (current, displays) = get_displays_2(&displays); let mut pi = PeerInfo { - conn_id: crate::SYNC_PEER_INFO_DISPLAYS, ..Default::default() }; pi.displays = displays.clone(); diff --git a/src/tray.rs b/src/tray.rs index 23b7e9b0c..a3926d3ad 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -61,7 +61,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> { let mut docker_hiden = false; let open_func = move || { - #[cfg(not(feature = "flutter"))] + if cfg!(not(feature = "flutter")) { crate::run_me::<&str>(vec![]).ok(); return; diff --git a/src/ui_session_interface.rs b/src/ui_session_interface.rs index e821b71dd..79544939e 100644 --- a/src/ui_session_interface.rs +++ b/src/ui_session_interface.rs @@ -49,7 +49,7 @@ const CHANGE_RESOLUTION_VALID_TIMEOUT_SECS: u64 = 15; #[derive(Clone, Default)] pub struct Session { - pub session_id: SessionID, + pub session_id: SessionID, // different from the one in LoginConfigHandler, used for flutter UI message pass pub id: String, // peer id pub password: String, pub args: Vec, @@ -312,8 +312,7 @@ impl Session { } pub fn get_audit_server(&self, typ: String) -> String { - if self.lc.read().unwrap().conn_id <= 0 - || LocalConfig::get_option("access_token").is_empty() + if LocalConfig::get_option("access_token").is_empty() { return "".to_owned(); } @@ -327,9 +326,9 @@ impl Session { pub fn send_note(&self, note: String) { let url = self.get_audit_server("conn".to_string()); let id = self.id.clone(); - let conn_id = self.lc.read().unwrap().conn_id; + let session_id = self.lc.read().unwrap().session_id; std::thread::spawn(move || { - send_note(url, id, conn_id, note); + send_note(url, id, session_id, note); }); } @@ -1347,7 +1346,7 @@ async fn start_one_port_forward( } #[tokio::main(flavor = "current_thread")] -async fn send_note(url: String, id: String, conn_id: i32, note: String) { - let body = serde_json::json!({ "id": id, "conn_id": conn_id, "note": note }); +async fn send_note(url: String, id: String, sid: u64, note: String) { + let body = serde_json::json!({ "id": id, "session_id": sid, "note": note }); allow_err!(crate::post_request(url, body.to_string(), "").await); }