Properly guard multithreaded TCP sends, and clean up on connection drop
This commit is contained in:
1 parent
f38bf36ee2
commit
5b7d304a39
1 file changed
+56
-21
@@ -4,19 +4,25 @@ use err_derive::Error;
|
||||
use log::*;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::btree_map::Entry;
|
||||
use std::net::{TcpStream, Shutdown, SocketAddr};
|
||||
use std::thread::JoinHandle;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||
use std::net::{TcpStream, SocketAddr};
|
||||
|
||||
/// A shareable connection to a camera. Handles serialization of messages.
|
||||
/// A shareable connection to a camera. Handles serialization of messages. To send/receive, call
|
||||
/// .subscribe() with a message ID. You can use the BcSubscription to send or receive only
|
||||
/// messages with that ID; each incoming message is routed to its appropriate subscriber.
|
||||
///
|
||||
/// There can be only one subscriber per kind of message at a time.
|
||||
pub struct BcConnection {
|
||||
connection: Arc<TcpStream>,
|
||||
connection: Arc<Mutex<TcpStream>>,
|
||||
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
|
||||
rx_thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
pub struct BcSubscription<'a> {
|
||||
pub rx: Receiver<Bc>,
|
||||
pub msg_id: u32,
|
||||
msg_id: u32,
|
||||
conn: &'a BcConnection,
|
||||
}
|
||||
|
||||
@@ -32,48 +38,63 @@ pub enum Error {
|
||||
|
||||
#[error(display="Serialization error")]
|
||||
SerializationError(#[error(source)] bc::ser::Error),
|
||||
|
||||
#[error(display="Simultaneous subscription")]
|
||||
SimultaneousSubscription { msg_id: u32 },
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub fn new(addr: SocketAddr) -> Result<BcConnection> {
|
||||
let bc_conn = BcConnection {
|
||||
connection: Arc::new(TcpStream::connect(addr)?),
|
||||
subscribers: Default::default(),
|
||||
};
|
||||
let tcp_conn = TcpStream::connect(addr)?;
|
||||
let subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>> = Default::default();
|
||||
|
||||
let mut conn = bc_conn.connection.clone();
|
||||
let mut subs = bc_conn.subscribers.clone();
|
||||
let mut subs = subscribers.clone();
|
||||
let conn = tcp_conn.try_clone()?;
|
||||
|
||||
let rx_poller = std::thread::spawn(move || {
|
||||
let rx_thread = std::thread::spawn(move || {
|
||||
let mut context = BcContext::new();
|
||||
while let Ok(_) = BcConnection::poll(&mut context, &mut conn, &mut subs) {}
|
||||
while let Ok(_) = BcConnection::poll(&mut context, &conn, &mut subs) {}
|
||||
});
|
||||
// TODO join on drop
|
||||
|
||||
Ok(bc_conn)
|
||||
Ok(BcConnection {
|
||||
connection: Arc::new(Mutex::new(tcp_conn)),
|
||||
subscribers,
|
||||
rx_thread: Some(rx_thread),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn subscribe(&self, msg_id: u32) -> Result<BcSubscription> {
|
||||
let (tx, rx) = channel();
|
||||
self.subscribers.lock().unwrap().insert(msg_id, tx);
|
||||
match self.subscribers.lock().unwrap().entry(msg_id) {
|
||||
Entry::Vacant(vac_entry) => vac_entry.insert(tx),
|
||||
Entry::Occupied(_) => return Err(Error::SimultaneousSubscription { msg_id }),
|
||||
};
|
||||
Ok(BcSubscription { rx, conn: self, msg_id })
|
||||
}
|
||||
|
||||
fn poll(
|
||||
context: &mut BcContext,
|
||||
connection: &mut Arc<TcpStream>,
|
||||
connection: &TcpStream,
|
||||
subscribers: &mut Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>
|
||||
) -> Result<()> {
|
||||
// TODO if the connection hangs up, hang up on all subscribers and delete our TcpStream
|
||||
let response = Bc::deserialize(context, &**connection)?;
|
||||
// Don't hold the lock during deserialization so we don't poison the subscribers mutex if
|
||||
// something goes wrong
|
||||
|
||||
let response = Bc::deserialize(context, connection)
|
||||
.map_err(|err| {
|
||||
// If the connection hangs up, hang up on all subscribers
|
||||
subscribers.lock().unwrap().clear();
|
||||
err
|
||||
})?;
|
||||
let msg_id = response.meta.msg_id;
|
||||
|
||||
let mut locked_subs = subscribers.lock().unwrap();
|
||||
match locked_subs.entry(msg_id) {
|
||||
Entry::Occupied(mut occ) => {
|
||||
if let Err(_) = occ.get_mut().send(response) {
|
||||
occ.remove();
|
||||
// Exceedingly unlikely, unless you mishandle the subscription object
|
||||
warn!("Subscriber to ID {} dropped their channel", msg_id);
|
||||
occ.remove();
|
||||
}
|
||||
}
|
||||
Entry::Vacant(_) => {
|
||||
@@ -86,12 +107,26 @@ impl BcConnection {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BcConnection {
|
||||
fn drop(&mut self) {
|
||||
debug!("Shutting down BcConnection...");
|
||||
let _ = self.connection.lock().unwrap().shutdown(Shutdown::Both);
|
||||
match self.rx_thread.take().expect("rx_thread join handle should always exist").join() {
|
||||
Ok(_) => {
|
||||
debug!("Shutdown finished OK");
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Receiving thread panicked: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> BcSubscription<'a> {
|
||||
pub fn send(&self, bc: Bc) -> Result<()> {
|
||||
assert!(bc.meta.msg_id == self.msg_id);
|
||||
|
||||
// TODO Use channel for writing, too
|
||||
bc.serialize(&*self.conn.connection)?;
|
||||
bc.serialize(&*self.conn.connection.lock().unwrap())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user