diff --git a/src/main.rs b/src/main.rs index 0a03760..cdc4258 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,7 +149,7 @@ fn main() { } if args.read_messages { - print!("{}", read_messages(&config.host, config.max_messages).expect("Error reading messages").0.join("\n")); + print!("{}", read_messages(&config.host, config.max_messages, 0).ok().flatten().expect("Error reading messages").0.join("\n")); return; } diff --git a/src/rac.rs b/src/rac.rs index a20fe34..1ac618f 100644 --- a/src/rac.rs +++ b/src/rac.rs @@ -33,7 +33,7 @@ fn skip_null(stream: &mut TcpStream) -> Result, Box> { } } -pub fn read_messages(host: &str, max_messages: usize) -> Result<(Vec, usize), Box> { +pub fn read_messages(host: &str, max_messages: usize, last_size: usize) -> Result, usize)>, Box> { let mut stream = TcpStream::connect(host)?; stream.write_all(&[0x00])?; @@ -56,6 +56,10 @@ pub fn read_messages(host: &str, max_messages: usize) -> Result<(Vec, us .parse()? }; + if last_size == packet_size { + return Ok(None); + } + stream.write_all(&[0x01])?; let packet_data = { @@ -75,19 +79,17 @@ pub fn read_messages(host: &str, max_messages: usize) -> Result<(Vec, us .map(|o| o.to_string()) .collect(); - Ok((lines, packet_size)) + Ok(Some((lines, packet_size))) } fn recv_loop(config: Arc, host: &str, cache: Arc<(RwLock>, AtomicUsize)>, input: Arc>, disable_formatting: bool) -> Result<(), Box> { - while let Ok(data) = read_messages(host, config.max_messages) { - if data.1 == cache.1.load(Ordering::SeqCst) { - continue + while let Ok(data) = read_messages(host, config.max_messages, cache.1.load(Ordering::SeqCst)) { + if let Some(data) = data { + *cache.0.write().unwrap() = data.0.clone(); + cache.1.store(data.1, Ordering::SeqCst); + print_console(config.clone(), data.0, &input.read().unwrap(), disable_formatting)?; + thread::sleep(Duration::from_millis(config.update_time as u64)); } - - *cache.0.write().unwrap() = data.0.clone(); - cache.1.store(data.1, Ordering::SeqCst); - print_console(config.clone(), data.0, &input.read().unwrap(), disable_formatting)?; - thread::sleep(Duration::from_millis(config.update_time as u64)); } Ok(()) }