mod.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. mod src;
  2. pub use src::*;
  3. use hbb_common::{allow_err, log};
  4. use std::sync::{Arc, Mutex};
  5. lazy_static::lazy_static! {
  6. static ref STOP: Arc<Mutex<bool>> = Arc::new(Mutex::new(true));
  7. }
  8. fn is_running() -> bool {
  9. !*STOP.lock().unwrap()
  10. }
  11. pub fn start(license: &str, host: &str) {
  12. if is_running() {
  13. return;
  14. }
  15. *STOP.lock().unwrap() = false;
  16. let port = rendezvous_server::DEFAULT_PORT;
  17. let addr = format!("0.0.0.0:{}", port);
  18. let addr2 = format!("0.0.0.0:{}", port.parse::<i32>().unwrap_or(0) - 1);
  19. let relay_servers: Vec<String> = vec![format!("{}:{}", host, relay_server::DEFAULT_PORT)];
  20. let tmp_license = license.to_owned();
  21. std::thread::spawn(move || {
  22. allow_err!(rendezvous_server::RendezvousServer::start(
  23. &addr,
  24. &addr2,
  25. relay_servers,
  26. 0,
  27. Default::default(),
  28. Default::default(),
  29. &tmp_license,
  30. STOP.clone(),
  31. ));
  32. });
  33. let tmp_license = license.to_owned();
  34. std::thread::spawn(move || {
  35. allow_err!(relay_server::start(
  36. relay_server::DEFAULT_PORT,
  37. &tmp_license,
  38. STOP.clone()
  39. ));
  40. });
  41. }
  42. pub fn stop() {
  43. *STOP.lock().unwrap() = true;
  44. }