Merge pull request #3584 from 21pages/fix

fix frequent load RustDesk_default.toml
This commit is contained in:
RustDesk 2023-03-10 12:50:06 +08:00 committed by GitHub
commit 2dd7a695d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
time::SystemTime, time::{Duration, Instant, SystemTime},
}; };
use anyhow::Result; use anyhow::Result;
@ -51,6 +51,7 @@ lazy_static::lazy_static! {
pub static ref APP_NAME: Arc<RwLock<String>> = Arc::new(RwLock::new("RustDesk".to_owned())); pub static ref APP_NAME: Arc<RwLock<String>> = Arc::new(RwLock::new("RustDesk".to_owned()));
static ref KEY_PAIR: Arc<Mutex<Option<KeyPair>>> = Default::default(); static ref KEY_PAIR: Arc<Mutex<Option<KeyPair>>> = Default::default();
static ref HW_CODEC_CONFIG: Arc<RwLock<HwCodecConfig>> = Arc::new(RwLock::new(HwCodecConfig::load())); static ref HW_CODEC_CONFIG: Arc<RwLock<HwCodecConfig>> = Arc::new(RwLock::new(HwCodecConfig::load()));
static ref USER_DEFAULT_CONFIG: Arc<RwLock<(UserDefaultConfig, Instant)>> = Arc::new(RwLock::new((UserDefaultConfig::load(), Instant::now())));
} }
lazy_static::lazy_static! { lazy_static::lazy_static! {
@ -123,7 +124,7 @@ macro_rules! serde_field_bool {
} }
impl $struct_name { impl $struct_name {
pub fn $func() -> bool { pub fn $func() -> bool {
UserDefaultConfig::load().get($field_name) == "Y" UserDefaultConfig::read().get($field_name) == "Y"
} }
} }
}; };
@ -980,21 +981,21 @@ impl PeerConfig {
serde_field_string!( serde_field_string!(
default_view_style, default_view_style,
deserialize_view_style, deserialize_view_style,
UserDefaultConfig::load().get("view_style") UserDefaultConfig::read().get("view_style")
); );
serde_field_string!( serde_field_string!(
default_scroll_style, default_scroll_style,
deserialize_scroll_style, deserialize_scroll_style,
UserDefaultConfig::load().get("scroll_style") UserDefaultConfig::read().get("scroll_style")
); );
serde_field_string!( serde_field_string!(
default_image_quality, default_image_quality,
deserialize_image_quality, deserialize_image_quality,
UserDefaultConfig::load().get("image_quality") UserDefaultConfig::read().get("image_quality")
); );
fn default_custom_image_quality() -> Vec<i32> { fn default_custom_image_quality() -> Vec<i32> {
let f: f64 = UserDefaultConfig::load() let f: f64 = UserDefaultConfig::read()
.get("custom_image_quality") .get("custom_image_quality")
.parse() .parse()
.unwrap_or(50.0); .unwrap_or(50.0);
@ -1020,15 +1021,15 @@ impl PeerConfig {
let mut mp: HashMap<String, String> = de::Deserialize::deserialize(deserializer)?; let mut mp: HashMap<String, String> = de::Deserialize::deserialize(deserializer)?;
let mut key = "codec-preference"; let mut key = "codec-preference";
if !mp.contains_key(key) { if !mp.contains_key(key) {
mp.insert(key.to_owned(), UserDefaultConfig::load().get(key)); mp.insert(key.to_owned(), UserDefaultConfig::read().get(key));
} }
key = "custom-fps"; key = "custom-fps";
if !mp.contains_key(key) { if !mp.contains_key(key) {
mp.insert(key.to_owned(), UserDefaultConfig::load().get(key)); mp.insert(key.to_owned(), UserDefaultConfig::read().get(key));
} }
key = "zoom-cursor"; key = "zoom-cursor";
if !mp.contains_key(key) { if !mp.contains_key(key) {
mp.insert(key.to_owned(), UserDefaultConfig::load().get(key)); mp.insert(key.to_owned(), UserDefaultConfig::read().get(key));
} }
Ok(mp) Ok(mp)
} }
@ -1046,7 +1047,12 @@ serde_field_bool!(
default_show_quality_monitor, default_show_quality_monitor,
"ShowQualityMonitor::default_show_quality_monitor" "ShowQualityMonitor::default_show_quality_monitor"
); );
serde_field_bool!(DisableAudio, "disable_audio", default_disable_audio, "DisableAudio::default_disable_audio"); serde_field_bool!(
DisableAudio,
"disable_audio",
default_disable_audio,
"DisableAudio::default_disable_audio"
);
serde_field_bool!( serde_field_bool!(
EnableFileTransfer, EnableFileTransfer,
"enable_file_transfer", "enable_file_transfer",
@ -1065,9 +1071,19 @@ serde_field_bool!(
default_lock_after_session_end, default_lock_after_session_end,
"LockAfterSessionEnd::default_lock_after_session_end" "LockAfterSessionEnd::default_lock_after_session_end"
); );
serde_field_bool!(PrivacyMode, "privacy_mode", default_privacy_mode, "PrivacyMode::default_privacy_mode"); serde_field_bool!(
PrivacyMode,
"privacy_mode",
default_privacy_mode,
"PrivacyMode::default_privacy_mode"
);
serde_field_bool!(AllowSwapKey, "allow_swap_key", default_allow_swap_key, "AllowSwapKey::default_allow_swap_key"); serde_field_bool!(
AllowSwapKey,
"allow_swap_key",
default_allow_swap_key,
"AllowSwapKey::default_allow_swap_key"
);
#[derive(Debug, Default, Serialize, Deserialize, Clone)] #[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct LocalConfig { pub struct LocalConfig {
@ -1282,6 +1298,14 @@ pub struct UserDefaultConfig {
} }
impl UserDefaultConfig { impl UserDefaultConfig {
pub fn read() -> UserDefaultConfig {
let mut cfg = USER_DEFAULT_CONFIG.write().unwrap();
if cfg.1.elapsed() > Duration::from_secs(1) {
*cfg = (Self::load(), Instant::now());
}
cfg.0.clone()
}
pub fn load() -> UserDefaultConfig { pub fn load() -> UserDefaultConfig {
Config::load_::<UserDefaultConfig>("_default") Config::load_::<UserDefaultConfig>("_default")
} }