diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index c9d4893..ce7b355 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -122,8 +122,17 @@ impl BcCamera { /// This will drop the connection. It will try to send the logout request to the camera /// first pub fn disconnect(&mut self) { - if let Err(err) = self.logout() { - warn!("Could not log out, ignoring: {}", err); + // Stop polling now. We don't need it for a disconnect + // and if we don't then another we might get a deserialise + // error from another thread that polls and needs the login. + // + // It will also ensure that when we drop the connection we don't + // get an error for read retrun zero bytes. + if let Some(connection) = &self.connection { + connection.stop_polling(); + if let Err(err) = self.logout() { + warn!("Could not log out, ignoring: {}", err); + } } self.connection = None; } diff --git a/crates/core/src/bc_protocol/connection.rs b/crates/core/src/bc_protocol/connection.rs index 467b2a5..f2695a2 100644 --- a/crates/core/src/bc_protocol/connection.rs +++ b/crates/core/src/bc_protocol/connection.rs @@ -8,7 +8,7 @@ use std::collections::BTreeMap; use std::error::Error as StdErr; // Just need the traits use std::net::{Shutdown, SocketAddr, TcpStream}; use std::sync::mpsc::{channel, Receiver, Sender}; -use std::sync::{Arc, Mutex}; +use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; use std::thread::JoinHandle; use std::time::Duration; @@ -24,6 +24,7 @@ pub struct BcConnection { // Arc> because it is shared between context // and connection for deserialisation and serialistion respectivly encryption_protocol: Arc>, + poll_abort: Arc, } pub struct BcSubscription<'a> { @@ -59,19 +60,26 @@ impl BcConnection { let encryption_protocol = Arc::new(Mutex::new(EncryptionProtocol::Unencrypted)); let connections_encryption_protocol = encryption_protocol.clone(); + let poll_abort = Arc::new(AtomicBool::new(false)); + let poll_abort_rx = poll_abort.clone(); let rx_thread = std::thread::spawn(move || { let mut context = BcContext::new(connections_encryption_protocol); let mut result; - while { + loop { result = BcConnection::poll(&mut context, &conn, &mut subs); - result.is_ok() - } {} - let e = result.unwrap_err(); - error!("Deserialization error: {}", e); - let mut cause = e.source(); - while let Some(e) = cause { - error!("caused by: {}", e); - cause = e.source(); + if poll_abort_rx.load(Ordering::Relaxed) { + break; // Poll has been aborted by request usally during disconnect + } + if result.is_err() { + let e = result.unwrap_err(); + error!("Deserialization error: {}", e); + let mut cause = e.source(); + while let Some(e) = cause { + error!("caused by: {}", e); + cause = e.source(); + } + break; + } } }); @@ -80,6 +88,7 @@ impl BcConnection { subscribers, rx_thread: Some(rx_thread), encryption_protocol, + poll_abort, }) } @@ -129,6 +138,10 @@ impl BcConnection { Ok(()) } + pub fn stop_polling(&self) { + self.poll_abort.store(true, Ordering::Relaxed); + } + pub fn set_encrypted(&self, value: EncryptionProtocol) { *(self.encryption_protocol.lock().unwrap()) = value; }