Fix ping pong

This commit is contained in:
Joscha 2022-06-30 15:16:27 +02:00
parent 4da132b5bb
commit e1ef195198
3 changed files with 18 additions and 4 deletions

View file

@ -8,7 +8,7 @@
use std::fmt; use std::fmt;
use chrono::{DateTime, Utc}; use chrono::{DateTime, TimeZone, Utc};
use serde::{de, ser, Deserialize, Serialize}; use serde::{de, ser, Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
@ -329,6 +329,16 @@ impl<'de> Deserialize<'de> for Snowflake {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Time(#[serde(with = "chrono::serde::ts_seconds")] pub DateTime<Utc>); pub struct Time(#[serde(with = "chrono::serde::ts_seconds")] pub DateTime<Utc>);
impl Time {
pub fn new(timestamp: i64) -> Self {
Self(Utc.timestamp(timestamp, 0))
}
pub fn now() -> Self {
Self::new(Utc::now().timestamp())
}
}
/// Identifies a user. /// Identifies a user.
/// ///
/// The prefix of this value (up to the colon) indicates a type of session, /// The prefix of this value (up to the colon) indicates a type of session,

View file

@ -7,10 +7,10 @@ use std::convert::Infallible;
use std::time::Duration; use std::time::Duration;
use anyhow::bail; use anyhow::bail;
use chrono::Utc;
use futures::channel::oneshot; use futures::channel::oneshot;
use futures::stream::{SplitSink, SplitStream}; use futures::stream::{SplitSink, SplitStream};
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use log::warn;
use rand::Rng; use rand::Rng;
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -344,23 +344,27 @@ impl State {
async fn do_pings(&mut self, event_tx: &mpsc::UnboundedSender<Event>) -> anyhow::Result<()> { async fn do_pings(&mut self, event_tx: &mpsc::UnboundedSender<Event>) -> anyhow::Result<()> {
// Check old ws ping // Check old ws ping
if self.last_ws_ping.is_some() && self.last_ws_ping != self.last_ws_pong { if self.last_ws_ping.is_some() && self.last_ws_ping != self.last_ws_pong {
warn!("server missed ws ping");
bail!("server missed ws ping") bail!("server missed ws ping")
} }
// Send new ws ping // Send new ws ping
let mut ws_payload = [0_u8; 8]; let mut ws_payload = [0_u8; 8];
rand::thread_rng().fill(&mut ws_payload); rand::thread_rng().fill(&mut ws_payload);
self.last_ws_ping = Some(ws_payload.to_vec());
self.ws_tx self.ws_tx
.send(tungstenite::Message::Ping(ws_payload.to_vec())) .send(tungstenite::Message::Ping(ws_payload.to_vec()))
.await?; .await?;
// Check old euph ping // Check old euph ping
if self.last_euph_ping.is_some() && self.last_euph_ping != self.last_euph_pong { if self.last_euph_ping.is_some() && self.last_euph_ping != self.last_euph_pong {
warn!("server missed euph ping");
bail!("server missed euph ping") bail!("server missed euph ping")
} }
// Send new euph ping // Send new euph ping
let euph_payload = Time(Utc::now()); let euph_payload = Time::now();
self.last_euph_ping = Some(euph_payload);
let (tx, _) = oneshot::channel(); let (tx, _) = oneshot::channel();
event_tx.send(Event::send_cmd(Ping { time: euph_payload }, tx))?; event_tx.send(Event::send_cmd(Ping { time: euph_payload }, tx))?;

View file

@ -33,7 +33,7 @@ impl ToSql for Time {
impl FromSql for Time { impl FromSql for Time {
fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> { fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
let timestamp = i64::column_result(value)?; let timestamp = i64::column_result(value)?;
Ok(Self(Utc.timestamp(timestamp, 0))) Ok(Self::new(timestamp))
} }
} }