Browse Source

add start/stop for external use

open-trade 4 years ago
parent
commit
cd241958a2
1 changed files with 43 additions and 0 deletions
  1. 43 0
      mod.rs

+ 43 - 0
mod.rs

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