(fix): connections

This commit is contained in:
Alois 2026-07-28 12:44:46 +02:00
commit 2f0762fd44
Signed by: alois
SSH key fingerprint: SHA256:GBzT2DXvAuGV9XIV5W3WrzVpjU54FThmxHXdbz95J24
2 changed files with 15 additions and 34 deletions

22
Cargo.lock generated
View file

@ -1614,7 +1614,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp" name = "mtp"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"mtp-client", "mtp-client",
"mtp-codec", "mtp-codec",
@ -1630,7 +1630,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-client" name = "mtp-client"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"mtp-codec", "mtp-codec",
"mtp-common", "mtp-common",
@ -1643,7 +1643,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-codec" name = "mtp-codec"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"base64", "base64",
"byteorder", "byteorder",
@ -1656,7 +1656,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-common" name = "mtp-common"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"quinn", "quinn",
"rustls", "rustls",
@ -1667,7 +1667,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-crypto" name = "mtp-crypto"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"base64", "base64",
"chacha20poly1305", "chacha20poly1305",
@ -1689,7 +1689,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-files" name = "mtp-files"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"mtp-crypto", "mtp-crypto",
"rand 0.10.2", "rand 0.10.2",
@ -1700,7 +1700,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-host" name = "mtp-host"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"mtp-codec", "mtp-codec",
"mtp-common", "mtp-common",
@ -1715,7 +1715,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-transport" name = "mtp-transport"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"mtp-codec", "mtp-codec",
@ -1733,7 +1733,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-type-map" name = "mtp-type-map"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"serde", "serde",
"serde_yaml", "serde_yaml",
@ -1742,7 +1742,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-webserver" name = "mtp-webserver"
version = "0.2.0" version = "0.2.0"
source = "git+https://git.methanium.net/methanium/mtp#bcf8aee3716f1690f1284748ac1ff3cd22799fb0" source = "git+https://git.methanium.net/methanium/mtp#fa271e62bec7918a8c994eea59ecdd62be76ead4"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bytes", "bytes",
@ -3515,7 +3515,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]

View file

@ -6,7 +6,7 @@ use crate::{
}; };
use dashmap::DashMap; use dashmap::DashMap;
use mtp::{ use mtp::{
codec::{CommunicationType, CommunicationValue, DataType, DataValue}, codec::{CommunicationType, CommunicationValue},
crypto::PublicKeyBundle, crypto::PublicKeyBundle,
host::{AuthenticationPolicy, HostConfig, Policy, SendMode}, host::{AuthenticationPolicy, HostConfig, Policy, SendMode},
webserver::{MTPWebServer, WebMtpReceiver, WebMtpSender}, webserver::{MTPWebServer, WebMtpReceiver, WebMtpSender},
@ -19,10 +19,7 @@ use std::{
}, },
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use tokio::{ use tokio::{sync::Mutex, time::interval};
sync::{Mutex, RwLock},
time::interval,
};
const CLEANUP_INTERVAL: Duration = Duration::from_secs(30); const CLEANUP_INTERVAL: Duration = Duration::from_secs(30);
const MAX_WAITING_AGE: Duration = Duration::from_secs(60); const MAX_WAITING_AGE: Duration = Duration::from_secs(60);
@ -52,7 +49,6 @@ pub struct WaitingTask {
pub struct OmikronConnection { pub struct OmikronConnection {
id: u64, id: u64,
sender: Mutex<Option<WebMtpSender>>, sender: Mutex<Option<WebMtpSender>>,
pub ping: RwLock<i64>,
waiting_tasks: DashMap<u32, WaitingTask>, waiting_tasks: DashMap<u32, WaitingTask>,
cleanup_handle: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>, cleanup_handle: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
} }
@ -69,7 +65,6 @@ impl OmikronConnection {
Arc::new(Self { Arc::new(Self {
id, id,
sender: Mutex::new(Some(sender)), sender: Mutex::new(Some(sender)),
ping: RwLock::new(-1),
waiting_tasks: DashMap::new(), waiting_tasks: DashMap::new(),
cleanup_handle: std::sync::Mutex::new(None), cleanup_handle: std::sync::Mutex::new(None),
}) })
@ -127,16 +122,11 @@ impl OmikronConnection {
} }
async fn process_message(self: Arc<Self>, value: CommunicationValue) -> OmikronResult<()> { async fn process_message(self: Arc<Self>, value: CommunicationValue) -> OmikronResult<()> {
if !value.is_type(CommunicationType::Pong) && !value.is_type(CommunicationType::Ping) { log_cv_in!(PrintType::Omikron, &value);
log_cv_in!(PrintType::Omikron, &value);
}
if let Some((_, task)) = self.waiting_tasks.remove(&value.get_id()) { if let Some((_, task)) = self.waiting_tasks.remove(&value.get_id()) {
let _ = (task.task)(self.clone(), value); let _ = (task.task)(self.clone(), value);
return Ok(()); return Ok(());
} }
if value.is_type(CommunicationType::Ping) {
return self.ping(value).await;
}
self.dispatch(value).await self.dispatch(value).await
} }
@ -215,17 +205,8 @@ impl OmikronConnection {
} }
} }
async fn ping(self: Arc<Self>, value: CommunicationValue) -> OmikronResult<()> {
if let DataValue::SignedNumber(last_ping) = value.get_data(DataType::LastPing) {
*self.ping.write().await = *last_ping as i64;
}
let response = CommunicationValue::new(CommunicationType::Pong).with_id(value.get_id());
self.send(&response).await
}
pub(crate) async fn send(self: Arc<Self>, value: &CommunicationValue) -> OmikronResult<()> { pub(crate) async fn send(self: Arc<Self>, value: &CommunicationValue) -> OmikronResult<()> {
if !value.is_type(CommunicationType::Pong) && !value.is_type(CommunicationType::Ping) { log_cv_out!(PrintType::Omikron, value);
log_cv_out!(PrintType::Omikron, value);
}
let guard = self.sender.lock().await; let guard = self.sender.lock().await;
let sender = guard let sender = guard
.as_ref() .as_ref()