mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-25 16:28:42 -06:00
wip
This commit is contained in:
parent
4cece2a876
commit
d5efff860c
|
@ -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<String>),
|
||||
ClientTimeout,
|
||||
ServerTimeout,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ClientStatus {
|
||||
Disconnected,
|
||||
|
@ -149,7 +160,7 @@ impl<S, R> Client<S, R> 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(())
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
||||
|
|
|
@ -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<ClientId> {
|
||||
let id = (1..=self.config.max_clients)
|
||||
fn connect_client(&mut self, addr: SocketAddr) -> Result<ClientId> {
|
||||
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();
|
||||
|
|
Loading…
Reference in a new issue