[Fix] More stable Connection
This commit is contained in:
parent
f24b116753
commit
7f7b8a5cdd
6 changed files with 271 additions and 244 deletions
|
|
@ -1,15 +1,21 @@
|
|||
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
|
||||
use crate::omega::omega_connection::OmegaConnection;
|
||||
use json::number::Number;
|
||||
use std::time::Duration;
|
||||
use tokio::time::Instant;
|
||||
use uuid::Uuid;
|
||||
|
||||
const PING_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
impl OmegaConnection {
|
||||
pub async fn send_ping(&self) {
|
||||
let uuid = Uuid::new_v4();
|
||||
let send_time = Instant::now();
|
||||
|
||||
self.message_send_times.lock().await.insert(uuid, send_time);
|
||||
let mut message_send_times = self.message_send_times.lock().await;
|
||||
message_send_times.retain(|_uuid, time| time.elapsed() < PING_TIMEOUT);
|
||||
message_send_times.insert(uuid, send_time);
|
||||
|
||||
self.send_ping_message(uuid).await;
|
||||
}
|
||||
|
||||
|
|
@ -27,16 +33,10 @@ impl OmegaConnection {
|
|||
/// Handles incoming pong and calculates latency
|
||||
pub async fn handle_pong(&self, cv: &CommunicationValue, _log: bool) {
|
||||
let id = cv.get_id();
|
||||
let send_time_opt = {
|
||||
let queue = self.message_send_times.lock().await;
|
||||
queue.get(&id).cloned()
|
||||
};
|
||||
|
||||
if let Some(send_time) = send_time_opt {
|
||||
let mut message_send_times = self.message_send_times.lock().await;
|
||||
if let Some(send_time) = message_send_times.remove(&id) {
|
||||
let ping = Instant::now().duration_since(send_time).as_millis() as i64;
|
||||
self.message_send_times.lock().await.remove(&id);
|
||||
|
||||
*self.last_ping.lock().await = ping as i64;
|
||||
*self.last_ping.lock().await = ping;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue