Set read, write, and connect timeouts on the Baichuan socket
Fixes #6 on GitHub.
This commit is contained in:
1 parent
c47553e6f9
commit
98f2e2602d
3 files changed
+24
-3
No files matched your search
@@ -22,6 +22,7 @@ indoc = "0.3"
|
||||
md5 = "0.7"
|
||||
nom = "5.1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
socket2 = "0.3"
|
||||
structopt = "0.3"
|
||||
toml = "0.5"
|
||||
yaserde = "0.3.16"
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ impl BcCamera {
|
||||
}
|
||||
|
||||
pub fn connect(&mut self) -> Result<()> {
|
||||
self.connection = Some(BcConnection::new(self.address)?);
|
||||
self.connection = Some(BcConnection::new(self.address, self.rx_timeout)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@ use crate::bc;
|
||||
use crate::bc::model::*;
|
||||
use err_derive::Error;
|
||||
use log::*;
|
||||
use socket2::{Socket, Domain, Type};
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::btree_map::Entry;
|
||||
use std::net::{TcpStream, Shutdown, SocketAddr};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||
|
||||
@@ -44,8 +46,8 @@ pub enum Error {
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub fn new(addr: SocketAddr) -> Result<BcConnection> {
|
||||
let tcp_conn = TcpStream::connect(addr)?;
|
||||
pub fn new(addr: SocketAddr, timeout: Duration) -> Result<BcConnection> {
|
||||
let tcp_conn = connect_to(addr, timeout)?;
|
||||
let subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>> = Default::default();
|
||||
|
||||
let mut subs = subscribers.clone();
|
||||
@@ -137,3 +139,21 @@ impl<'a> Drop for BcSubscription<'a> {
|
||||
self.conn.subscribers.lock().unwrap().remove(&self.msg_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to create a TcpStream with a connect timeout
|
||||
fn connect_to(addr: SocketAddr, timeout: Duration) -> Result<TcpStream> {
|
||||
let socket = match addr {
|
||||
SocketAddr::V4(_) => {
|
||||
Socket::new(Domain::ipv4(), Type::stream(), None)?
|
||||
}
|
||||
SocketAddr::V6(_) => {
|
||||
let s = Socket::new(Domain::ipv6(), Type::stream(), None)?;
|
||||
s.set_only_v6(false)?;
|
||||
s
|
||||
}
|
||||
};
|
||||
|
||||
socket.connect_timeout(&addr.into(), timeout)?;
|
||||
|
||||
Ok(socket.into_tcp_stream())
|
||||
}
|
||||
Reference in new issue
Block a user