diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 22:58:49 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-14 22:58:49 +0800 |
| commit | 3d2874a218b0abe736dffa6d9a6b79c103ca5186 (patch) | |
| tree | c73677067738f4e6c3d810f7cf70a331705b92e0 /src/client/connector.rs | |
| parent | 6fc21196b8283c397e7fe46e9a266b33166ca7cb (diff) | |
refactor: move event loop to client
Diffstat (limited to 'src/client/connector.rs')
| -rw-r--r-- | src/client/connector.rs | 77 |
1 files changed, 37 insertions, 40 deletions
diff --git a/src/client/connector.rs b/src/client/connector.rs index 231b19a..7b5e1cd 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -1,6 +1,6 @@ pub mod httpconnector; -use std::{net::IpAddr, thread::sleep, time::Duration}; +use std::net::IpAddr; use crate::{client::Client, executor::Executor}; use remote_control_rs::tasks::IntoTask; @@ -39,50 +39,47 @@ pub trait Connection { } fn reinit(&mut self) { - sleep(Duration::from_millis(500)); self.force_network(); } - fn r#loop(&mut self) -> ! { - loop { - // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO - // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication - // the cons of the current approch is that the first time we've entered, it would start sleeping - self.reinit(); + fn tick(&mut self) { + // tradeoff: if I put this at the ened of the loop, then the continue would just skip it, and continue doing update() until with no waiting, this wastes lot of cpu/IO + // however, of course we can add a manual sleep before it continue and put this in the end, but that's code duplication + // the cons of the current approch is that the first time we've entered, it would start sleeping + self.reinit(); - match self.update() { - Result::Ok(s) => { - if self.is_prev(&s) { - continue; - } - - let mut it = s.as_ref().split_whitespace(); - let task = match it.next() { - Some(t) => t, - None => continue, - }; - // FIXME: handle the errors... peak prod code - self.ntfy( - Executor::new(task, it.map(str::to_string)) - .unwrap() - .run() - .unwrap() - .clone() - .unwrap(), - ) - .unwrap(); - } - Result::Err(e) => { - #[cfg(debug_assertions)] - std::eprintln!( - "[{}:{}:{}] {} = {:#?}", - std::file!(), - std::line!(), - std::column!(), - std::stringify!(e), - &&e as &dyn std::fmt::Debug, - ); + match self.update() { + Result::Ok(s) => { + if self.is_prev(&s) { + return; } + + let mut it = s.as_ref().split_whitespace(); + let task = match it.next() { + Some(t) => t, + None => return, + }; + // FIXME: handle the errors... peak prod code + self.ntfy( + Executor::new(task, it.map(str::to_string)) + .unwrap() + .run() + .unwrap() + .clone() + .unwrap(), + ) + .unwrap(); + } + Result::Err(e) => { + #[cfg(debug_assertions)] + std::eprintln!( + "[{}:{}:{}] {} = {:#?}", + std::file!(), + std::line!(), + std::column!(), + std::stringify!(e), + &&e as &dyn std::fmt::Debug, + ); } } } |
