diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index 33575ad..91c2222 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -9,9 +9,20 @@ use bincode::{Encode, Decode}; use crate::{ BINCODE_CONFIG, packet::{ClientPacket, IdClientPacket, IdServerPacket, ServerPacket}, - common::{ClientId, DisconnectReason} + common::ClientId }; +#[derive(Default, Clone)] +#[repr(u8)] +pub enum DisconnectReason { + #[default] + NotConnected, + ClientDisconnected, + KickedByServer(Option), + ClientTimeout, + ServerTimeout, +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ClientStatus { Disconnected, @@ -149,7 +160,7 @@ impl Client where S: Encode + Decode, R: Encode + Decode { return Ok(()) }, ServerPacket::Disconnected(reason) => { - let reason = DisconnectReason::KickedByServer(reason); + let reason = DisconnectReason::KickedByServer(Some(reason)); //this should never fail but we're handling the error anyway self.disconnect_inner(reason, true)?; return Ok(()) diff --git a/kubi-udp/src/common.rs b/kubi-udp/src/common.rs index 26c56f8..20e5a1e 100644 --- a/kubi-udp/src/common.rs +++ b/kubi-udp/src/common.rs @@ -1,16 +1,4 @@ use std::num::NonZeroU8; -use bincode::{Encode, Decode}; pub type ClientId = NonZeroU8; pub const MAX_CLIENTS: usize = u8::MAX as _; - -#[derive(Default, Encode, Decode, Clone)] -#[repr(u8)] -pub enum DisconnectReason { - #[default] - NotConnected, - ClientDisconnected, - KickedByServer(String), - ClientTimeout, - ServerTimeout, -} diff --git a/kubi-udp/src/lib.rs b/kubi-udp/src/lib.rs index e4b9002..ec14b35 100644 --- a/kubi-udp/src/lib.rs +++ b/kubi-udp/src/lib.rs @@ -2,7 +2,7 @@ pub mod client; pub mod server; pub(crate) mod packet; pub(crate) mod common; -pub use common::{ClientId, DisconnectReason}; +pub use common::ClientId; //pub(crate) trait Serializable: bincode::Encode + bincode::Decode {} pub(crate) const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard() diff --git a/kubi-udp/src/server.rs b/kubi-udp/src/server.rs index 87c7bc8..3edb8c2 100644 --- a/kubi-udp/src/server.rs +++ b/kubi-udp/src/server.rs @@ -1,4 +1,5 @@ use std::{net::{UdpSocket, SocketAddr}, time::Instant}; +use anyhow::{Result, bail}; use hashbrown::HashMap; use nohash_hasher::BuildNoHashHasher; use crate::{BINCODE_CONFIG, common::{ClientId, MAX_CLIENTS}}; @@ -38,18 +39,22 @@ impl Server { clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default()) }) } - /// Returns None if there are no free spots left - fn connect_client(&mut self) -> Option { - let id = (1..=self.config.max_clients) + fn connect_client(&mut self, addr: SocketAddr) -> Result { + let Some(id) = (1..=self.config.max_clients) .map(|x| ClientId::new(x as _).unwrap()) - .find(|i| self.clients.contains_key(i))?; + .find(|i| self.clients.contains_key(i)) else { + bail!("Server full"); + }; + if self.clients.iter().any(|x| x.1.addr == addr) { + bail!("Already connected from the same address"); + } self.clients.insert(id, ConnectedClient { id, - addr: "0.0.0.0:0".parse().unwrap(), + addr, timeout: Instant::now(), }); - todo!(); - Some(id) + log::info!("Client with id {id} connected"); + Ok(id) } pub fn update(&mut self) { let mut buf = Vec::new();