Skip to content

Commit

Permalink
--no-server parameter to avoid server ipc occupied by ui (rustdesk#9272)
Browse files Browse the repository at this point in the history
Signed-off-by: 21pages <pages21@163.com>
  • Loading branch information
21pages authored Sep 6, 2024
1 parent aa3402b commit f0ca4b9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/core_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn core_main() -> Option<Vec<String>> {
let mut _is_run_as_system = false;
let mut _is_quick_support = false;
let mut _is_flutter_invoke_new_connection = false;
let mut no_server = false;
let mut arg_exe = Default::default();
for arg in std::env::args() {
if i == 0 {
Expand All @@ -62,6 +63,8 @@ pub fn core_main() -> Option<Vec<String>> {
_is_run_as_system = true;
} else if arg == "--quick_support" {
_is_quick_support = true;
} else if arg == "--no-server" {
no_server = true;
} else {
args.push(arg);
}
Expand Down Expand Up @@ -134,6 +137,7 @@ pub fn core_main() -> Option<Vec<String>> {
}
}
hbb_common::init_log(false, &log_name);
log::info!("main start args: {:?}, env: {:?}", args, std::env::args());

// linux uni (url) go here.
#[cfg(all(target_os = "linux", feature = "flutter"))]
Expand Down Expand Up @@ -161,9 +165,8 @@ pub fn core_main() -> Option<Vec<String>> {
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
init_plugins(&args);
log::info!("main start args:{:?}", args);
if args.is_empty() || crate::common::is_empty_uni_link(&args[0]) {
std::thread::spawn(move || crate::start_server(false));
std::thread::spawn(move || crate::start_server(false, no_server));
} else {
#[cfg(windows)]
{
Expand Down Expand Up @@ -279,11 +282,11 @@ pub fn core_main() -> Option<Vec<String>> {
crate::privacy_mode::restore_reg_connectivity(true);
#[cfg(any(target_os = "linux", target_os = "windows"))]
{
crate::start_server(true);
crate::start_server(true, false);
}
#[cfg(target_os = "macos")]
{
let handler = std::thread::spawn(move || crate::start_server(true));
let handler = std::thread::spawn(move || crate::start_server(true, false));
crate::tray::start_tray();
// prevent server exit when encountering errors from tray
hbb_common::allow_err!(handler.join());
Expand Down
6 changes: 3 additions & 3 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ async fn handle(data: Data, stream: &mut Connection) {
std::fs::remove_file(&Config::ipc_path("")).ok();
#[cfg(target_os = "linux")]
{
// https://github.com/rustdesk/rustdesk/discussions/9254, slow on some machines
hbb_common::sleep((crate::platform::SERVICE_INTERVAL * 2) as f32 / 1000.0 + 1.2)
hbb_common::sleep((crate::platform::SERVICE_INTERVAL * 2) as f32 / 1000.0)
.await;
crate::run_me::<&str>(vec![]).ok();
// https://github.com/rustdesk/rustdesk/discussions/9254
crate::run_me::<&str>(vec!["--no-server"]).ok();
}
#[cfg(target_os = "macos")]
{
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn main() {
cli::connect_test(p, key, token);
} else if let Some(p) = matches.value_of("server") {
log::info!("id={}", hbb_common::config::Config::get_id());
crate::start_server(true);
crate::start_server(true, false);
}
common::global_clean();
}
1 change: 1 addition & 0 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ pub fn uninstall_service(show_new_window: bool, _: bool) -> bool {
Config::set_option("stop-service".into(), "".into());
return true;
}
// systemctl stop will kill child processes, below may not be executed.
if show_new_window {
run_me_with(2);
}
Expand Down
31 changes: 21 additions & 10 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,21 @@ pub async fn start_server(_is_server: bool) {
/// * `is_server` - Whether the current client is definitely the server.
/// If true, the server will be started.
/// Otherwise, client will check if there's already a server and start one if not.
/// * `no_server` - If `is_server` is false, whether to start a server if not found.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[tokio::main]
pub async fn start_server(is_server: bool) {
#[cfg(target_os = "linux")]
{
log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY"));
}
#[cfg(windows)]
hbb_common::platform::windows::start_cpu_performance_monitor();
pub async fn start_server(is_server: bool, no_server: bool) {
use std::sync::Once;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
#[cfg(target_os = "linux")]
{
log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY"));
}
#[cfg(windows)]
hbb_common::platform::windows::start_cpu_performance_monitor();
});

if is_server {
crate::common::set_server_running(true);
Expand Down Expand Up @@ -516,8 +521,14 @@ pub async fn start_server(is_server: bool) {
crate::ipc::client_get_hwcodec_config_thread(0);
}
Err(err) => {
log::info!("server not started (will try to start): {}", err);
std::thread::spawn(|| start_server(true));
log::info!("server not started: {err:?}, no_server: {no_server}");
if no_server {
hbb_common::sleep(1.0).await;
std::thread::spawn(|| start_server(false, true));
} else {
log::info!("try start server");
std::thread::spawn(|| start_server(true, false));
}
}
}
}
Expand Down

0 comments on commit f0ca4b9

Please sign in to comment.