mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-25 21:28:20 -06:00
kick inactive clients
This commit is contained in:
parent
ed721ea91c
commit
7bb7b9b131
|
@ -74,12 +74,17 @@ impl<S, R> Server<S, R> where S: Message, R: Message {
|
|||
})
|
||||
}
|
||||
|
||||
fn send_to_addr(&self, addr: SocketAddr, packet: IdServerPacket<S>) -> Result<()> {
|
||||
|
||||
fn send_to_addr_inner(socket: &UdpSocket, addr: SocketAddr, packet: IdServerPacket<S>) -> Result<()> {
|
||||
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
|
||||
self.socket.send_to(&bytes, addr)?;
|
||||
socket.send_to(&bytes, addr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_to_addr(&self, addr: SocketAddr, packet: IdServerPacket<S>) -> Result<()> {
|
||||
Self::send_to_addr_inner(&self.socket, addr, packet)
|
||||
}
|
||||
|
||||
fn send_packet(&self, packet: IdServerPacket<S>) -> Result<()> {
|
||||
let Some(id) = packet.0 else {
|
||||
bail!("send_to_client call without id")
|
||||
|
@ -144,7 +149,21 @@ impl<S, R> Server<S, R> where S: Message, R: Message {
|
|||
}
|
||||
|
||||
pub fn update(&mut self) -> Result<()> {
|
||||
//TODO client timeout
|
||||
//kick inactive clients
|
||||
self.clients.retain(|&id, client| {
|
||||
if client.timeout.elapsed() > self.config.client_timeout {
|
||||
if let Err(_) = Self::send_to_addr_inner(&self.socket, client.addr, IdServerPacket(
|
||||
Some(id), ServerPacket::Disconnected("Timed out".into())
|
||||
)) {
|
||||
log::warn!("Client {id} timed out and we failed to send the kick packet. This shouldn't reaally matter")
|
||||
} else {
|
||||
log::info!("Client {id} timed out");
|
||||
}
|
||||
return false
|
||||
}
|
||||
true
|
||||
});
|
||||
|
||||
let mut buf = [0; u16::MAX as usize];
|
||||
loop {
|
||||
match self.socket.recv_from(&mut buf) {
|
||||
|
|
Loading…
Reference in a new issue