From 039edfc18ee675b09573fa774a5c912a1a7a4c0d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 2 Nov 2021 13:40:43 +0700 Subject: [PATCH 1/3] Limit sending keep alive packets --- .../core/src/bc_protocol/connection/bcconn.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 4f606f2..ef50b53 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -11,7 +11,7 @@ use std::net::{SocketAddr, TcpStream}; use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc, Mutex}; use std::thread::JoinHandle; -use std::time::Duration; +use std::time::{Duration, Instant}; /// 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 @@ -46,6 +46,8 @@ impl BcConnection { let keep_alive_encryption_protocol = connections_encryption_protocol.clone(); let mut context = BcContext::new(connections_encryption_protocol); let mut result; + let mut last_keep_alive = Instant::now(); + let keep_alive_time = Duration::from_millis(500); loop { result = Self::poll(&mut context, &conn, &mut subs); if poll_abort_rx.load(Ordering::Relaxed) { @@ -61,11 +63,14 @@ impl BcConnection { break; } // Send a udp keep alive if set - if let Ok(lock) = connections_keep_alive_msg.try_lock() { - if let Some(keep_alive_msg) = lock.as_ref() { - let _ = keep_alive_msg - .serialize(&conn, &keep_alive_encryption_protocol.lock().unwrap()); - let _ = conn.flush(); + if last_keep_alive.elapsed() > keep_alive_time { + last_keep_alive = Instant::now(); + if let Ok(lock) = connections_keep_alive_msg.try_lock() { + if let Some(keep_alive_msg) = lock.as_ref() { + let _ = keep_alive_msg + .serialize(&conn, &keep_alive_encryption_protocol.lock().unwrap()); + let _ = conn.flush(); + } } } } From 9021acf6d6f1bd59fd654c109f5f8dd0fa98f262 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 24 Nov 2021 17:12:02 +0700 Subject: [PATCH 2/3] Stop sending keep alive messages if the camera responses with an error code to them --- crates/core/src/bc_protocol/connection/bcconn.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index ef50b53..c693f0c 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -49,7 +49,7 @@ impl BcConnection { let mut last_keep_alive = Instant::now(); let keep_alive_time = Duration::from_millis(500); loop { - result = Self::poll(&mut context, &conn, &mut subs); + result = Self::poll(&mut context, &conn, &mut subs, &connections_keep_alive_msg); if poll_abort_rx.load(Ordering::Relaxed) { break; // Poll has been aborted by request usally during disconnect } @@ -126,6 +126,7 @@ impl BcConnection { context: &mut BcContext, connection: &BcSource, subscribers: &mut Arc>>>, + connections_keep_alive_msg: &Arc>>, ) -> Result<()> { // Don't hold the lock during deserialization so we don't poison the subscribers mutex if // something goes wrong @@ -149,6 +150,14 @@ impl BcConnection { if msg_id != MSG_ID_UDP_KEEP_ALIVE { debug!("Ignoring uninteresting message ID {}", msg_id); trace!("Contents: {:?}", response); + } else { + // This is a keep alive message let see what the camera says about it + if response.meta.response_code != 200 { + // Camera dosen't support the current keep alive message stop sending them + if let Ok(mut lock) = connections_keep_alive_msg.try_lock() { + *lock = None; + } + } } } } From 0145983198a301738396958999757cc246006fd8 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 25 Nov 2021 10:36:56 +0700 Subject: [PATCH 3/3] Only enable keep alive during UDP connection --- crates/core/src/bc_protocol.rs | 31 ++++++++++--------- .../core/src/bc_protocol/connection/bcconn.rs | 4 +++ .../src/bc_protocol/connection/bcsource.rs | 4 +++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 50375f4..24c8fca 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -186,23 +186,24 @@ impl BcCamera { }; if let Some(conn) = &me.connection { - let keep_alive_msg = Bc { - meta: BcMeta { - msg_id: MSG_ID_UDP_KEEP_ALIVE, - channel_id: me.channel_id, - msg_num: me.new_message_num(), - stream_type: 0, - response_code: 0, - class: 0x6414, - }, - body: BcBody::ModernMsg(ModernMsg { - ..Default::default() - }), - }; + if conn.is_udp() { + let keep_alive_msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_UDP_KEEP_ALIVE, + channel_id: me.channel_id, + msg_num: me.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }; - conn.set_keep_alive_msg(keep_alive_msg); + conn.set_keep_alive_msg(keep_alive_msg); + } } - Ok(me) } diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index c693f0c..e6ee85e 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -122,6 +122,10 @@ impl BcConnection { (*self.encryption_protocol.lock().unwrap()).clone() } + pub fn is_udp(&self) -> bool { + self.sink.lock().unwrap().is_udp() + } + fn poll( context: &mut BcContext, connection: &BcSource, diff --git a/crates/core/src/bc_protocol/connection/bcsource.rs b/crates/core/src/bc_protocol/connection/bcsource.rs index 1628e58..be31773 100644 --- a/crates/core/src/bc_protocol/connection/bcsource.rs +++ b/crates/core/src/bc_protocol/connection/bcsource.rs @@ -12,6 +12,10 @@ pub enum BcSource { } impl BcSource { + pub fn is_udp(&self) -> bool { + matches!(self, BcSource::Udp(_)) + } + pub fn new_tcp(addr: SocketAddr, timeout: Duration) -> Result { let source = TcpSource::new(addr, timeout)?; Ok(BcSource::Tcp(Mutex::new(source)))