diff --git a/src/server.rs b/src/server.rs index 6e11e07..1a0f5b6 100755 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ use std::{ error::Error, io::{BufRead, BufReader, Read, Write}, net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, TcpListener, TcpStream}, - str::FromStr, sync::Arc, thread::{sleep, spawn}, time::Duration + str::FromStr, sync::Arc, thread::{spawn, JoinHandle} }; use ignore_result::Ignore; @@ -34,32 +34,35 @@ impl FlowgateServer { } pub fn run(self) { - self.start(); - - loop { - sleep(Duration::from_secs(60)); - } - } - - pub fn start(self) { let local_self = Arc::new(self); local_self.clone().start_http(); - local_self.clone().start_https(); + local_self.clone().run_https().ignore(); } - pub fn start_http(self: Arc) { + pub fn start(self) -> Vec> { + let local_self = Arc::new(self); + + let mut handles = Vec::new(); + + handles.push(local_self.clone().start_http()); + handles.push(local_self.clone().start_https()); + + handles + } + + pub fn start_http(self: Arc) -> JoinHandle<()> { spawn({ let local_self = self.clone(); move || { local_self.run_http().ignore(); } - }); + }) } - pub fn start_https(self: Arc) { + pub fn start_https(self: Arc) -> JoinHandle<()> { spawn({ let local_self = self.clone(); move || { local_self.run_https().ignore(); } - }); + }) } pub fn run_http(self: Arc) -> Result<(), Box> { @@ -344,7 +347,7 @@ impl FlowgateServer { while read < content_length { let mut buf = vec![0; 4096]; - let Ok(size) = conn.stream.get_mut().read(&mut buf) else { break }; + let size = conn.stream.get_mut().read(&mut buf).ok()?; if size == 0 { break } @@ -414,7 +417,7 @@ impl FlowgateServer { while read < content_length { let mut buf = vec![0; 4096]; - let Ok(size) = conn.stream.get_mut().read(&mut buf) else { break }; + let size = conn.stream.get_mut().read(&mut buf).ok()?; if size == 0 { break }