Merge pull request #5891 from fufesou/refact/flutter_sessions_lock

refact, flutter sessions lock
This commit is contained in:
RustDesk 2023-10-03 13:19:07 +08:00 committed by GitHub
commit 03c0111017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 188 additions and 170 deletions

View File

@ -75,15 +75,15 @@ impl Interface for Session {
} }
} }
fn handle_login_error(&mut self, err: &str) -> bool { fn handle_login_error(&self, err: &str) -> bool {
handle_login_error(self.lc.clone(), err, self) handle_login_error(self.lc.clone(), err, self)
} }
fn handle_peer_info(&mut self, pi: PeerInfo) { fn handle_peer_info(&self, pi: PeerInfo) {
self.lc.write().unwrap().handle_peer_info(&pi); self.lc.write().unwrap().handle_peer_info(&pi);
} }
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream) { async fn handle_hash(&self, pass: &str, hash: Hash, peer: &mut Stream) {
log::info!( log::info!(
"password={}", "password={}",
hbb_common::password_security::temporary_password() hbb_common::password_security::temporary_password()
@ -92,7 +92,7 @@ impl Interface for Session {
} }
async fn handle_login_from_ui( async fn handle_login_from_ui(
&mut self, &self,
os_username: String, os_username: String,
os_password: String, os_password: String,
password: String, password: String,
@ -110,7 +110,7 @@ impl Interface for Session {
.await; .await;
} }
async fn handle_test_delay(&mut self, t: TestDelay, peer: &mut Stream) { async fn handle_test_delay(&self, t: TestDelay, peer: &mut Stream) {
handle_test_delay(t, peer).await; handle_test_delay(t, peer).await;
} }

View File

@ -677,7 +677,7 @@ impl Client {
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
fn try_stop_clipboard(_self_uuid: &uuid::Uuid) { fn try_stop_clipboard(_self_uuid: &uuid::Uuid) {
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if crate::flutter::other_sessions_running(_self_uuid) { if crate::flutter::sessions::other_sessions_running(_self_uuid) {
return; return;
} }
TEXT_CLIPBOARD_STATE.lock().unwrap().running = false; TEXT_CLIPBOARD_STATE.lock().unwrap().running = false;
@ -2417,21 +2417,21 @@ pub trait Interface: Send + Clone + 'static + Sized {
/// Send message data to remote peer. /// Send message data to remote peer.
fn send(&self, data: Data); fn send(&self, data: Data);
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str); fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str);
fn handle_login_error(&mut self, err: &str) -> bool; fn handle_login_error(&self, err: &str) -> bool;
fn handle_peer_info(&mut self, pi: PeerInfo); fn handle_peer_info(&self, pi: PeerInfo);
fn on_error(&self, err: &str) { fn on_error(&self, err: &str) {
self.msgbox("error", "Error", err, ""); self.msgbox("error", "Error", err, "");
} }
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream); async fn handle_hash(&self, pass: &str, hash: Hash, peer: &mut Stream);
async fn handle_login_from_ui( async fn handle_login_from_ui(
&mut self, &self,
os_username: String, os_username: String,
os_password: String, os_password: String,
password: String, password: String,
remember: bool, remember: bool,
peer: &mut Stream, peer: &mut Stream,
); );
async fn handle_test_delay(&mut self, t: TestDelay, peer: &mut Stream); async fn handle_test_delay(&self, t: TestDelay, peer: &mut Stream);
fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>>; fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>>;

View File

@ -816,7 +816,7 @@ impl<T: InvokeUiSession> Remote<T> {
job: &fs::TransferJob, job: &fs::TransferJob,
elapsed: i32, elapsed: i32,
last_update_jobs_status: &mut (Instant, HashMap<i32, u64>), last_update_jobs_status: &mut (Instant, HashMap<i32, u64>),
handler: &mut Session<T>, handler: &Session<T>,
) { ) {
if elapsed <= 0 { if elapsed <= 0 {
return; return;
@ -843,7 +843,7 @@ impl<T: InvokeUiSession> Remote<T> {
job, job,
elapsed, elapsed,
&mut self.last_update_jobs_status, &mut self.last_update_jobs_status,
&mut self.handler, &self.handler,
); );
} }
for job in self.write_jobs.iter() { for job in self.write_jobs.iter() {

View File

@ -42,9 +42,10 @@ pub(crate) const APP_TYPE_CM: &str = "main";
// pub(crate) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer"; // pub(crate) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer";
// pub(crate) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward"; // pub(crate) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward";
pub type FlutterSession = Arc<Session<FlutterHandler>>;
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub(crate) static ref CUR_SESSION_ID: RwLock<SessionID> = Default::default(); pub(crate) static ref CUR_SESSION_ID: RwLock<SessionID> = Default::default();
pub(crate) static ref SESSIONS: RwLock<HashMap<SessionID, Session<FlutterHandler>>> = Default::default();
static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel
} }
@ -287,7 +288,7 @@ impl FlutterHandler {
) )
} }
pub(crate) fn close_event_stream(&mut self) { pub(crate) fn close_event_stream(&self) {
let mut stream_lock = self.event_stream.write().unwrap(); let mut stream_lock = self.event_stream.write().unwrap();
if let Some(stream) = &*stream_lock { if let Some(stream) = &*stream_lock {
stream.add(EventToUI::Event("close".to_owned())); stream.add(EventToUI::Event("close".to_owned()));
@ -337,13 +338,13 @@ impl FlutterHandler {
#[inline] #[inline]
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
pub fn register_texture(&mut self, ptr: usize) { pub fn register_texture(&self, ptr: usize) {
*self.renderer.read().unwrap().ptr.write().unwrap() = ptr; *self.renderer.read().unwrap().ptr.write().unwrap() = ptr;
} }
#[inline] #[inline]
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
pub fn set_size(&mut self, width: usize, height: usize) { pub fn set_size(&self, width: usize, height: usize) {
*self.notify_rendered.write().unwrap() = false; *self.notify_rendered.write().unwrap() = false;
self.renderer.write().unwrap().set_size(width, height); self.renderer.write().unwrap().set_size(width, height);
} }
@ -731,7 +732,7 @@ pub fn session_add(
switch_uuid: &str, switch_uuid: &str,
force_relay: bool, force_relay: bool,
password: String, password: String,
) -> ResultType<Session<FlutterHandler>> { ) -> ResultType<FlutterSession> {
LocalConfig::set_remote_id(&id); LocalConfig::set_remote_id(&id);
let session: Session<FlutterHandler> = Session { let session: Session<FlutterHandler> = Session {
@ -768,11 +769,8 @@ pub fn session_add(
.unwrap() .unwrap()
.initialize(id.to_owned(), conn_type, switch_uuid, force_relay); .initialize(id.to_owned(), conn_type, switch_uuid, force_relay);
if let Some(same_id_session) = SESSIONS let session = Arc::new(session.clone());
.write() if let Some(same_id_session) = sessions::add_session(session_id.to_owned(), session.clone()) {
.unwrap()
.insert(session_id.to_owned(), session.clone())
{
log::error!("Should not happen"); log::error!("Should not happen");
same_id_session.close(); same_id_session.close();
} }
@ -791,7 +789,7 @@ pub fn session_start_(
id: &str, id: &str,
event_stream: StreamSink<EventToUI>, event_stream: StreamSink<EventToUI>,
) -> ResultType<()> { ) -> ResultType<()> {
if let Some(session) = SESSIONS.write().unwrap().get_mut(session_id) { if let Some(session) = sessions::get_session(session_id) {
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
log::info!( log::info!(
"Session {} start, render by flutter texture rgba plugin", "Session {} start, render by flutter texture rgba plugin",
@ -803,7 +801,7 @@ pub fn session_start_(
session.close_event_stream(); session.close_event_stream();
*session.event_stream.write().unwrap() = Some(event_stream); *session.event_stream.write().unwrap() = Some(event_stream);
if !is_pre_added { if !is_pre_added {
let session = session.clone(); let session = (*session).clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let round = session.connection_round_state.lock().unwrap().new_round(); let round = session.connection_round_state.lock().unwrap().new_round();
io_loop(session, round); io_loop(session, round);
@ -817,29 +815,15 @@ pub fn session_start_(
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn update_text_clipboard_required() { pub fn update_text_clipboard_required() {
let is_required = SESSIONS let is_required = sessions::get_sessions()
.read()
.unwrap()
.iter() .iter()
.any(|(_id, session)| session.is_text_clipboard_required()); .any(|session| session.is_text_clipboard_required());
Client::set_is_text_clipboard_required(is_required); Client::set_is_text_clipboard_required(is_required);
} }
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn other_sessions_running(session_id: &SessionID) -> bool {
SESSIONS
.read()
.unwrap()
.keys()
.filter(|k| *k != session_id)
.count()
!= 0
}
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn send_text_clipboard_msg(msg: Message) { pub fn send_text_clipboard_msg(msg: Message) {
for (_id, session) in SESSIONS.read().unwrap().iter() { for session in sessions::get_sessions() {
if session.is_text_clipboard_required() { if session.is_text_clipboard_required() {
session.send(Data::Message(msg.clone())); session.send(Data::Message(msg.clone()));
} }
@ -1052,7 +1036,7 @@ fn char_to_session_id(c: *const char) -> ResultType<SessionID> {
pub fn session_get_rgba_size(_session_id: SessionID) -> usize { pub fn session_get_rgba_size(_session_id: SessionID) -> usize {
#[cfg(not(feature = "flutter_texture_render"))] #[cfg(not(feature = "flutter_texture_render"))]
if let Some(session) = SESSIONS.read().unwrap().get(&_session_id) { if let Some(session) = sessions::get_session(&_session_id) {
return session.rgba.read().unwrap().len(); return session.rgba.read().unwrap().len();
} }
0 0
@ -1061,7 +1045,7 @@ pub fn session_get_rgba_size(_session_id: SessionID) -> usize {
#[no_mangle] #[no_mangle]
pub extern "C" fn session_get_rgba(session_uuid_str: *const char) -> *const u8 { pub extern "C" fn session_get_rgba(session_uuid_str: *const char) -> *const u8 {
if let Ok(session_id) = char_to_session_id(session_uuid_str) { if let Ok(session_id) = char_to_session_id(session_uuid_str) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
return session.get_rgba(); return session.get_rgba();
} }
} }
@ -1070,7 +1054,7 @@ pub extern "C" fn session_get_rgba(session_uuid_str: *const char) -> *const u8 {
} }
pub fn session_next_rgba(session_id: SessionID) { pub fn session_next_rgba(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
return session.next_rgba(); return session.next_rgba();
} }
} }
@ -1078,7 +1062,7 @@ pub fn session_next_rgba(session_id: SessionID) {
#[inline] #[inline]
pub fn session_register_texture(_session_id: SessionID, _ptr: usize) { pub fn session_register_texture(_session_id: SessionID, _ptr: usize) {
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
if let Some(session) = SESSIONS.write().unwrap().get_mut(&_session_id) { if let Some(session) = sessions::get_session(&_session_id) {
session.register_texture(_ptr); session.register_texture(_ptr);
return; return;
} }
@ -1090,11 +1074,7 @@ pub fn push_session_event(
name: &str, name: &str,
event: Vec<(&str, &str)>, event: Vec<(&str, &str)>,
) -> Option<bool> { ) -> Option<bool> {
SESSIONS sessions::get_session(session_id)?.push_event(name, event)
.read()
.unwrap()
.get(session_id)?
.push_event(name, event)
} }
#[inline] #[inline]
@ -1143,7 +1123,7 @@ fn session_send_touch_scale(
) { ) {
match v.get("v").and_then(|s| s.as_i64()) { match v.get("v").and_then(|s| s.as_i64()) {
Some(scale) => { Some(scale) => {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.send_touch_scale(scale as _, alt, ctrl, shift, command); session.send_touch_scale(scale as _, alt, ctrl, shift, command);
} }
} }
@ -1167,7 +1147,7 @@ fn session_send_touch_pan(
v.get("y").and_then(|y| y.as_i64()), v.get("y").and_then(|y| y.as_i64()),
) { ) {
(Some(x), Some(y)) => { (Some(x), Some(y)) => {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session session
.send_touch_pan_event(pan_event, x as _, y as _, alt, ctrl, shift, command); .send_touch_pan_event(pan_event, x as _, y as _, alt, ctrl, shift, command);
} }
@ -1216,3 +1196,60 @@ pub fn session_send_pointer(session_id: SessionID, msg: String) {
pub enum SessionHook { pub enum SessionHook {
OnSessionRgba(fn(String, &mut scrap::ImageRgb)), OnSessionRgba(fn(String, &mut scrap::ImageRgb)),
} }
#[inline]
pub fn get_cur_session() -> Option<FlutterSession> {
sessions::get_session(&*CUR_SESSION_ID.read().unwrap())
}
// sessions mod is used to avoid the big lock of sessions' map.
pub mod sessions {
use super::*;
lazy_static::lazy_static! {
static ref SESSIONS: RwLock<HashMap<SessionID, FlutterSession>> = Default::default();
}
#[inline]
pub fn add_session(session_id: SessionID, session: FlutterSession) -> Option<FlutterSession> {
SESSIONS.write().unwrap().insert(session_id, session)
}
#[inline]
pub fn remove_session(session_id: &SessionID) -> Option<FlutterSession> {
SESSIONS.write().unwrap().remove(session_id)
}
#[inline]
pub fn get_session(session_id: &SessionID) -> Option<FlutterSession> {
SESSIONS.read().unwrap().get(session_id).cloned()
}
#[inline]
pub fn get_sessions() -> Vec<FlutterSession> {
SESSIONS.read().unwrap().values().cloned().collect()
}
#[inline]
pub fn get_session_by_peer_id(peer_id: &str) -> Option<FlutterSession> {
SESSIONS
.read()
.unwrap()
.values()
.find(|session| session.id == peer_id)
.map(|s| s.clone())
.clone()
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn other_sessions_running(session_id: &SessionID) -> bool {
SESSIONS
.read()
.unwrap()
.keys()
.filter(|k| *k != session_id)
.count()
!= 0
}
}

View File

@ -4,8 +4,7 @@ use crate::{
client::file_trait::FileManager, client::file_trait::FileManager,
common::is_keyboard_mode_supported, common::is_keyboard_mode_supported,
common::make_fd_to_json, common::make_fd_to_json,
flutter::{self, SESSIONS}, flutter::{self, session_add, session_start_, sessions},
flutter::{session_add, session_start_},
input::*, input::*,
ui_interface::{self, *}, ui_interface::{self, *},
}; };
@ -113,7 +112,7 @@ pub fn session_start(
} }
pub fn session_get_remember(session_id: SessionID) -> Option<bool> { pub fn session_get_remember(session_id: SessionID) -> Option<bool> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_remember()) Some(session.get_remember())
} else { } else {
None None
@ -121,7 +120,7 @@ pub fn session_get_remember(session_id: SessionID) -> Option<bool> {
} }
pub fn session_get_toggle_option(session_id: SessionID, arg: String) -> Option<bool> { pub fn session_get_toggle_option(session_id: SessionID, arg: String) -> Option<bool> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_toggle_option(arg)) Some(session.get_toggle_option(arg))
} else { } else {
None None
@ -134,7 +133,7 @@ pub fn session_get_toggle_option_sync(session_id: SessionID, arg: String) -> Syn
} }
pub fn session_get_option(session_id: SessionID, arg: String) -> Option<String> { pub fn session_get_option(session_id: SessionID, arg: String) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_option(arg)) Some(session.get_option(arg))
} else { } else {
None None
@ -148,55 +147,55 @@ pub fn session_login(
password: String, password: String,
remember: bool, remember: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.login(os_username, os_password, password, remember); session.login(os_username, os_password, password, remember);
} }
} }
pub fn session_close(session_id: SessionID) { pub fn session_close(session_id: SessionID) {
if let Some(mut session) = SESSIONS.write().unwrap().remove(&session_id) { if let Some(session) = sessions::remove_session(&session_id) {
session.close_event_stream(); session.close_event_stream();
session.close(); session.close();
} }
} }
pub fn session_refresh(session_id: SessionID) { pub fn session_refresh(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.refresh_video(); session.refresh_video();
} }
} }
pub fn session_record_screen(session_id: SessionID, start: bool, width: usize, height: usize) { pub fn session_record_screen(session_id: SessionID, start: bool, width: usize, height: usize) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.record_screen(start, width as _, height as _); session.record_screen(start, width as _, height as _);
} }
} }
pub fn session_record_status(session_id: SessionID, status: bool) { pub fn session_record_status(session_id: SessionID, status: bool) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.record_status(status); session.record_status(status);
} }
} }
pub fn session_reconnect(session_id: SessionID, force_relay: bool) { pub fn session_reconnect(session_id: SessionID, force_relay: bool) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.reconnect(force_relay); session.reconnect(force_relay);
} }
} }
pub fn session_toggle_option(session_id: SessionID, value: String) { pub fn session_toggle_option(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
log::warn!("toggle option {}", &value); log::warn!("toggle option {}", &value);
session.toggle_option(value.clone()); session.toggle_option(value.clone());
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
if SESSIONS.read().unwrap().get(&session_id).is_some() && value == "disable-clipboard" { if sessions::get_session(&session_id).is_some() && value == "disable-clipboard" {
crate::flutter::update_text_clipboard_required(); crate::flutter::update_text_clipboard_required();
} }
} }
pub fn session_get_flutter_option(session_id: SessionID, k: String) -> Option<String> { pub fn session_get_flutter_option(session_id: SessionID, k: String) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_flutter_option(k)) Some(session.get_flutter_option(k))
} else { } else {
None None
@ -204,13 +203,13 @@ pub fn session_get_flutter_option(session_id: SessionID, k: String) -> Option<St
} }
pub fn session_set_flutter_option(session_id: SessionID, k: String, v: String) { pub fn session_set_flutter_option(session_id: SessionID, k: String, v: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_flutter_option(k, v); session.save_flutter_option(k, v);
} }
} }
pub fn session_get_flutter_option_by_peer_id(id: String, k: String) -> Option<String> { pub fn session_get_flutter_option_by_peer_id(id: String, k: String) -> Option<String> {
if let Some((_, session)) = SESSIONS.read().unwrap().iter().find(|(_, s)| s.id == id) { if let Some(session) = sessions::get_session_by_peer_id(&id) {
Some(session.get_flutter_option(k)) Some(session.get_flutter_option(k))
} else { } else {
None None
@ -239,7 +238,7 @@ pub fn set_local_kb_layout_type(kb_layout_type: String) {
} }
pub fn session_get_view_style(session_id: SessionID) -> Option<String> { pub fn session_get_view_style(session_id: SessionID) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_view_style()) Some(session.get_view_style())
} else { } else {
None None
@ -247,13 +246,13 @@ pub fn session_get_view_style(session_id: SessionID) -> Option<String> {
} }
pub fn session_set_view_style(session_id: SessionID, value: String) { pub fn session_set_view_style(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_view_style(value); session.save_view_style(value);
} }
} }
pub fn session_get_scroll_style(session_id: SessionID) -> Option<String> { pub fn session_get_scroll_style(session_id: SessionID) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_scroll_style()) Some(session.get_scroll_style())
} else { } else {
None None
@ -261,13 +260,13 @@ pub fn session_get_scroll_style(session_id: SessionID) -> Option<String> {
} }
pub fn session_set_scroll_style(session_id: SessionID, value: String) { pub fn session_set_scroll_style(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_scroll_style(value); session.save_scroll_style(value);
} }
} }
pub fn session_get_image_quality(session_id: SessionID) -> Option<String> { pub fn session_get_image_quality(session_id: SessionID) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_image_quality()) Some(session.get_image_quality())
} else { } else {
None None
@ -275,13 +274,13 @@ pub fn session_get_image_quality(session_id: SessionID) -> Option<String> {
} }
pub fn session_set_image_quality(session_id: SessionID, value: String) { pub fn session_set_image_quality(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_image_quality(value); session.save_image_quality(value);
} }
} }
pub fn session_get_keyboard_mode(session_id: SessionID) -> Option<String> { pub fn session_get_keyboard_mode(session_id: SessionID) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_keyboard_mode()) Some(session.get_keyboard_mode())
} else { } else {
None None
@ -290,7 +289,7 @@ pub fn session_get_keyboard_mode(session_id: SessionID) -> Option<String> {
pub fn session_set_keyboard_mode(session_id: SessionID, value: String) { pub fn session_set_keyboard_mode(session_id: SessionID, value: String) {
let mut _mode_updated = false; let mut _mode_updated = false;
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_keyboard_mode(value.clone()); session.save_keyboard_mode(value.clone());
_mode_updated = true; _mode_updated = true;
} }
@ -301,7 +300,7 @@ pub fn session_set_keyboard_mode(session_id: SessionID, value: String) {
} }
pub fn session_get_reverse_mouse_wheel(session_id: SessionID) -> Option<String> { pub fn session_get_reverse_mouse_wheel(session_id: SessionID) -> Option<String> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_reverse_mouse_wheel()) Some(session.get_reverse_mouse_wheel())
} else { } else {
None None
@ -309,13 +308,13 @@ pub fn session_get_reverse_mouse_wheel(session_id: SessionID) -> Option<String>
} }
pub fn session_set_reverse_mouse_wheel(session_id: SessionID, value: String) { pub fn session_set_reverse_mouse_wheel(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_reverse_mouse_wheel(value); session.save_reverse_mouse_wheel(value);
} }
} }
pub fn session_get_custom_image_quality(session_id: SessionID) -> Option<Vec<i32>> { pub fn session_get_custom_image_quality(session_id: SessionID) -> Option<Vec<i32>> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_custom_image_quality()) Some(session.get_custom_image_quality())
} else { } else {
None None
@ -323,7 +322,7 @@ pub fn session_get_custom_image_quality(session_id: SessionID) -> Option<Vec<i32
} }
pub fn session_is_keyboard_mode_supported(session_id: SessionID, mode: String) -> SyncReturn<bool> { pub fn session_is_keyboard_mode_supported(session_id: SessionID, mode: String) -> SyncReturn<bool> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
if let Ok(mode) = KeyboardMode::from_str(&mode[..]) { if let Ok(mode) = KeyboardMode::from_str(&mode[..]) {
SyncReturn(is_keyboard_mode_supported( SyncReturn(is_keyboard_mode_supported(
&mode, &mode,
@ -338,31 +337,31 @@ pub fn session_is_keyboard_mode_supported(session_id: SessionID, mode: String) -
} }
pub fn session_set_custom_image_quality(session_id: SessionID, value: i32) { pub fn session_set_custom_image_quality(session_id: SessionID, value: i32) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.save_custom_image_quality(value); session.save_custom_image_quality(value);
} }
} }
pub fn session_set_custom_fps(session_id: SessionID, fps: i32) { pub fn session_set_custom_fps(session_id: SessionID, fps: i32) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.set_custom_fps(fps); session.set_custom_fps(fps);
} }
} }
pub fn session_lock_screen(session_id: SessionID) { pub fn session_lock_screen(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.lock_screen(); session.lock_screen();
} }
} }
pub fn session_ctrl_alt_del(session_id: SessionID) { pub fn session_ctrl_alt_del(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.ctrl_alt_del(); session.ctrl_alt_del();
} }
} }
pub fn session_switch_display(session_id: SessionID, value: i32) { pub fn session_switch_display(session_id: SessionID, value: i32) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.switch_display(value); session.switch_display(value);
} }
} }
@ -375,7 +374,7 @@ pub fn session_handle_flutter_key_event(
lock_modes: i32, lock_modes: i32,
down_or_up: bool, down_or_up: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
let keyboard_mode = session.get_keyboard_mode(); let keyboard_mode = session.get_keyboard_mode();
session.handle_flutter_key_event( session.handle_flutter_key_event(
&keyboard_mode, &keyboard_mode,
@ -396,7 +395,7 @@ pub fn session_handle_flutter_key_event(
// This will cause the keyboard input to take no effect. // This will cause the keyboard input to take no effect.
pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> { pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> {
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Some(session) = SESSIONS.read().unwrap().get(&_session_id) { if let Some(session) = sessions::get_session(&_session_id) {
let keyboard_mode = session.get_keyboard_mode(); let keyboard_mode = session.get_keyboard_mode();
if _enter { if _enter {
set_cur_session_id_(_session_id, &keyboard_mode); set_cur_session_id_(_session_id, &keyboard_mode);
@ -418,14 +417,14 @@ pub fn session_input_key(
shift: bool, shift: bool,
command: bool, command: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
// #[cfg(any(target_os = "android", target_os = "ios"))] // #[cfg(any(target_os = "android", target_os = "ios"))]
session.input_key(&name, down, press, alt, ctrl, shift, command); session.input_key(&name, down, press, alt, ctrl, shift, command);
} }
} }
pub fn session_input_string(session_id: SessionID, value: String) { pub fn session_input_string(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
// #[cfg(any(target_os = "android", target_os = "ios"))] // #[cfg(any(target_os = "android", target_os = "ios"))]
session.input_string(&value); session.input_string(&value);
} }
@ -433,33 +432,33 @@ pub fn session_input_string(session_id: SessionID, value: String) {
// chat_client_mode // chat_client_mode
pub fn session_send_chat(session_id: SessionID, text: String) { pub fn session_send_chat(session_id: SessionID, text: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.send_chat(text); session.send_chat(text);
} }
} }
pub fn session_peer_option(session_id: SessionID, name: String, value: String) { pub fn session_peer_option(session_id: SessionID, name: String, value: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.set_option(name, value); session.set_option(name, value);
} }
} }
pub fn session_get_peer_option(session_id: SessionID, name: String) -> String { pub fn session_get_peer_option(session_id: SessionID, name: String) -> String {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
return session.get_option(name); return session.get_option(name);
} }
"".to_string() "".to_string()
} }
pub fn session_input_os_password(session_id: SessionID, value: String) { pub fn session_input_os_password(session_id: SessionID, value: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.input_os_password(value, true); session.input_os_password(value, true);
} }
} }
// File Action // File Action
pub fn session_read_remote_dir(session_id: SessionID, path: String, include_hidden: bool) { pub fn session_read_remote_dir(session_id: SessionID, path: String, include_hidden: bool) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.read_remote_dir(path, include_hidden); session.read_remote_dir(path, include_hidden);
} }
} }
@ -473,7 +472,7 @@ pub fn session_send_files(
include_hidden: bool, include_hidden: bool,
is_remote: bool, is_remote: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.send_files(act_id, path, to, file_num, include_hidden, is_remote); session.send_files(act_id, path, to, file_num, include_hidden, is_remote);
} }
} }
@ -486,7 +485,7 @@ pub fn session_set_confirm_override_file(
remember: bool, remember: bool,
is_upload: bool, is_upload: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.set_confirm_override_file(act_id, file_num, need_override, remember, is_upload); session.set_confirm_override_file(act_id, file_num, need_override, remember, is_upload);
} }
} }
@ -498,7 +497,7 @@ pub fn session_remove_file(
file_num: i32, file_num: i32,
is_remote: bool, is_remote: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.remove_file(act_id, path, file_num, is_remote); session.remove_file(act_id, path, file_num, is_remote);
} }
} }
@ -510,7 +509,7 @@ pub fn session_read_dir_recursive(
is_remote: bool, is_remote: bool,
show_hidden: bool, show_hidden: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.remove_dir_all(act_id, path, is_remote, show_hidden); session.remove_dir_all(act_id, path, is_remote, show_hidden);
} }
} }
@ -521,19 +520,19 @@ pub fn session_remove_all_empty_dirs(
path: String, path: String,
is_remote: bool, is_remote: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.remove_dir(act_id, path, is_remote); session.remove_dir(act_id, path, is_remote);
} }
} }
pub fn session_cancel_job(session_id: SessionID, act_id: i32) { pub fn session_cancel_job(session_id: SessionID, act_id: i32) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.cancel_job(act_id); session.cancel_job(act_id);
} }
} }
pub fn session_create_dir(session_id: SessionID, act_id: i32, path: String, is_remote: bool) { pub fn session_create_dir(session_id: SessionID, act_id: i32, path: String, is_remote: bool) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.create_dir(act_id, path, is_remote); session.create_dir(act_id, path, is_remote);
} }
} }
@ -550,14 +549,14 @@ pub fn session_read_local_dir_sync(
} }
pub fn session_get_platform(session_id: SessionID, is_remote: bool) -> String { pub fn session_get_platform(session_id: SessionID, is_remote: bool) -> String {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
return session.get_platform(is_remote); return session.get_platform(is_remote);
} }
"".to_string() "".to_string()
} }
pub fn session_load_last_transfer_jobs(session_id: SessionID) { pub fn session_load_last_transfer_jobs(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
return session.load_last_jobs(); return session.load_last_jobs();
} else { } else {
// a tip for flutter dev // a tip for flutter dev
@ -577,44 +576,44 @@ pub fn session_add_job(
include_hidden: bool, include_hidden: bool,
is_remote: bool, is_remote: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.add_job(act_id, path, to, file_num, include_hidden, is_remote); session.add_job(act_id, path, to, file_num, include_hidden, is_remote);
} }
} }
pub fn session_resume_job(session_id: SessionID, act_id: i32, is_remote: bool) { pub fn session_resume_job(session_id: SessionID, act_id: i32, is_remote: bool) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.resume_job(act_id, is_remote); session.resume_job(act_id, is_remote);
} }
} }
pub fn session_elevate_direct(session_id: SessionID) { pub fn session_elevate_direct(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.elevate_direct(); session.elevate_direct();
} }
} }
pub fn session_elevate_with_logon(session_id: SessionID, username: String, password: String) { pub fn session_elevate_with_logon(session_id: SessionID, username: String, password: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.elevate_with_logon(username, password); session.elevate_with_logon(username, password);
} }
} }
pub fn session_switch_sides(session_id: SessionID) { pub fn session_switch_sides(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.switch_sides(); session.switch_sides();
} }
} }
pub fn session_change_resolution(session_id: SessionID, display: i32, width: i32, height: i32) { pub fn session_change_resolution(session_id: SessionID, display: i32, width: i32, height: i32) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.change_resolution(display, width, height); session.change_resolution(display, width, height);
} }
} }
pub fn session_set_size(_session_id: SessionID, _width: usize, _height: usize) { pub fn session_set_size(_session_id: SessionID, _width: usize, _height: usize) {
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
if let Some(session) = SESSIONS.write().unwrap().get_mut(&_session_id) { if let Some(session) = sessions::get_session(&_session_id) {
session.set_size(_width, _height); session.set_size(_width, _height);
} }
} }
@ -1030,31 +1029,31 @@ pub fn session_add_port_forward(
remote_host: String, remote_host: String,
remote_port: i32, remote_port: i32,
) { ) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.add_port_forward(local_port, remote_host, remote_port); session.add_port_forward(local_port, remote_host, remote_port);
} }
} }
pub fn session_remove_port_forward(session_id: SessionID, local_port: i32) { pub fn session_remove_port_forward(session_id: SessionID, local_port: i32) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.remove_port_forward(local_port); session.remove_port_forward(local_port);
} }
} }
pub fn session_new_rdp(session_id: SessionID) { pub fn session_new_rdp(session_id: SessionID) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.new_rdp(); session.new_rdp();
} }
} }
pub fn session_request_voice_call(session_id: SessionID) { pub fn session_request_voice_call(session_id: SessionID) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.request_voice_call(); session.request_voice_call();
} }
} }
pub fn session_close_voice_call(session_id: SessionID) { pub fn session_close_voice_call(session_id: SessionID) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.close_voice_call(); session.close_voice_call();
} }
} }
@ -1239,20 +1238,20 @@ pub fn session_send_mouse(session_id: SessionID, msg: String) {
_ => 0, _ => 0,
} << 3; } << 3;
} }
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.send_mouse(mask, x, y, alt, ctrl, shift, command); session.send_mouse(mask, x, y, alt, ctrl, shift, command);
} }
} }
} }
pub fn session_restart_remote_device(session_id: SessionID) { pub fn session_restart_remote_device(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.restart_remote_device(); session.restart_remote_device();
} }
} }
pub fn session_get_audit_server_sync(session_id: SessionID, typ: String) -> SyncReturn<String> { pub fn session_get_audit_server_sync(session_id: SessionID, typ: String) -> SyncReturn<String> {
let res = if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { let res = if let Some(session) = sessions::get_session(&session_id) {
session.get_audit_server(typ) session.get_audit_server(typ)
} else { } else {
"".to_owned() "".to_owned()
@ -1261,13 +1260,13 @@ pub fn session_get_audit_server_sync(session_id: SessionID, typ: String) -> Sync
} }
pub fn session_send_note(session_id: SessionID, note: String) { pub fn session_send_note(session_id: SessionID, note: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.send_note(note) session.send_note(note)
} }
} }
pub fn session_alternative_codecs(session_id: SessionID) -> String { pub fn session_alternative_codecs(session_id: SessionID) -> String {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
let (vp8, av1, h264, h265) = session.alternative_codecs(); let (vp8, av1, h264, h265) = session.alternative_codecs();
let msg = HashMap::from([("vp8", vp8), ("av1", av1), ("h264", h264), ("h265", h265)]); let msg = HashMap::from([("vp8", vp8), ("av1", av1), ("h264", h264), ("h265", h265)]);
serde_json::ser::to_string(&msg).unwrap_or("".to_owned()) serde_json::ser::to_string(&msg).unwrap_or("".to_owned())
@ -1277,13 +1276,13 @@ pub fn session_alternative_codecs(session_id: SessionID) -> String {
} }
pub fn session_change_prefer_codec(session_id: SessionID) { pub fn session_change_prefer_codec(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.change_prefer_codec(); session.change_prefer_codec();
} }
} }
pub fn session_on_waiting_for_image_dialog_show(session_id: SessionID) { pub fn session_on_waiting_for_image_dialog_show(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
session.ui_handler.on_waiting_for_image_dialog_show(); session.ui_handler.on_waiting_for_image_dialog_show();
} }
} }
@ -1523,7 +1522,7 @@ pub fn main_update_me() -> SyncReturn<bool> {
} }
pub fn set_cur_session_id(session_id: SessionID) { pub fn set_cur_session_id(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = sessions::get_session(&session_id) {
set_cur_session_id_(session_id, &session.get_keyboard_mode()) set_cur_session_id_(session_id, &session.get_keyboard_mode())
} }
} }

View File

@ -1,5 +1,5 @@
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
use crate::flutter::{CUR_SESSION_ID, SESSIONS}; use crate::flutter;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use crate::platform::windows::{get_char_from_vk, get_unicode_from_vk}; use crate::platform::windows::{get_char_from_vk, get_unicode_from_vk};
#[cfg(not(any(feature = "flutter", feature = "cli")))] #[cfg(not(any(feature = "flutter", feature = "cli")))]
@ -220,11 +220,7 @@ fn get_keyboard_mode() -> String {
return session.get_keyboard_mode(); return session.get_keyboard_mode();
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if let Some(session) = SESSIONS if let Some(session) = flutter::get_cur_session() {
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
return session.get_keyboard_mode(); return session.get_keyboard_mode();
} }
"legacy".to_string() "legacy".to_string()
@ -579,11 +575,7 @@ pub fn send_key_event(key_event: &KeyEvent) {
session.send_key_event(key_event); session.send_key_event(key_event);
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if let Some(session) = SESSIONS if let Some(session) = flutter::get_cur_session() {
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
session.send_key_event(key_event); session.send_key_event(key_event);
} }
} }
@ -594,11 +586,7 @@ pub fn get_peer_platform() -> String {
return session.peer_platform(); return session.peer_platform();
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if let Some(session) = SESSIONS if let Some(session) = flutter::get_cur_session() {
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
return session.peer_platform(); return session.peer_platform();
} }
"Windows".to_string() "Windows".to_string()

View File

@ -7,12 +7,7 @@ use std::{
use flutter_rust_bridge::StreamSink; use flutter_rust_bridge::StreamSink;
use crate::{ use crate::{define_method_prefix, flutter_ffi::EventToUI};
define_method_prefix,
flutter::FlutterHandler,
flutter_ffi::EventToUI,
ui_session_interface::{ConnectionState, Session},
};
const MSG_TO_UI_TYPE_SESSION_CREATED: &str = "session_created"; const MSG_TO_UI_TYPE_SESSION_CREATED: &str = "session_created";
@ -30,7 +25,7 @@ pub type OnSessionRgbaCallback = unsafe extern "C" fn(
#[derive(Default)] #[derive(Default)]
/// Session related handler for librustdesk core. /// Session related handler for librustdesk core.
pub struct PluginNativeSessionHandler { pub struct PluginNativeSessionHandler {
sessions: Arc<RwLock<Vec<Session<FlutterHandler>>>>, sessions: Arc<RwLock<Vec<crate::flutter::FlutterSession>>>,
cbs: Arc<RwLock<HashMap<String, OnSessionRgbaCallback>>>, cbs: Arc<RwLock<HashMap<String, OnSessionRgbaCallback>>>,
} }
@ -63,7 +58,8 @@ impl PluginNativeHandler for PluginNativeSessionHandler {
let sessions = SESSION_HANDLER.sessions.read().unwrap(); let sessions = SESSION_HANDLER.sessions.read().unwrap();
for session in sessions.iter() { for session in sessions.iter() {
if session.id == id { if session.id == id {
let round = session.connection_round_state.lock().unwrap().new_round(); let round =
session.connection_round_state.lock().unwrap().new_round();
crate::ui_session_interface::io_loop(session.clone(), round); crate::ui_session_interface::io_loop(session.clone(), round);
} }
} }

View File

@ -75,7 +75,7 @@ pub async fn listen(
let interface = interface.clone(); let interface = interface.clone();
tokio::spawn(async move { tokio::spawn(async move {
if let Err(err) = run_forward(forward, stream).await { if let Err(err) = run_forward(forward, stream).await {
interface.msgbox("error", "Error", &err.to_string(), ""); interface.msgbox("error", "Error", &err.to_string(), "");
} }
log::info!("connection from {:?} closed", addr); log::info!("connection from {:?} closed", addr);
}); });
@ -121,7 +121,6 @@ async fn connect_and_login(
let (mut stream, direct, _pk) = let (mut stream, direct, _pk) =
Client::start(id, key, token, conn_type, interface.clone()).await?; Client::start(id, key, token, conn_type, interface.clone()).await?;
interface.update_direct(Some(direct)); interface.update_direct(Some(direct));
let mut interface = interface;
let mut buffer = Vec::new(); let mut buffer = Vec::new();
let mut received = false; let mut received = false;
loop { loop {

View File

@ -1,4 +1,7 @@
use crate::input::{MOUSE_BUTTON_LEFT, MOUSE_TYPE_DOWN, MOUSE_TYPE_UP, MOUSE_TYPE_WHEEL}; use crate::input::{MOUSE_BUTTON_LEFT, MOUSE_TYPE_DOWN, MOUSE_TYPE_UP, MOUSE_TYPE_WHEEL};
use async_trait::async_trait;
use bytes::Bytes;
use rdev::{Event, EventType::*, KeyCode};
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
use std::{collections::HashMap, sync::atomic::AtomicBool}; use std::{collections::HashMap, sync::atomic::AtomicBool};
use std::{ use std::{
@ -10,10 +13,6 @@ use std::{
}, },
time::SystemTime, time::SystemTime,
}; };
use async_trait::async_trait;
use bytes::Bytes;
use rdev::{Event, EventType::*, KeyCode};
use uuid::Uuid; use uuid::Uuid;
#[cfg(not(feature = "flutter"))] #[cfg(not(feature = "flutter"))]
@ -232,7 +231,7 @@ impl<T: InvokeUiSession> Session<T> {
} }
} }
pub fn save_keyboard_mode(&mut self, value: String) { pub fn save_keyboard_mode(&self, value: String) {
self.lc.write().unwrap().save_keyboard_mode(value); self.lc.write().unwrap().save_keyboard_mode(value);
} }
@ -240,19 +239,19 @@ impl<T: InvokeUiSession> Session<T> {
self.lc.read().unwrap().reverse_mouse_wheel.clone() self.lc.read().unwrap().reverse_mouse_wheel.clone()
} }
pub fn save_reverse_mouse_wheel(&mut self, value: String) { pub fn save_reverse_mouse_wheel(&self, value: String) {
self.lc.write().unwrap().save_reverse_mouse_wheel(value); self.lc.write().unwrap().save_reverse_mouse_wheel(value);
} }
pub fn save_view_style(&mut self, value: String) { pub fn save_view_style(&self, value: String) {
self.lc.write().unwrap().save_view_style(value); self.lc.write().unwrap().save_view_style(value);
} }
pub fn save_scroll_style(&mut self, value: String) { pub fn save_scroll_style(&self, value: String) {
self.lc.write().unwrap().save_scroll_style(value); self.lc.write().unwrap().save_scroll_style(value);
} }
pub fn save_flutter_option(&mut self, k: String, v: String) { pub fn save_flutter_option(&self, k: String, v: String) {
self.lc.write().unwrap().save_ui_flutter(k, v); self.lc.write().unwrap().save_ui_flutter(k, v);
} }
@ -260,7 +259,7 @@ impl<T: InvokeUiSession> Session<T> {
self.lc.read().unwrap().get_ui_flutter(&k) self.lc.read().unwrap().get_ui_flutter(&k)
} }
pub fn toggle_option(&mut self, name: String) { pub fn toggle_option(&self, name: String) {
let msg = self.lc.write().unwrap().toggle_option(name.clone()); let msg = self.lc.write().unwrap().toggle_option(name.clone());
#[cfg(not(feature = "flutter"))] #[cfg(not(feature = "flutter"))]
if name == "enable-file-transfer" { if name == "enable-file-transfer" {
@ -303,7 +302,7 @@ impl<T: InvokeUiSession> Session<T> {
self.send(Data::Message(msg)); self.send(Data::Message(msg));
} }
pub fn save_custom_image_quality(&mut self, custom_image_quality: i32) { pub fn save_custom_image_quality(&self, custom_image_quality: i32) {
let msg = self let msg = self
.lc .lc
.write() .write()
@ -312,14 +311,14 @@ impl<T: InvokeUiSession> Session<T> {
self.send(Data::Message(msg)); self.send(Data::Message(msg));
} }
pub fn save_image_quality(&mut self, value: String) { pub fn save_image_quality(&self, value: String) {
let msg = self.lc.write().unwrap().save_image_quality(value); let msg = self.lc.write().unwrap().save_image_quality(value);
if let Some(msg) = msg { if let Some(msg) = msg {
self.send(Data::Message(msg)); self.send(Data::Message(msg));
} }
} }
pub fn set_custom_fps(&mut self, custom_fps: i32) { pub fn set_custom_fps(&self, custom_fps: i32) {
let msg = self.lc.write().unwrap().set_custom_fps(custom_fps); let msg = self.lc.write().unwrap().set_custom_fps(custom_fps);
self.send(Data::Message(msg)); self.send(Data::Message(msg));
} }
@ -423,7 +422,7 @@ impl<T: InvokeUiSession> Session<T> {
self.send(Data::RemovePortForward(port)); self.send(Data::RemovePortForward(port));
} }
pub fn add_port_forward(&mut self, port: i32, remote_host: String, remote_port: i32) { pub fn add_port_forward(&self, port: i32, remote_host: String, remote_port: i32) {
let mut config = self.load_config(); let mut config = self.load_config();
if config if config
.port_forwards .port_forwards
@ -901,7 +900,7 @@ impl<T: InvokeUiSession> Session<T> {
let cloned = self.clone(); let cloned = self.clone();
// override only if true // override only if true
if true == force_relay { if true == force_relay {
cloned.lc.write().unwrap().force_relay = true; self.lc.write().unwrap().force_relay = true;
} }
let mut lock = self.thread.lock().unwrap(); let mut lock = self.thread.lock().unwrap();
// No need to join the previous thread, because it will exit automatically. // No need to join the previous thread, because it will exit automatically.
@ -1216,11 +1215,11 @@ impl<T: InvokeUiSession> Interface for Session<T> {
self.ui_handler.msgbox(msgtype, title, text, link, retry); self.ui_handler.msgbox(msgtype, title, text, link, retry);
} }
fn handle_login_error(&mut self, err: &str) -> bool { fn handle_login_error(&self, err: &str) -> bool {
handle_login_error(self.lc.clone(), err, self) handle_login_error(self.lc.clone(), err, self)
} }
fn handle_peer_info(&mut self, mut pi: PeerInfo) { fn handle_peer_info(&self, mut pi: PeerInfo) {
log::debug!("handle_peer_info :{:?}", pi); log::debug!("handle_peer_info :{:?}", pi);
pi.username = self.lc.read().unwrap().get_username(&pi); pi.username = self.lc.read().unwrap().get_username(&pi);
if pi.current_display as usize >= pi.displays.len() { if pi.current_display as usize >= pi.displays.len() {
@ -1282,12 +1281,12 @@ impl<T: InvokeUiSession> Interface for Session<T> {
} }
} }
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream) { async fn handle_hash(&self, pass: &str, hash: Hash, peer: &mut Stream) {
handle_hash(self.lc.clone(), pass, hash, self, peer).await; handle_hash(self.lc.clone(), pass, hash, self, peer).await;
} }
async fn handle_login_from_ui( async fn handle_login_from_ui(
&mut self, &self,
os_username: String, os_username: String,
os_password: String, os_password: String,
password: String, password: String,
@ -1305,7 +1304,7 @@ impl<T: InvokeUiSession> Interface for Session<T> {
.await; .await;
} }
async fn handle_test_delay(&mut self, t: TestDelay, peer: &mut Stream) { async fn handle_test_delay(&self, t: TestDelay, peer: &mut Stream) {
if !t.from_client { if !t.from_client {
self.update_quality_status(QualityStatus { self.update_quality_status(QualityStatus {
delay: Some(t.last_delay as _), delay: Some(t.last_delay as _),