Gracefully stop polling

Polling would error on a normal disconnect before. This fixes that
This commit is contained in:
QuantumEntangledAndy committed 2021-08-02 09:56:08 +07:00
1 parent 4897dc80eb
commit ffdbdb6777
2 files changed
+34 -12

No files matched your search

+11 -2
View File
@@ -122,8 +122,17 @@ impl BcCamera {
/// This will drop the connection. It will try to send the logout request to the camera /// This will drop the connection. It will try to send the logout request to the camera
/// first /// first
pub fn disconnect(&mut self) { pub fn disconnect(&mut self) {
if let Err(err) = self.logout() { // Stop polling now. We don't need it for a disconnect
warn!("Could not log out, ignoring: {}", err); // 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; self.connection = None;
} }
+23 -10
View File
@@ -8,7 +8,7 @@ use std::collections::BTreeMap;
use std::error::Error as StdErr; // Just need the traits use std::error::Error as StdErr; // Just need the traits
use std::net::{Shutdown, SocketAddr, TcpStream}; use std::net::{Shutdown, SocketAddr, TcpStream};
use std::sync::mpsc::{channel, Receiver, Sender}; 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::thread::JoinHandle;
use std::time::Duration; use std::time::Duration;
@@ -24,6 +24,7 @@ pub struct BcConnection {
// Arc<Mutex<EncryptionProtocol>> because it is shared between context // Arc<Mutex<EncryptionProtocol>> because it is shared between context
// and connection for deserialisation and serialistion respectivly // and connection for deserialisation and serialistion respectivly
encryption_protocol: Arc<Mutex<EncryptionProtocol>>, encryption_protocol: Arc<Mutex<EncryptionProtocol>>,
poll_abort: Arc<AtomicBool>,
} }
pub struct BcSubscription<'a> { pub struct BcSubscription<'a> {
@@ -59,19 +60,26 @@ impl BcConnection {
let encryption_protocol = Arc::new(Mutex::new(EncryptionProtocol::Unencrypted)); let encryption_protocol = Arc::new(Mutex::new(EncryptionProtocol::Unencrypted));
let connections_encryption_protocol = encryption_protocol.clone(); 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 rx_thread = std::thread::spawn(move || {
let mut context = BcContext::new(connections_encryption_protocol); let mut context = BcContext::new(connections_encryption_protocol);
let mut result; let mut result;
while { loop {
result = BcConnection::poll(&mut context, &conn, &mut subs); result = BcConnection::poll(&mut context, &conn, &mut subs);
result.is_ok() if poll_abort_rx.load(Ordering::Relaxed) {
} {} break; // Poll has been aborted by request usally during disconnect
let e = result.unwrap_err(); }
error!("Deserialization error: {}", e); if result.is_err() {
let mut cause = e.source(); let e = result.unwrap_err();
while let Some(e) = cause { error!("Deserialization error: {}", e);
error!("caused by: {}", e); let mut cause = e.source();
cause = e.source(); while let Some(e) = cause {
error!("caused by: {}", e);
cause = e.source();
}
break;
}
} }
}); });
@@ -80,6 +88,7 @@ impl BcConnection {
subscribers, subscribers,
rx_thread: Some(rx_thread), rx_thread: Some(rx_thread),
encryption_protocol, encryption_protocol,
poll_abort,
}) })
} }
@@ -129,6 +138,10 @@ impl BcConnection {
Ok(()) Ok(())
} }
pub fn stop_polling(&self) {
self.poll_abort.store(true, Ordering::Relaxed);
}
pub fn set_encrypted(&self, value: EncryptionProtocol) { pub fn set_encrypted(&self, value: EncryptionProtocol) {
*(self.encryption_protocol.lock().unwrap()) = value; *(self.encryption_protocol.lock().unwrap()) = value;
} }