From ef96409082241db6ff012dc2d898894e6efde3a3 Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:26:03 -0700 Subject: [PATCH] fixed some handshake socket issues --- src/coordinator.rs | 34 ++++++++++++-------------- src/remote_sources/automated_source.rs | 3 --- src/remote_sources/mod.rs | 13 +++++++--- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/coordinator.rs b/src/coordinator.rs index 24f7b3c..a9602e6 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -81,25 +81,23 @@ impl<'a> CoordState<'a> { self.sck_outbound.is_some() } - pub async fn socket_start(&mut self, conn: String) { - if !(self.sck_alive_recvr.load(Ordering::SeqCst)) { - info!("Starting socket"); + async fn socket_start(&mut self, conn: String) { + info!("Starting socket"); - match connect_async(conn).await { - Ok((val, _)) => { - info!("Socket connection to camera made successfully"); + match connect_async(conn).await { + Ok((val, _)) => { + info!("Socket connection to camera made successfully"); - let (outbound, inbound) = val.split(); - self.rt.spawn(socket_listen( - self.to_mec.clone(), - self.sck_alive_recvr.clone(), - inbound, - )); - self.sck_outbound = Some(outbound); - } - Err(_) => { - error!("Couldn't connect to URL!"); - } + let (outbound, inbound) = val.split(); + self.rt.spawn(socket_listen( + self.to_mec.clone(), + self.sck_alive_recvr.clone(), + inbound, + )); + self.sck_outbound = Some(outbound); + } + Err(_) => { + error!("Couldn't connect to URL!"); } } } @@ -122,7 +120,7 @@ impl<'a> CoordState<'a> { )); } - if !self.sck_alive_server.load(Ordering::SeqCst) || self.sck_outbound.is_none() { + if !self.sck_alive_server.load(Ordering::SeqCst) { info!("Restarting socket server"); self.sck_alive_server.store(true, Ordering::SeqCst); self.rt.spawn(remote_sources::start_socketserver( diff --git a/src/remote_sources/automated_source.rs b/src/remote_sources/automated_source.rs index 376a261..039b141 100644 --- a/src/remote_sources/automated_source.rs +++ b/src/remote_sources/automated_source.rs @@ -101,15 +101,12 @@ fn process_incoming_string( ts.tracking_id = boxes[0].id; } - info!("boxes len: {}", boxes.len()); - if let Some(target_box) = boxes.into_iter().find(|e| e.id == ts.tracking_id) { let x_adjust = calc_x_adjust(target_box.x1, target_box.x2); let y_adjust = calc_y_adjust(target_box.y1); ts.last_detect = Instant::now(); Ok((x_adjust, y_adjust)) } else { - info!("Tracking ID: {}", ts.tracking_id); Err("Couldn't find target in results".to_string()) } } else { diff --git a/src/remote_sources/mod.rs b/src/remote_sources/mod.rs index bc024f2..1045945 100644 --- a/src/remote_sources/mod.rs +++ b/src/remote_sources/mod.rs @@ -10,7 +10,7 @@ use std::{ use async_channel::Sender; use futures_core::FusedStream; use futures_util::{SinkExt, StreamExt}; -use log::{error, info}; +use log::{debug, error, info, warn}; use tokio::{ net::{TcpListener, TcpStream}, runtime::Handle, @@ -50,7 +50,7 @@ pub async fn start_socketserver( let peer = stream .peer_addr() .expect("connected streams should have a peer address"); - info!("Peer address: {}", peer); + debug!("Peer address: {}", peer); rt.spawn(accept_connection( peer, @@ -109,14 +109,19 @@ async fn handle_connection( }; if msg.is_text() && msg.to_string().starts_with("Type: ") { - match msg.to_string().split(' ').collect::>()[0] { + match msg.to_string().split(' ').collect::>()[1] { "Automated" => { + debug!("Connection type is: Automated"); connection_type = Some(ConnectionType::Automated); } "Remote" => { + debug!("Connection type is: Remote"); connection_type = Some(ConnectionType::Remote); } - _ => {} + _ => { + warn!("Unknown connection type, dropping connection"); + ws_stream.close(None).await?; + } } }