From b234106c017fc7145807dd71732f72bfdda2f807 Mon Sep 17 00:00:00 2001 From: Shaochang Tan <478710209@qq.com> Date: Wed, 4 Sep 2024 22:47:38 +0200 Subject: [PATCH] add port check before spawning new viewer --- crates/top/rerun/src/run.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/top/rerun/src/run.rs b/crates/top/rerun/src/run.rs index 46d7a1fa10eee..f99a310ee0d8b 100644 --- a/crates/top/rerun/src/run.rs +++ b/crates/top/rerun/src/run.rs @@ -887,6 +887,7 @@ fn run_impl( ) -> anyhow::Result<()> { #[cfg(feature = "native_viewer")] let profiler = profiler(&args); + let mut is_another_viewer_running = false; #[cfg(feature = "native_viewer")] let startup_options = { @@ -951,13 +952,26 @@ fn run_impl( #[cfg(feature = "server")] { - let server_options = re_sdk_comms::ServerOptions { - max_latency_sec: parse_max_latency(args.drop_at_latency.as_ref()), - quiet: false, - }; - let tcp_listener: Receiver = - re_sdk_comms::serve(&args.bind, args.port, server_options)?; - rxs.push(tcp_listener); + // Check if there is already a viewer running + // and if so, send the data to it. + use std::net::TcpStream; + let connect_addr = std::net::SocketAddr::new(args.bind.parse().unwrap(), args.port); + if TcpStream::connect_timeout(&connect_addr, std::time::Duration::from_secs(1)).is_ok() + { + re_log::info!( + addr = %connect_addr, + "A process is already listening at this address. Assuming it's a Rerun Viewer." + ); + is_another_viewer_running = true; + } else { + let server_options = re_sdk_comms::ServerOptions { + max_latency_sec: parse_max_latency(args.drop_at_latency.as_ref()), + quiet: false, + }; + let tcp_listener: Receiver = + re_sdk_comms::serve(&args.bind, args.port, server_options)?; + rxs.push(tcp_listener); + } } rxs @@ -1030,6 +1044,9 @@ fn run_impl( return Ok(()); } + } else if is_another_viewer_running { + re_log::info!("Another viewer is already running, streaming data to it."); + Ok(()) } else { #[cfg(feature = "native_viewer")] return re_viewer::run_native_app(