more robust connection handling

This commit is contained in:
Nickiel12 2024-09-15 04:39:05 +00:00
parent c41b9eb69c
commit 3f003075ef
3 changed files with 30 additions and 17 deletions

30
Cargo.lock generated
View file

@ -99,9 +99,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.87"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f00e1f6e58a40e807377c75c6a7f97bf9044fab57816f2414e6f5f4499d7b8"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]]
name = "arc-swap"
@ -2731,9 +2731,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.19.0"
version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe"
[[package]]
name = "opaque-debug"
@ -3369,9 +3369,9 @@ dependencies = [
[[package]]
name = "redox_syscall"
version = "0.5.3"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853"
dependencies = [
"bitflags 2.6.0",
]
@ -3529,9 +3529,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.38.36"
version = "0.38.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36"
checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
dependencies = [
"bitflags 2.6.0",
"errno",
@ -3542,9 +3542,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.12"
version = "0.23.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044"
checksum = "f2dabaac7466917e566adb06783a81ca48944c6898a1b08b9374106dd671f4c8"
dependencies = [
"once_cell",
"ring",
@ -4837,9 +4837,9 @@ checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
[[package]]
name = "unicode-ident"
version = "1.0.12"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "unicode-normalization"
@ -4852,9 +4852,9 @@ dependencies = [
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]]
name = "unicode-xid"
@ -4914,7 +4914,7 @@ checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vcs-common"
version = "0.1.0"
source = "git+https://git.nickiel.net/VCC/vcs-common.git?branch=main#01495ff1d8d105de485f0c746251cd471176d1cc"
source = "git+https://git.nickiel.net/VCC/vcs-common.git?branch=main#14b06e3351761a5eb29b9a2d35b1f5aa4212b82b"
dependencies = [
"async-channel",
"bincode",

View file

@ -126,8 +126,8 @@ pub async fn run_main_event_loop(
state
.camera_satellites
.iter_mut()
.filter(|x| !x.is_connected() && !x.try_connecting)
.for_each(|x| {
info!("Resetting connections");
x.try_connecting = true;
x.retry_attempts = 0;
});
@ -208,11 +208,15 @@ pub async fn run_main_event_loop(
ApplicationMessage::WebRTCPacket(pkt) => {
send_frontend_message(serde_json::to_string(&pkt).unwrap())
}
ApplicationMessage::CloseConnection => {
error!("Cannot handle close connection from satellite yet");
}
},
None => {}
}
}
}
info!("Closing the MEC loop");
state
.joystick_task_is_alive
.store(false, std::sync::atomic::Ordering::SeqCst);
@ -224,6 +228,7 @@ pub async fn run_main_event_loop(
.map(|x| x.close())
.collect();
futures::future::join_all(close_handles).await;
info!("Satellite connections all closed.");
}
pub fn send_frontend_message(message: String) {

View file

@ -34,7 +34,7 @@ impl SatelliteConnection {
connection: conn_string.clone(),
retry_attempts: 0,
try_connecting: true,
try_connecting: false,
currently_connecting: false,
@ -46,6 +46,12 @@ impl SatelliteConnection {
#[instrument(skip(self))]
pub async fn close(&mut self) {
if self.to_socket.is_some() {
if let Err(_) = self.to_socket.as_ref().unwrap().send(ApplicationMessage::CloseConnection).await {
info!("Could not send close connection to active satellite");
}
}
self.socket_is_dead
.store(true, std::sync::atomic::Ordering::SeqCst);
self.to_socket.take(); // closing all senders will dispose of the
@ -85,7 +91,9 @@ impl SatelliteConnection {
}
Err(e) => {
self.retry_attempts += 1;
self.currently_connecting = false;
if self.retry_attempts > 5 {
info!("Retry attempts maxed out. Stopping the retries");
self.try_connecting = false;
}
error!(