Merge branch 'socket-timeout'

This commit is contained in:
George Hilliard committed 2020-05-13 17:36:09 -05:00
commit f50b9d8e87
6 files changed
+73 -193

No files matched your search

Generated
+28 -160
View File
@@ -38,28 +38,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "autocfg"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "backoff"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66483433ebf577e03c6546df761778e4cc40b00e5a1ea7ed850733ffb555d71f"
dependencies = [
"instant",
"rand",
]
[[package]]
name = "bitflags"
version = "1.2.1"
@@ -87,15 +71,6 @@ dependencies = [
"vec_map",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "cookie-factory"
version = "0.3.1"
@@ -143,7 +118,7 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
dependencies = [
"autocfg 1.0.0",
"autocfg",
"cfg-if",
"crossbeam-utils",
"lazy_static",
@@ -168,7 +143,7 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
dependencies = [
"autocfg 1.0.0",
"autocfg",
"cfg-if",
"lazy_static",
]
@@ -200,12 +175,6 @@ dependencies = [
"synstructure",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "futures-channel"
version = "0.3.4"
@@ -626,12 +595,6 @@ dependencies = [
"unindent",
]
[[package]]
name = "instant"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7152d2aed88aa566e7a342250f21ba2222c1ae230ad577499dbfa3c18475b80"
[[package]]
name = "lazy_static"
version = "1.4.0"
@@ -691,7 +654,7 @@ version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8"
dependencies = [
"autocfg 1.0.0",
"autocfg",
]
[[package]]
@@ -704,7 +667,6 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204"
name = "neolink"
version = "0.1.0"
dependencies = [
"backoff",
"cookie-factory",
"crossbeam",
"env_logger",
@@ -719,6 +681,7 @@ dependencies = [
"md5",
"nom",
"serde",
"socket2",
"structopt",
"toml",
"xml-rs",
@@ -749,7 +712,7 @@ version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
dependencies = [
"autocfg 1.0.0",
"autocfg",
"num-traits",
]
@@ -759,7 +722,7 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
dependencies = [
"autocfg 1.0.0",
"autocfg",
"num-integer",
"num-traits",
]
@@ -770,7 +733,7 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [
"autocfg 1.0.0",
"autocfg",
]
[[package]]
@@ -870,119 +833,10 @@ dependencies = [
]
[[package]]
name = "rand"
version = "0.6.5"
name = "redox_syscall"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
dependencies = [
"autocfg 0.1.7",
"libc",
"rand_chacha",
"rand_core 0.4.2",
"rand_hc",
"rand_isaac",
"rand_jitter",
"rand_os",
"rand_pcg",
"rand_xorshift",
"winapi",
]
[[package]]
name = "rand_chacha"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
dependencies = [
"autocfg 0.1.7",
"rand_core 0.3.1",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_hc"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_isaac"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_jitter"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
dependencies = [
"libc",
"rand_core 0.4.2",
"winapi",
]
[[package]]
name = "rand_os"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc",
"rand_core 0.4.2",
"rdrand",
"winapi",
]
[[package]]
name = "rand_pcg"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
dependencies = [
"autocfg 0.1.7",
"rand_core 0.4.2",
]
[[package]]
name = "rand_xorshift"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "regex"
@@ -1075,6 +929,18 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "socket2"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"winapi",
]
[[package]]
name = "static_assertions"
version = "0.3.4"
@@ -1256,8 +1122,9 @@ checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"
[[package]]
name = "yaserde"
version = "0.3.15"
source = "git+https://github.com/media-io/yaserde.git?rev=158bf492f#158bf492f78100a1d881555c972c552cba3e84cc"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe81055b36d926da70850a7c1a9bc72eff184659da29e008ce9f2af4bfee7c89"
dependencies = [
"log",
"xml-rs",
@@ -1265,8 +1132,9 @@ dependencies = [
[[package]]
name = "yaserde_derive"
version = "0.3.15"
source = "git+https://github.com/media-io/yaserde.git?rev=158bf492f#158bf492f78100a1d881555c972c552cba3e84cc"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fcdf088bb87971689cf7aef14b123c5c85ca8d71267985090369e5eab16a514"
dependencies = [
"proc-macro2",
"quote",
+3 -3
View File
@@ -7,7 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
backoff = "0.1"
cookie-factory = "0.3"
crossbeam = "0.7"
env_logger = "*"
@@ -22,8 +21,9 @@ 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 = { git = "https://github.com/media-io/yaserde.git", rev="158bf492f" }
yaserde_derive = { git = "https://github.com/media-io/yaserde.git", rev="158bf492f" }
yaserde = "0.3.16"
yaserde_derive = "0.3.16"
xml-rs = "0.8"
+1 -1
View File
@@ -25,7 +25,7 @@ impl Default for AllTopXmls {
}
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
#[yaserde(root="body")]
#[yaserde(rename="body")]
pub struct BcXml {
#[yaserde(rename="Encryption")]
pub encryption: Option<Encryption>,
+1 -1
View File
@@ -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(())
}
+23 -2
View File
@@ -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,22 @@ 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.set_keepalive(Some(timeout))?;
socket.connect_timeout(&addr.into(), timeout)?;
Ok(socket.into_tcp_stream())
}
+17 -26
View File
@@ -6,7 +6,6 @@ mod config;
mod cmdline;
mod gst;
use backoff::{Error::*, ExponentialBackoff, Operation};
use bc_protocol::BcCamera;
use config::{Config, CameraConfig};
use cmdline::Opt;
@@ -51,28 +50,26 @@ fn main() -> Result<(), Error> {
}
fn camera_loop(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), Error> {
let mut backoff = ExponentialBackoff {
initial_interval: Duration::from_secs(1),
max_interval: Duration::from_secs(10),
..Default::default()
};
let mut op = || {
camera_main(camera_config, output)
};
let res = op.retry_notify(&mut backoff, |err, next: Duration| {
error!("Error streaming from camera {}, will retry in {}s: {}", camera_config.name, next.as_secs(), err);
});
let mut current_backoff = Duration::from_secs(1);
let max_backoff = Duration::from_secs(15);
let err = match res {
Ok(_) => unreachable!("should never return on success"),
Err(backoff::Error::Transient(e)) |
Err(backoff::Error::Permanent(e)) => e,
};
loop {
match camera_main(camera_config, output) {
// Authentication failures are permanent; we retry everything else
Err(err @ bc_protocol::Error::AuthFailed) => {
error!("Authentication failed to camera {}, not retrying", camera_config.name);
return Err(err.into());
}
Ok(_) => error!("Camera {} stream stopped unexpectedly, will retry in {}s", camera_config.name, current_backoff.as_secs()),
Err(err) => error!("Error streaming from camera {}, will retry in {}s: {}", camera_config.name, current_backoff.as_secs(), err),
}
Err(err.into())
std::thread::sleep(current_backoff);
current_backoff = std::cmp::min(max_backoff, current_backoff * 2);
}
}
fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), backoff::Error<bc_protocol::Error>> {
fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), bc_protocol::Error> {
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
if let Some(timeout) = camera_config.timeout {
camera.set_rx_timeout(timeout);
@@ -81,13 +78,7 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(
println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr);
camera.connect()?;
// Authentication failures are permanent; we retry everything else
camera.login(&camera_config.username, camera_config.password.as_deref()).map_err(|e| {
match e {
bc_protocol::Error::AuthFailed => Permanent(e),
_ => Transient(e)
}
})?;
camera.login(&camera_config.username, camera_config.password.as_deref())?;
println!("{}: Connected to camera, starting video stream", camera_config.name);
camera.start_video(output)?;