diff --git a/Cargo.lock b/Cargo.lock index 0f1b88a..02c5fa1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,16 +86,16 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi 0.3.9", ] [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "base-x" @@ -492,7 +492,7 @@ checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -802,6 +802,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + [[package]] name = "http" version = "0.2.6" @@ -919,9 +928,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.98" +version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] name = "local-ip-address" @@ -936,6 +945,16 @@ dependencies = [ "windows", ] +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.14" @@ -980,24 +999,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.7.14" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "miow", - "ntapi", - "winapi 0.3.9", -] - -[[package]] -name = "miow" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" -dependencies = [ - "winapi 0.3.9", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys", ] [[package]] @@ -1051,7 +1060,7 @@ dependencies = [ "regex", "rumqttc", "serde", - "socket2", + "socket2 0.3.19", "structopt", "time", "tokio", @@ -1084,8 +1093,9 @@ dependencies = [ "nom", "rand", "regex", - "socket2", + "socket2 0.3.19", "time", + "tokio", "xml-rs", "yaserde", "yaserde_derive", @@ -1104,15 +1114,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "ntapi" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "num-integer" version = "0.1.44" @@ -1143,6 +1144,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" +dependencies = [ + "hermit-abi 0.2.6", + "libc", +] + [[package]] name = "once_cell" version = "1.8.0" @@ -1155,6 +1166,29 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "paste" version = "1.0.5" @@ -1307,6 +1341,15 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.5.4" @@ -1459,6 +1502,15 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.3" @@ -1482,6 +1534,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "socket2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" +dependencies = [ + "libc", + "winapi 0.3.9", +] + [[package]] name = "spin" version = "0.5.2" @@ -1740,17 +1802,22 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.15.0" +version = "1.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" +checksum = "597a12a59981d9e3c38d216785b0c37399f6e415e8d0712047620f189371b0bb" dependencies = [ + "autocfg", "bytes", "libc", "memchr", "mio", + "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2 0.4.7", "tokio-macros", - "winapi 0.3.9", + "windows-sys", ] [[package]] @@ -1904,6 +1971,12 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.78" @@ -2026,12 +2099,51 @@ dependencies = [ "windows_macros", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + [[package]] name = "windows_gen" version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf583322dc423ee021035b358e535015f7fd163058a31e2d37b99a939141121d" +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + [[package]] name = "windows_macros" version = "0.18.0" @@ -2042,6 +2154,24 @@ dependencies = [ "windows_gen", ] +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" + [[package]] name = "wyz" version = "0.2.0" diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index f60b6b9..c240298 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -22,6 +22,7 @@ rand = "0.8.4" regex = "1.5.4" socket2 = "0.3" time = "0.2" +tokio = { version = "1.24.2", features = ["full"] } xml-rs = "0.8" yaserde = "0.3.16" yaserde_derive = "0.3.16" diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 8372122..fb46193 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -13,14 +13,14 @@ use time::OffsetDateTime; type IResult> = Result<(I, O), nom::Err>; /// The error types used during deserialisation -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { /// A Nom parsing error usually a malformed packet #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] - IoError(#[error(source)] std::io::Error), + IoError(#[error(source)] std::sync::Arc), } type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; @@ -35,6 +35,12 @@ impl<'a> From>> for Error { } } +impl From for Error { + fn from(k: std::io::Error) -> Self { + Error::IoError(std::sync::Arc::new(k)) + } +} + fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, diff --git a/crates/core/src/bc/ser.rs b/crates/core/src/bc/ser.rs index a56de4f..d4b96bc 100644 --- a/crates/core/src/bc/ser.rs +++ b/crates/core/src/bc/ser.rs @@ -5,11 +5,23 @@ use cookie_factory::bytes::*; use cookie_factory::sequence::tuple; use cookie_factory::{combinator::*, gen}; use cookie_factory::{GenError, SerializeFn, WriteContext}; +use err_derive::Error; use log::error; use std::io::Write; /// The error types used during serialisation -pub type Error = GenError; +#[derive(Debug, Error, Clone)] +pub enum Error { + /// A Cookie Factor GenError + #[error(display = "Cookie GenError")] + GenError(#[error(source)] std::sync::Arc), +} + +impl From for Error { + fn from(k: GenError) -> Self { + Error::GenError(std::sync::Arc::new(k)) + } +} impl Bc { pub(crate) fn serialize( diff --git a/crates/core/src/bc_protocol/connection/mod.rs b/crates/core/src/bc_protocol/connection/mod.rs index 3b3cd2c..6c244d4 100644 --- a/crates/core/src/bc_protocol/connection/mod.rs +++ b/crates/core/src/bc_protocol/connection/mod.rs @@ -1,4 +1,3 @@ -#![allow(unused_imports)] //! This module handles connections and subscribers //! //! This includes a tcp and udp connections. As well @@ -35,10 +34,10 @@ pub(crate) use self::{ filesub::FileSubscriber, tcpconn::TcpSource, udpconn::UdpSource, }; -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { #[error(display = "Communication error")] - Communication(#[error(source)] std::io::Error), + Communication(#[error(source)] Arc), #[error(display = "Communication Reciever error")] RecvCommunication(#[error(source)] RecvError), @@ -71,4 +70,10 @@ pub enum Error { ConnectionUnavaliable, } +impl From for Error { + fn from(k: std::io::Error) -> Self { + Error::Communication(std::sync::Arc::new(k)) + } +} + type Result = std::result::Result; diff --git a/crates/core/src/bc_protocol/connection/tcpconn.rs b/crates/core/src/bc_protocol/connection/tcpconn.rs index 6fdd043..cd6da70 100644 --- a/crates/core/src/bc_protocol/connection/tcpconn.rs +++ b/crates/core/src/bc_protocol/connection/tcpconn.rs @@ -1,69 +1,194 @@ -use super::{BcSubscription, Error, Result}; -use crate::bc; -use crate::bc::model::*; -use crossbeam_channel::{unbounded, Receiver, Sender}; -use log::*; -use socket2::{Domain, Socket, Type}; -use std::collections::btree_map::Entry; -use std::collections::BTreeMap; -use std::error::Error as StdErr; // Just need the traits -use std::io::{Error as IoError, Read, Write}; -use std::net::{SocketAddr, TcpStream}; -use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; -use std::thread::JoinHandle; -use std::time::Duration; +use super::{Error, Result}; +use std::io; +use std::io::{BufRead, Error as IoError, ErrorKind, Read, Write}; +use std::net::SocketAddr; +use std::sync::Arc; +use tokio::net::{TcpSocket, TcpStream}; +use tokio::sync::mpsc::{channel, error::TryRecvError, Receiver, Sender}; type IoResult = std::result::Result; pub struct TcpSource { - stream: TcpStream, + stream: Arc, + tx_out: Sender>, + rx_in: Receiver>>, + rx_out_err: Receiver, + buffer: Vec, + write_buffer: Vec, } -impl TcpSource { - pub fn new(addr: SocketAddr, timeout: Duration) -> Result { - let tcp_conn = connect_to(addr, timeout)?; +const MTU: usize = 1030; - Ok(Self { stream: tcp_conn }) +impl TcpSource { + pub async fn new(addr: SocketAddr) -> Result { + let stream = Arc::new(connect_to(addr).await?); + + // Spawn task that handles incomming bytes + let (tx_in, rx_in) = channel(100); + let stream_in = stream.clone(); + tokio::spawn(async move { + loop { + match process_inbound(&stream_in).await { + Ok(data) => { + tx_in.send(Ok(data)); + } + Err(e) => { + tx_in.send(Err(e.into())); + break; + } + } + } + }); + + // Spawn task that handles outgoing bytes + let (tx_out, mut rx_out) = channel(100); + let (tx_out_err, rx_out_err) = channel(1); + let stream_out = stream.clone(); + tokio::spawn(async move { + loop { + if let Some(data) = rx_out.recv().await { + if let Err(e) = process_outbound(&stream_out, &data).await { + tx_out_err.send(e).await; + break; + } + } else { + break; + } + } + }); + + let me = Self { + stream, + tx_out, + rx_out_err, + rx_in, + buffer: Default::default(), + write_buffer: Default::default(), + }; + Ok(me) + } +} + +impl BufRead for TcpSource { + fn fill_buf(&mut self) -> IoResult<&[u8]> { + loop { + match self.rx_in.try_recv() { + Ok(Ok(data)) => { + self.buffer.extend(data); + } + Err(TryRecvError::Empty) => { + break; + } + Err(TryRecvError::Disconnected) => { + return Err(IoError::from(ErrorKind::ConnectionAborted)); + } + Err(e) => { + return Err(IoError::new(ErrorKind::Other, e)); + } + } + } + Ok(&self.buffer) } - pub fn try_clone(&self) -> IoResult { - Ok(Self { - stream: self.stream.try_clone()?, - }) + fn consume(&mut self, amt: usize) { + self.buffer.drain(0..amt).collect::>(); } } impl Read for TcpSource { fn read(&mut self, buf: &mut [u8]) -> IoResult { - self.stream.read(buf) + let buffer = self.fill_buf()?; + let amt = std::cmp::min(buf.len(), buffer.len()); + + // First check if the amount of bytes we want to read is small: + // `copy_from_slice` will generally expand to a call to `memcpy`, and + // for a single byte the overhead is significant. + if amt == 1 { + buf[0] = buffer[0]; + } else { + buf[..amt].copy_from_slice(&buffer[..amt]); + } + + self.consume(amt); + Ok(amt) } } impl Write for TcpSource { fn write(&mut self, buf: &[u8]) -> IoResult { - self.stream.write(buf) + self.write_buffer.extend(buf.to_vec()); + if self.write_buffer.len() > MTU { + let _ = self.flush(); + } + Ok(buf.len()) } fn flush(&mut self) -> IoResult<()> { - self.stream.flush() + for chunk in self.write_buffer.chunks(MTU) { + if self.tx_out.blocking_send(chunk.to_vec()).is_err() { + self.write_buffer.clear(); + return Err(IoError::new( + std::io::ErrorKind::ConnectionAborted, + "Connection dropped", + )); + } + } + self.write_buffer.clear(); + Ok(()) } } /// Helper to create a TcpStream with a connect timeout -fn connect_to(addr: SocketAddr, timeout: Duration) -> Result { +async fn connect_to(addr: SocketAddr) -> Result { 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 - } + SocketAddr::V4(_) => TcpSocket::new_v4()?, + SocketAddr::V6(_) => TcpSocket::new_v6()?, }; - socket.set_keepalive(Some(timeout))?; - socket.set_read_timeout(Some(timeout))?; - socket.set_write_timeout(Some(timeout))?; - socket.connect_timeout(&addr.into(), timeout)?; + Ok(socket.connect(addr).await?) +} - Ok(socket.into_tcp_stream()) +async fn process_inbound(stream: &TcpStream) -> Result> { + // Wait for the socket to be readable + stream.readable().await?; + + let mut buf = Vec::::with_capacity(4096); + + // Try to read data, this may still fail with `WouldBlock` + // if the readiness event is a false positive. + match stream.try_read_buf(&mut buf) { + Ok(0) => { + return Ok(Default::default()); + } + Ok(n) => { + return Ok(buf); + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + return Ok(Default::default()); + } + Err(e) => { + return Err(e.into()); + } + } +} + +async fn process_outbound(stream: &TcpStream, tx: &Vec) -> Result { + // Wait for the socket to be writable + loop { + stream.writable().await?; + + // Try to write data, this may still fail with `WouldBlock` + // if the readiness event is a false positive. + match stream.try_write(b"hello world") { + Ok(n) => { + return Ok(n); + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + continue; + } + Err(e) => { + return Err(e.into()); + } + } + } } diff --git a/crates/core/src/bc_protocol/errors.rs b/crates/core/src/bc_protocol/errors.rs index 0452303..cd64e66 100644 --- a/crates/core/src/bc_protocol/errors.rs +++ b/crates/core/src/bc_protocol/errors.rs @@ -2,7 +2,7 @@ use super::bc::model::Bc; use err_derive::Error; /// This is the primary error type of the library -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] #[allow(clippy::large_enum_variant)] pub enum Error { /// Error raised during deserlization @@ -29,7 +29,7 @@ pub enum Error { #[error(display = "Communication error")] UnintelligibleReply { /// The Bc packet that was not understood - reply: Box, + reply: std::sync::Arc>, /// The message attached to the error why: &'static str, }, diff --git a/crates/core/src/bc_protocol/ledstate.rs b/crates/core/src/bc_protocol/ledstate.rs index a3e8aee..d19f45c 100644 --- a/crates/core/src/bc_protocol/ledstate.rs +++ b/crates/core/src/bc_protocol/ledstate.rs @@ -43,7 +43,7 @@ impl BcCamera { Ok(ledstate) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "Expected LEDState xml but it was not recieved", }) } @@ -90,7 +90,7 @@ impl BcCamera { Ok(()) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not except the LEDState xml", }) } diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index a0a0b2d..660b614 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -61,7 +61,7 @@ impl BcCamera { } _ => { return Err(Error::UnintelligibleReply { - reply: Box::new(legacy_reply), + reply: std::sync::Arc::new(Box::new(legacy_reply)), why: "Expected an Encryption message back", }) } @@ -126,7 +126,7 @@ impl BcCamera { }) => return Err(Error::AuthFailed), _ => { return Err(Error::UnintelligibleReply { - reply: Box::new(modern_reply), + reply: std::sync::Arc::new(Box::new(legacy_reply)), why: "Expected a DeviceInfo message back from login", }) } diff --git a/crates/core/src/bc_protocol/motion.rs b/crates/core/src/bc_protocol/motion.rs index bc3bf2f..0a9bbc0 100644 --- a/crates/core/src/bc_protocol/motion.rs +++ b/crates/core/src/bc_protocol/motion.rs @@ -115,7 +115,7 @@ impl BcCamera { Ok(msg_num) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the request to start motion", }) } diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index aae4c91..78ac239 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -43,7 +43,7 @@ impl BcCamera { Ok(pirstate) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "Expected PirSate xml but it was not recieved", }) } @@ -89,7 +89,7 @@ impl BcCamera { Ok(()) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not except the RfAlarmCfg xml", }) } diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs index 252cbb2..00925ba 100644 --- a/crates/core/src/bc_protocol/ptz.rs +++ b/crates/core/src/bc_protocol/ptz.rs @@ -73,7 +73,7 @@ impl BcCamera { Ok(()) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the PtzControl xml", }) } diff --git a/crates/core/src/bc_protocol/reboot.rs b/crates/core/src/bc_protocol/reboot.rs index 6da31dc..add7aaa 100644 --- a/crates/core/src/bc_protocol/reboot.rs +++ b/crates/core/src/bc_protocol/reboot.rs @@ -32,7 +32,7 @@ impl BcCamera { Ok(()) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the reboot command", }) } diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 1dc0a3c..7d952eb 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -154,7 +154,7 @@ impl BcCamera { { } else { return Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the stream start command.", }); } diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index a313992..597a7ef 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -46,7 +46,7 @@ impl BcCamera { { } else { return Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the talk stop command.", }); } @@ -94,7 +94,7 @@ impl BcCamera { Ok(talk_ability) } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "Expected TalkAbility xml but it was not recieved", }) } @@ -174,7 +174,7 @@ impl BcCamera { { } else { return Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the TalkConfig xml. Audio format is likely incorrect", }); @@ -315,7 +315,7 @@ impl BcCamera { { } else { return Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "The camera did not accept the TalkConfig xml. Audio format is likely incorrect", }); diff --git a/crates/core/src/bc_protocol/time.rs b/crates/core/src/bc_protocol/time.rs index 75b6384..539c55b 100644 --- a/crates/core/src/bc_protocol/time.rs +++ b/crates/core/src/bc_protocol/time.rs @@ -56,7 +56,7 @@ impl BcCamera { Ok(dt) => dt, Err(_) => { return Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "Could not parse date", }) } @@ -77,7 +77,7 @@ impl BcCamera { } } else { Err(Error::UnintelligibleReply { - reply: Box::new(msg), + reply: std::sync::Arc::new(Box::new(msg)), why: "Reply did not contain SystemGeneral with all time fields filled out", }) } diff --git a/crates/core/src/bc_protocol/version.rs b/crates/core/src/bc_protocol/version.rs index 3aa9d0f..1c6de65 100644 --- a/crates/core/src/bc_protocol/version.rs +++ b/crates/core/src/bc_protocol/version.rs @@ -42,7 +42,7 @@ impl BcCamera { } _ => { return Err(Error::UnintelligibleReply { - reply: Box::new(modern_reply), + reply: std::sync::Arc::new(Box::new(modern_reply)), why: "Expected a VersionInfo message", }) } diff --git a/crates/core/src/bcmedia/de.rs b/crates/core/src/bcmedia/de.rs index 72c60a1..3bdef4d 100644 --- a/crates/core/src/bcmedia/de.rs +++ b/crates/core/src/bcmedia/de.rs @@ -12,14 +12,14 @@ type IResult> = Result<(I, O), nom::Err const PAD_SIZE: u32 = 8; /// The error types used during deserialisation -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { /// A Nom parsing error usually a malformed packet #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] - IoError(#[error(source)] std::io::Error), + IoError(#[error(source)] std::sync::Arc), } type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; @@ -34,6 +34,12 @@ impl<'a> From>> for Error { } } +impl From for Error { + fn from(k: std::io::Error) -> Self { + Error::IoError(std::sync::Arc::new(k)) + } +} + fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, diff --git a/crates/core/src/bcmedia/ser.rs b/crates/core/src/bcmedia/ser.rs index 39b69a8..37b0f05 100644 --- a/crates/core/src/bcmedia/ser.rs +++ b/crates/core/src/bcmedia/ser.rs @@ -11,11 +11,17 @@ use std::io::Write; const PAD_SIZE: u32 = 8; /// The error types used during serialisation -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { /// A Cookie Factor GenError #[error(display = "Cookie GenError")] - GenError(#[error(source)] GenError), + GenError(#[error(source)] std::sync::Arc), +} + +impl From for Error { + fn from(k: GenError) -> Self { + Error::GenError(std::sync::Arc::new(k)) + } } impl BcMedia { diff --git a/crates/core/src/bcudp/de.rs b/crates/core/src/bcudp/de.rs index d05dda8..4735a5d 100644 --- a/crates/core/src/bcudp/de.rs +++ b/crates/core/src/bcudp/de.rs @@ -21,14 +21,14 @@ where type IResult> = Result<(I, O), nom::Err>; /// The error types used during deserialisation -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { /// A Nom parsing error usually a malformed packet #[error(display = "Parsing error: {}", _0)] NomError(String), /// An IO error such as the stream being dropped #[error(display = "I/O error")] - IoError(#[error(source)] std::io::Error), + IoError(#[error(source)] std::sync::Arc), } type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; @@ -43,6 +43,12 @@ impl<'a> From>> for Error { } } +impl From for Error { + fn from(k: std::io::Error) -> Self { + Error::IoError(std::sync::Arc::new(k)) + } +} + fn read_from_reader(mut parser: P, mut rdr: R) -> Result where R: Read, diff --git a/crates/core/src/bcudp/ser.rs b/crates/core/src/bcudp/ser.rs index e9071f7..5a72951 100644 --- a/crates/core/src/bcudp/ser.rs +++ b/crates/core/src/bcudp/ser.rs @@ -8,11 +8,17 @@ use log::error; use std::io::Write; /// The error types used during serialisation -#[derive(Debug, Error)] +#[derive(Debug, Error, Clone)] pub enum Error { /// A Cookie Factor GenError #[error(display = "Cookie GenError")] - GenError(#[error(source)] GenError), + GenError(#[error(source)] std::sync::Arc), +} + +impl From for Error { + fn from(k: GenError) -> Self { + Error::GenError(std::sync::Arc::new(k)) + } } impl BcUdp { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..32a9786 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +edition = "2018"