From cd241958a2aa8b6d46c50293eb265c01b059121a Mon Sep 17 00:00:00 2001 From: open-trade Date: Fri, 19 Mar 2021 17:29:08 +0800 Subject: [PATCH] add start/stop for external use --- mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/mod.rs b/mod.rs index 846b7e1..bcceddb 100644 --- a/mod.rs +++ b/mod.rs @@ -1,2 +1,45 @@ mod src; pub use src::*; + +use hbb_common::{allow_err, log}; +use std::sync::{Arc, Mutex}; + +lazy_static::lazy_static! { + static ref STOP: Arc> = Arc::new(Mutex::new(true)); +} + +fn is_running() -> bool { + !*STOP.lock().unwrap() +} + +pub fn start() { + if is_running() { + return; + } + *STOP.lock().unwrap() = false; + let port = rendezvous_server::DEFAULT_PORT; + let addr = format!("0.0.0.0:{}", port); + let addr2 = format!("0.0.0.0:{}", port.parse::().unwrap_or(0) - 1); + let relay_servers: Vec = Default::default(); + std::thread::spawn(move || { + allow_err!(rendezvous_server::RendezvousServer::start( + &addr, + &addr2, + relay_servers, + 0, + Default::default(), + Default::default(), + STOP.clone(), + )); + }); + std::thread::spawn(|| { + allow_err!(relay_server::start( + relay_server::DEFAULT_PORT, + STOP.clone() + )); + }); +} + +pub fn stop() { + *STOP.lock().unwrap() = true; +}