refact, flutter sessions lock

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-10-03 09:51:21 +08:00
parent b02f169764
commit 06987c4ca9
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)
}
fn handle_peer_info(&mut self, pi: PeerInfo) {
fn handle_peer_info(&self, pi: PeerInfo) {
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!(
"password={}",
hbb_common::password_security::temporary_password()
@ -92,7 +92,7 @@ impl Interface for Session {
}
async fn handle_login_from_ui(
&mut self,
&self,
os_username: String,
os_password: String,
password: String,
@ -110,7 +110,7 @@ impl Interface for Session {
.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;
}

View File

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

View File

@ -816,7 +816,7 @@ impl<T: InvokeUiSession> Remote<T> {
job: &fs::TransferJob,
elapsed: i32,
last_update_jobs_status: &mut (Instant, HashMap<i32, u64>),
handler: &mut Session<T>,
handler: &Session<T>,
) {
if elapsed <= 0 {
return;
@ -843,7 +843,7 @@ impl<T: InvokeUiSession> Remote<T> {
job,
elapsed,
&mut self.last_update_jobs_status,
&mut self.handler,
&self.handler,
);
}
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_PORT_FORWARD: &str = "port forward";
pub type FlutterSession = Arc<Session<FlutterHandler>>;
lazy_static::lazy_static! {
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
}
@ -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();
if let Some(stream) = &*stream_lock {
stream.add(EventToUI::Event("close".to_owned()));
@ -337,13 +338,13 @@ impl FlutterHandler {
#[inline]
#[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;
}
#[inline]
#[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.renderer.write().unwrap().set_size(width, height);
}
@ -731,7 +732,7 @@ pub fn session_add(
switch_uuid: &str,
force_relay: bool,
password: String,
) -> ResultType<Session<FlutterHandler>> {
) -> ResultType<FlutterSession> {
LocalConfig::set_remote_id(&id);
let session: Session<FlutterHandler> = Session {
@ -768,11 +769,8 @@ pub fn session_add(
.unwrap()
.initialize(id.to_owned(), conn_type, switch_uuid, force_relay);
if let Some(same_id_session) = SESSIONS
.write()
.unwrap()
.insert(session_id.to_owned(), session.clone())
{
let session = Arc::new(session.clone());
if let Some(same_id_session) = sessions::add_session(session_id.to_owned(), session.clone()) {
log::error!("Should not happen");
same_id_session.close();
}
@ -791,7 +789,7 @@ pub fn session_start_(
id: &str,
event_stream: StreamSink<EventToUI>,
) -> 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")]
log::info!(
"Session {} start, render by flutter texture rgba plugin",
@ -803,7 +801,7 @@ pub fn session_start_(
session.close_event_stream();
*session.event_stream.write().unwrap() = Some(event_stream);
if !is_pre_added {
let session = session.clone();
let session = (*session).clone();
std::thread::spawn(move || {
let round = session.connection_round_state.lock().unwrap().new_round();
io_loop(session, round);
@ -817,29 +815,15 @@ pub fn session_start_(
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn update_text_clipboard_required() {
let is_required = SESSIONS
.read()
.unwrap()
let is_required = sessions::get_sessions()
.iter()
.any(|(_id, session)| session.is_text_clipboard_required());
.any(|session| session.is_text_clipboard_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")))]
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() {
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 {
#[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();
}
0
@ -1061,7 +1045,7 @@ pub fn session_get_rgba_size(_session_id: SessionID) -> usize {
#[no_mangle]
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 Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
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) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
return session.next_rgba();
}
}
@ -1078,7 +1062,7 @@ pub fn session_next_rgba(session_id: SessionID) {
#[inline]
pub fn session_register_texture(_session_id: SessionID, _ptr: usize) {
#[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);
return;
}
@ -1090,11 +1074,7 @@ pub fn push_session_event(
name: &str,
event: Vec<(&str, &str)>,
) -> Option<bool> {
SESSIONS
.read()
.unwrap()
.get(session_id)?
.push_event(name, event)
sessions::get_session(session_id)?.push_event(name, event)
}
#[inline]
@ -1143,7 +1123,7 @@ fn session_send_touch_scale(
) {
match v.get("v").and_then(|s| s.as_i64()) {
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);
}
}
@ -1167,7 +1147,7 @@ fn session_send_touch_pan(
v.get("y").and_then(|y| y.as_i64()),
) {
(Some(x), Some(y)) => {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
session
.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 {
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,
common::is_keyboard_mode_supported,
common::make_fd_to_json,
flutter::{self, SESSIONS},
flutter::{session_add, session_start_},
flutter::{self, session_add, session_start_, sessions},
input::*,
ui_interface::{self, *},
};
@ -113,7 +112,7 @@ pub fn session_start(
}
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())
} else {
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> {
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))
} else {
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> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_option(arg))
} else {
None
@ -148,55 +147,55 @@ pub fn session_login(
password: String,
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);
}
}
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();
}
}
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();
}
}
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 _);
}
}
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);
}
}
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);
}
}
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);
session.toggle_option(value.clone());
}
#[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();
}
}
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))
} else {
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) {
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);
}
}
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))
} else {
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> {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
Some(session.get_view_style())
} else {
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) {
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);
}
}
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())
} else {
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) {
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);
}
}
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())
} else {
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) {
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);
}
}
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())
} else {
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) {
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());
_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> {
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())
} else {
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) {
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);
}
}
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())
} else {
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> {
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[..]) {
SyncReturn(is_keyboard_mode_supported(
&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) {
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);
}
}
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);
}
}
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();
}
}
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();
}
}
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);
}
}
@ -375,7 +374,7 @@ pub fn session_handle_flutter_key_event(
lock_modes: i32,
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();
session.handle_flutter_key_event(
&keyboard_mode,
@ -396,7 +395,7 @@ pub fn session_handle_flutter_key_event(
// This will cause the keyboard input to take no effect.
pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> {
#[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();
if _enter {
set_cur_session_id_(_session_id, &keyboard_mode);
@ -418,14 +417,14 @@ pub fn session_input_key(
shift: 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"))]
session.input_key(&name, down, press, alt, ctrl, shift, command);
}
}
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"))]
session.input_string(&value);
}
@ -433,33 +432,33 @@ pub fn session_input_string(session_id: SessionID, value: String) {
// chat_client_mode
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);
}
}
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);
}
}
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);
}
"".to_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);
}
}
// File Action
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);
}
}
@ -473,7 +472,7 @@ pub fn session_send_files(
include_hidden: 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);
}
}
@ -486,7 +485,7 @@ pub fn session_set_confirm_override_file(
remember: 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);
}
}
@ -498,7 +497,7 @@ pub fn session_remove_file(
file_num: i32,
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);
}
}
@ -510,7 +509,7 @@ pub fn session_read_dir_recursive(
is_remote: 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);
}
}
@ -521,19 +520,19 @@ pub fn session_remove_all_empty_dirs(
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.remove_dir(act_id, path, is_remote);
}
}
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);
}
}
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);
}
}
@ -550,14 +549,14 @@ pub fn session_read_local_dir_sync(
}
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);
}
"".to_string()
}
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();
} else {
// a tip for flutter dev
@ -577,44 +576,44 @@ pub fn session_add_job(
include_hidden: 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);
}
}
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);
}
}
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();
}
}
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);
}
}
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();
}
}
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);
}
}
pub fn session_set_size(_session_id: SessionID, _width: usize, _height: usize) {
#[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);
}
}
@ -1030,31 +1029,31 @@ pub fn session_add_port_forward(
remote_host: String,
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);
}
}
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);
}
}
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();
}
}
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();
}
}
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();
}
}
@ -1239,20 +1238,20 @@ pub fn session_send_mouse(session_id: SessionID, msg: String) {
_ => 0,
} << 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);
}
}
}
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();
}
}
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)
} else {
"".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) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
session.send_note(note)
}
}
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 msg = HashMap::from([("vp8", vp8), ("av1", av1), ("h264", h264), ("h265", h265)]);
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) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Some(session) = sessions::get_session(&session_id) {
session.change_prefer_codec();
}
}
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();
}
}
@ -1523,7 +1522,7 @@ pub fn main_update_me() -> SyncReturn<bool> {
}
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())
}
}

View File

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

View File

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

View File

@ -75,7 +75,7 @@ pub async fn listen(
let interface = interface.clone();
tokio::spawn(async move {
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);
});
@ -121,7 +121,6 @@ async fn connect_and_login(
let (mut stream, direct, _pk) =
Client::start(id, key, token, conn_type, interface.clone()).await?;
interface.update_direct(Some(direct));
let mut interface = interface;
let mut buffer = Vec::new();
let mut received = false;
loop {

View File

@ -1,4 +1,7 @@
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")))]
use std::{collections::HashMap, sync::atomic::AtomicBool};
use std::{
@ -10,10 +13,6 @@ use std::{
},
time::SystemTime,
};
use async_trait::async_trait;
use bytes::Bytes;
use rdev::{Event, EventType::*, KeyCode};
use uuid::Uuid;
#[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);
}
@ -240,19 +239,19 @@ impl<T: InvokeUiSession> Session<T> {
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);
}
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);
}
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);
}
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);
}
@ -260,7 +259,7 @@ impl<T: InvokeUiSession> Session<T> {
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());
#[cfg(not(feature = "flutter"))]
if name == "enable-file-transfer" {
@ -303,7 +302,7 @@ impl<T: InvokeUiSession> Session<T> {
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
.lc
.write()
@ -312,14 +311,14 @@ impl<T: InvokeUiSession> Session<T> {
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);
if let Some(msg) = 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);
self.send(Data::Message(msg));
}
@ -423,7 +422,7 @@ impl<T: InvokeUiSession> Session<T> {
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();
if config
.port_forwards
@ -901,7 +900,7 @@ impl<T: InvokeUiSession> Session<T> {
let cloned = self.clone();
// override only if true
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();
// 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);
}
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)
}
fn handle_peer_info(&mut self, mut pi: PeerInfo) {
fn handle_peer_info(&self, mut pi: PeerInfo) {
log::debug!("handle_peer_info :{:?}", pi);
pi.username = self.lc.read().unwrap().get_username(&pi);
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;
}
async fn handle_login_from_ui(
&mut self,
&self,
os_username: String,
os_password: String,
password: String,
@ -1305,7 +1304,7 @@ impl<T: InvokeUiSession> Interface for Session<T> {
.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 {
self.update_quality_status(QualityStatus {
delay: Some(t.last_delay as _),