This commit is contained in:
griffi-gh 2023-02-06 19:19:02 +01:00
parent 4cece2a876
commit d5efff860c
4 changed files with 26 additions and 22 deletions

View file

@ -9,9 +9,20 @@ use bincode::{Encode, Decode};
use crate::{ use crate::{
BINCODE_CONFIG, BINCODE_CONFIG,
packet::{ClientPacket, IdClientPacket, IdServerPacket, ServerPacket}, packet::{ClientPacket, IdClientPacket, IdServerPacket, ServerPacket},
common::{ClientId, DisconnectReason} common::ClientId
}; };
#[derive(Default, Clone)]
#[repr(u8)]
pub enum DisconnectReason {
#[default]
NotConnected,
ClientDisconnected,
KickedByServer(Option<String>),
ClientTimeout,
ServerTimeout,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientStatus { pub enum ClientStatus {
Disconnected, Disconnected,
@ -149,7 +160,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
return Ok(()) return Ok(())
}, },
ServerPacket::Disconnected(reason) => { 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 //this should never fail but we're handling the error anyway
self.disconnect_inner(reason, true)?; self.disconnect_inner(reason, true)?;
return Ok(()) return Ok(())

View file

@ -1,16 +1,4 @@
use std::num::NonZeroU8; use std::num::NonZeroU8;
use bincode::{Encode, Decode};
pub type ClientId = NonZeroU8; pub type ClientId = NonZeroU8;
pub const MAX_CLIENTS: usize = u8::MAX as _; 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,
}

View file

@ -2,7 +2,7 @@ pub mod client;
pub mod server; pub mod server;
pub(crate) mod packet; pub(crate) mod packet;
pub(crate) mod common; pub(crate) mod common;
pub use common::{ClientId, DisconnectReason}; pub use common::ClientId;
//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {} //pub(crate) trait Serializable: bincode::Encode + bincode::Decode {}
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard() pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()

View file

@ -1,4 +1,5 @@
use std::{net::{UdpSocket, SocketAddr}, time::Instant}; use std::{net::{UdpSocket, SocketAddr}, time::Instant};
use anyhow::{Result, bail};
use hashbrown::HashMap; use hashbrown::HashMap;
use nohash_hasher::BuildNoHashHasher; use nohash_hasher::BuildNoHashHasher;
use crate::{BINCODE_CONFIG, common::{ClientId, MAX_CLIENTS}}; use crate::{BINCODE_CONFIG, common::{ClientId, MAX_CLIENTS}};
@ -38,18 +39,22 @@ impl Server {
clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default()) clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default())
}) })
} }
/// Returns None if there are no free spots left fn connect_client(&mut self, addr: SocketAddr) -> Result<ClientId> {
fn connect_client(&mut self) -> Option<ClientId> { let Some(id) = (1..=self.config.max_clients)
let id = (1..=self.config.max_clients)
.map(|x| ClientId::new(x as _).unwrap()) .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 { self.clients.insert(id, ConnectedClient {
id, id,
addr: "0.0.0.0:0".parse().unwrap(), addr,
timeout: Instant::now(), timeout: Instant::now(),
}); });
todo!(); log::info!("Client with id {id} connected");
Some(id) Ok(id)
} }
pub fn update(&mut self) { pub fn update(&mut self) {
let mut buf = Vec::new(); let mut buf = Vec::new();