diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-10-30 19:35:00 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-10-30 19:35:02 +0100 |
| commit | 2558e05b3661d3812ca6d417e5604da615124644 (patch) | |
| tree | 30e09a9b63f6a66d1081c84f513541fc5446d539 /server/src/state.rs | |
| parent | d861319090fa6378aabf4fd102566083b915c1a5 (diff) | |
| download | hurrycurry-2558e05b3661d3812ca6d417e5604da615124644.tar hurrycurry-2558e05b3661d3812ca6d417e5604da615124644.tar.bz2 hurrycurry-2558e05b3661d3812ca6d417e5604da615124644.tar.zst | |
Add connection keepalives; Disconnect reasions
Diffstat (limited to 'server/src/state.rs')
| -rw-r--r-- | server/src/state.rs | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/server/src/state.rs b/server/src/state.rs index 1b42243f..23339543 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -21,8 +21,8 @@ use crate::{ }; use anyhow::Result; use hurrycurry_locale::{TrError, tre, trm}; -use hurrycurry_protocol::{Menu, Message, PacketC, PacketS, PlayerID, VERSION}; -use log::{debug, info, trace}; +use hurrycurry_protocol::{KEEPALIVE_INTERVAL, Menu, Message, PacketC, PacketS, PlayerID, VERSION}; +use log::{debug, info, trace, warn}; use std::{ collections::HashSet, time::{Duration, Instant}, @@ -31,15 +31,16 @@ use tokio::sync::{broadcast, mpsc}; impl Server { pub fn tick_outer(&mut self, dt: f32) -> anyhow::Result<()> { - let mut idle_kick = Vec::new(); + let mut keepalive_kick = Vec::new(); for (cid, conn) in &mut self.connections { - conn.last_player_input += dt; - if conn.last_player_input > self.config.inactivity_kick_timeout { - idle_kick.push(*cid); + conn.keepalive_timer += dt; + if conn.keepalive_timer > KEEPALIVE_INTERVAL + 10. { + keepalive_kick.push(*cid); } } - for cid in idle_kick { - self.disconnect(cid, trm!("s.disconnect_reason.inactivity_kick")); + for cid in keepalive_kick { + warn!("{cid} Client did not send keepalive in time"); + self.disconnect(cid, Some(trm!("s.disconnect_reason.keepalive_timer"))); } if !self.paused { @@ -111,7 +112,7 @@ impl Server { idle: false, ready: false, players: HashSet::new(), - last_player_input: 0., + keepalive_timer: 0., replies: replies_tx, }, ); @@ -119,9 +120,11 @@ impl Server { (init, broadcast_rx, replies_rx) } - pub fn disconnect(&mut self, conn: ConnectionID, reason: Message) { + pub fn disconnect(&mut self, conn: ConnectionID, reason: Option<Message>) { if let Some(cd) = self.connections.get(&conn) { - let _ = cd.replies.try_send(PacketC::Disconnect { reason }); + if let Some(reason) = reason { + let _ = cd.replies.try_send(PacketC::Disconnect { reason }); + } for player in cd.players.clone() { let _ = self.packet_in_outer(conn, PacketS::Leave { player }); } @@ -135,8 +138,11 @@ impl Server { conn: ConnectionID, packet: PacketS, ) -> Result<Vec<PacketC>, TrError> { + let Some(conn_data) = self.connections.get_mut(&conn) else { + return Ok(vec![]); + }; if let Some(p) = get_packet_player(&packet) - && !self.connections.get(&conn).unwrap().players.contains(&p) + && !conn_data.players.contains(&p) { return Err(tre!("s.error.packet_sender_invalid")); } @@ -159,11 +165,11 @@ impl Server { } } PacketS::Ready => { - self.connections.get_mut(&conn).unwrap().ready = true; + conn_data.ready = true; self.update_paused(); } PacketS::Idle { paused } => { - self.connections.get_mut(&conn).unwrap().idle = *paused; + conn_data.idle = *paused; self.update_paused(); } PacketS::Leave { player } => { @@ -174,17 +180,12 @@ impl Server { .remove(player); } PacketS::Join { .. } => { - if self.connections.get_mut(&conn).unwrap().players.len() > 8 { + if conn_data.players.len() > 8 { return Err(tre!("s.error.conn_too_many_players")); } } - PacketS::Interact { .. } - | PacketS::Communicate { .. } - | PacketS::Movement { boost: true, .. } => { - self.connections.get_mut(&conn).unwrap().last_player_input = 0.; - } - PacketS::Movement { dir, .. } if dir.length() > 0.5 => { - self.connections.get_mut(&conn).unwrap().last_player_input = 0.; + PacketS::Keepalive => { + conn_data.keepalive_timer = 0.; } _ => (), } @@ -280,6 +281,7 @@ fn get_packet_player(packet: &PacketS) -> Option<PlayerID> { | PacketS::Ready | PacketS::ApplyScore(_) | PacketS::ReplayTick { .. } - | PacketS::Debug(_) => None, + | PacketS::Debug(_) + | PacketS::Keepalive => None, } } |