Gracefully stop polling
Polling would error on a normal disconnect before. This fixes that
This commit is contained in:
2 files changed
+34
-12
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<Mutex<EncryptionProtocol>> because it is shared between context
|
||||
// and connection for deserialisation and serialistion respectivly
|
||||
encryption_protocol: Arc<Mutex<EncryptionProtocol>>,
|
||||
poll_abort: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user