mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-10 01:28:41 -06:00
kick inactive clients
This commit is contained in:
parent
32963044f3
commit
95c681d0fc
|
@ -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)?;
|
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
|
||||||
self.socket.send_to(&bytes, addr)?;
|
socket.send_to(&bytes, addr)?;
|
||||||
Ok(())
|
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<()> {
|
fn send_packet(&self, packet: IdServerPacket<S>) -> Result<()> {
|
||||||
let Some(id) = packet.0 else {
|
let Some(id) = packet.0 else {
|
||||||
bail!("send_to_client call without id")
|
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<()> {
|
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];
|
let mut buf = [0; u16::MAX as usize];
|
||||||
loop {
|
loop {
|
||||||
match self.socket.recv_from(&mut buf) {
|
match self.socket.recv_from(&mut buf) {
|
||||||
|
|
Loading…
Reference in a new issue