Fix ping pong
This commit is contained in:
parent
4da132b5bb
commit
e1ef195198
3 changed files with 18 additions and 4 deletions
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
use std::fmt;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use serde::{de, ser, Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
|
|
@ -329,6 +329,16 @@ impl<'de> Deserialize<'de> for Snowflake {
|
|||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
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.
|
||||
///
|
||||
/// The prefix of this value (up to the colon) indicates a type of session,
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ use std::convert::Infallible;
|
|||
use std::time::Duration;
|
||||
|
||||
use anyhow::bail;
|
||||
use chrono::Utc;
|
||||
use futures::channel::oneshot;
|
||||
use futures::stream::{SplitSink, SplitStream};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use log::warn;
|
||||
use rand::Rng;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::mpsc;
|
||||
|
|
@ -344,23 +344,27 @@ impl State {
|
|||
async fn do_pings(&mut self, event_tx: &mpsc::UnboundedSender<Event>) -> anyhow::Result<()> {
|
||||
// Check old ws ping
|
||||
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")
|
||||
}
|
||||
|
||||
// Send new ws ping
|
||||
let mut ws_payload = [0_u8; 8];
|
||||
rand::thread_rng().fill(&mut ws_payload);
|
||||
self.last_ws_ping = Some(ws_payload.to_vec());
|
||||
self.ws_tx
|
||||
.send(tungstenite::Message::Ping(ws_payload.to_vec()))
|
||||
.await?;
|
||||
|
||||
// Check old euph ping
|
||||
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")
|
||||
}
|
||||
|
||||
// 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();
|
||||
event_tx.send(Event::send_cmd(Ping { time: euph_payload }, tx))?;
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl ToSql for Time {
|
|||
impl FromSql for Time {
|
||||
fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
|
||||
let timestamp = i64::column_result(value)?;
|
||||
Ok(Self(Utc.timestamp(timestamp, 0)))
|
||||
Ok(Self::new(timestamp))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue