mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-14 03:18:41 -06:00
update
This commit is contained in:
parent
c4fb530258
commit
9eae5d780b
|
@ -8,5 +8,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bincode = "2.0.0-rc"
|
bincode = "2.0.0-rc"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
hashbrown = "0.13"
|
||||||
nohash-hasher = "0.2.0"
|
nohash-hasher = "0.2.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::{
|
||||||
net::{UdpSocket, SocketAddr},
|
net::{UdpSocket, SocketAddr},
|
||||||
time::{Instant, Duration},
|
time::{Instant, Duration},
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
|
collections::{VecDeque, vec_deque::Drain},
|
||||||
};
|
};
|
||||||
use bincode::{Encode, Decode};
|
use bincode::{Encode, Decode};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -24,6 +25,12 @@ pub struct ClientConfig {
|
||||||
pub heartbeat_interval: Duration,
|
pub heartbeat_interval: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum ClientEvent<T> where T: Encode + Decode {
|
||||||
|
Connected,
|
||||||
|
Disconnected(DisconnectReason),
|
||||||
|
MessageReceived(T)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
pub config: ClientConfig,
|
pub config: ClientConfig,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
|
@ -33,8 +40,8 @@ pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
last_heartbeat: Instant,
|
last_heartbeat: Instant,
|
||||||
client_id: Option<ClientId>,
|
client_id: Option<ClientId>,
|
||||||
disconnect_reason: DisconnectReason,
|
disconnect_reason: DisconnectReason,
|
||||||
|
event_queue: VecDeque<ClientEvent<R>>,
|
||||||
_s: PhantomData<*const S>,
|
_s: PhantomData<*const S>,
|
||||||
_r: PhantomData<*const R>,
|
|
||||||
}
|
}
|
||||||
impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
pub fn new(addr: SocketAddr, config: ClientConfig) -> Result<Self> {
|
pub fn new(addr: SocketAddr, config: ClientConfig) -> Result<Self> {
|
||||||
|
@ -50,8 +57,8 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
last_heartbeat: Instant::now(),
|
last_heartbeat: Instant::now(),
|
||||||
client_id: None,
|
client_id: None,
|
||||||
disconnect_reason: DisconnectReason::default(),
|
disconnect_reason: DisconnectReason::default(),
|
||||||
|
event_queue: VecDeque::new(),
|
||||||
_s: PhantomData,
|
_s: PhantomData,
|
||||||
_r: PhantomData,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +76,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
self.client_id = None;
|
self.client_id = None;
|
||||||
self.status = ClientStatus::Disconnected;
|
self.status = ClientStatus::Disconnected;
|
||||||
self.disconnect_reason = reason;
|
self.disconnect_reason = reason;
|
||||||
|
self.event_queue.push_back(ClientEvent::Disconnected(self.disconnect_reason.clone()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +113,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, callback: fn(R) -> Result<()>) -> Result<()> {
|
pub fn update(&mut self) -> Result<()> { // , callback: fn(ClientEvent<R>) -> Result<()>
|
||||||
if self.status == ClientStatus::Disconnected {
|
if self.status == ClientStatus::Disconnected {
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
|
@ -137,6 +145,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
ServerPacket::Connected(client_id) => {
|
ServerPacket::Connected(client_id) => {
|
||||||
self.client_id = Some(client_id);
|
self.client_id = Some(client_id);
|
||||||
self.status = ClientStatus::Connected;
|
self.status = ClientStatus::Connected;
|
||||||
|
self.event_queue.push_back(ClientEvent::Connected);
|
||||||
return Ok(())
|
return Ok(())
|
||||||
},
|
},
|
||||||
ServerPacket::Disconnected(reason) => {
|
ServerPacket::Disconnected(reason) => {
|
||||||
|
@ -146,7 +155,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
return Ok(())
|
return Ok(())
|
||||||
},
|
},
|
||||||
ServerPacket::Data(message) => {
|
ServerPacket::Data(message) => {
|
||||||
callback(message)?;
|
self.event_queue.push_back(ClientEvent::MessageReceived(message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -156,4 +165,11 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_event(&mut self) -> Option<ClientEvent<R>> {
|
||||||
|
self.event_queue.pop_front()
|
||||||
|
}
|
||||||
|
pub fn process_events(&mut self) -> Drain<ClientEvent<R>> {
|
||||||
|
self.event_queue.drain(..)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,9 @@ use std::num::NonZeroU8;
|
||||||
use bincode::{Encode, Decode};
|
use bincode::{Encode, Decode};
|
||||||
|
|
||||||
pub type ClientId = NonZeroU8;
|
pub type ClientId = NonZeroU8;
|
||||||
|
pub const MAX_CLIENTS: usize = u8::MAX as _;
|
||||||
|
|
||||||
#[derive(Default, Encode, Decode)]
|
#[derive(Default, Encode, Decode, Clone)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum DisconnectReason {
|
pub enum DisconnectReason {
|
||||||
#[default]
|
#[default]
|
||||||
|
|
|
@ -2,6 +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(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()
|
||||||
|
|
|
@ -1,14 +1,26 @@
|
||||||
use std::net::{UdpSocket, SocketAddr};
|
use std::{net::{UdpSocket, SocketAddr}, time::Instant};
|
||||||
use crate::BINCODE_CONFIG;
|
use hashbrown::HashMap;
|
||||||
|
use nohash_hasher::BuildNoHashHasher;
|
||||||
|
use crate::{BINCODE_CONFIG, common::{ClientId, MAX_CLIENTS}};
|
||||||
|
|
||||||
|
pub struct ConnectedClient {
|
||||||
|
id: ClientId,
|
||||||
|
addr: SocketAddr,
|
||||||
|
timeout: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
|
clients: HashMap<ClientId, ConnectedClient, BuildNoHashHasher<ClientId>>
|
||||||
}
|
}
|
||||||
impl Server {
|
impl Server {
|
||||||
pub fn bind(addr: SocketAddr) -> anyhow::Result<Self> {
|
pub fn bind(addr: SocketAddr) -> anyhow::Result<Self> {
|
||||||
let socket = UdpSocket::bind(addr)?;
|
let socket = UdpSocket::bind(addr)?;
|
||||||
socket.set_nonblocking(true)?;
|
socket.set_nonblocking(true)?;
|
||||||
socket.set_broadcast(true)?;
|
socket.set_broadcast(true)?;
|
||||||
Ok(Self { socket })
|
Ok(Self {
|
||||||
|
socket,
|
||||||
|
clients: HashMap::with_capacity_and_hasher(MAX_CLIENTS, BuildNoHashHasher::default())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue