From 0bd48bf46f9174c8bb088b44911da0e35f3c169d Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 3 Feb 2023 19:36:52 +0100 Subject: [PATCH] id packets --- kubi-udp/src/client.rs | 32 ++++++++++++-------------------- kubi-udp/src/common.rs | 15 +++++++++++++++ kubi-udp/src/lib.rs | 8 +++----- kubi-udp/src/packet.rs | 4 ++++ 4 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 kubi-udp/src/common.rs diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index 0538c43..fd96620 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -3,7 +3,11 @@ use std::{ marker::PhantomData, time::{Instant, Duration} }; use bincode::{Encode, Decode}; -use crate::{BINCODE_CONFIG, packet::ClientPacket}; +use crate::{ + BINCODE_CONFIG, + packet::{ClientPacket, IdClientPacket}, + common::{ClientId, DisconnectReason} +}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ClientStatus { @@ -18,19 +22,6 @@ pub struct ClientConfig { pub heartbeat_interval: Duration, } -pub type ClientId = u8; - -#[derive(Default, Encode, Decode)] -#[repr(u8)] -pub enum DisconnectReason { - #[default] - NotConnected, - ClientDisconnected, - KickedByServer, - ClientTimeout, - ServerTimeout, -} - pub struct Client where S: Encode + Decode, R: Encode + Decode { addr: SocketAddr, config: ClientConfig, @@ -69,20 +60,21 @@ impl Client where S: Encode + Decode, R: Encode + Decode { self.timeout = Instant::now(); self.last_heartbeat = Instant::now(); self.socket.connect(self.addr)?; - self.send_raw_packet(&ClientPacket::Connect)?; + self.send_raw_packet(ClientPacket::Connect)?; Ok(()) } - fn send_raw_packet(&self, packet: &ClientPacket) -> anyhow::Result<()> { - let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?; + fn send_raw_packet(&self, packet: ClientPacket) -> anyhow::Result<()> { + let id_packet = IdClientPacket(self.client_id, packet); + let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?; self.socket.send(&bytes)?; Ok(()) } pub fn send_message(&self, message: S) -> anyhow::Result<()> { - self.send_raw_packet(&ClientPacket::Data(message))?; + self.send_raw_packet(ClientPacket::Data(message))?; Ok(()) } fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> { - self.send_raw_packet(&ClientPacket::Disconnect)?; + self.send_raw_packet(ClientPacket::Disconnect)?; self.status = ClientStatus::Disconnected; self.disconnect_reason = DisconnectReason::ClientDisconnected; Ok(()) @@ -108,7 +100,7 @@ impl Client where S: Encode + Decode, R: Encode + Decode { } if self.last_heartbeat.elapsed() > self.config.heartbeat_interval { log::trace!("Sending heartbeat packet"); - self.send_raw_packet(&ClientPacket::Heartbeat)?; + self.send_raw_packet(ClientPacket::Heartbeat)?; self.last_heartbeat = Instant::now(); } Ok(()) diff --git a/kubi-udp/src/common.rs b/kubi-udp/src/common.rs new file mode 100644 index 0000000..ec4b097 --- /dev/null +++ b/kubi-udp/src/common.rs @@ -0,0 +1,15 @@ +use std::num::NonZeroU8; +use bincode::{Encode, Decode}; + +pub type ClientId = NonZeroU8; + +#[derive(Default, Encode, Decode)] +#[repr(u8)] +pub enum DisconnectReason { + #[default] + NotConnected, + ClientDisconnected, + KickedByServer, + ClientTimeout, + ServerTimeout, +} diff --git a/kubi-udp/src/lib.rs b/kubi-udp/src/lib.rs index 2790ddc..b906e02 100644 --- a/kubi-udp/src/lib.rs +++ b/kubi-udp/src/lib.rs @@ -1,9 +1,7 @@ -pub(crate) mod client; -pub(crate) mod server; +pub mod client; +pub mod server; pub(crate) mod packet; - -pub use server::Server; -pub use client::Client; +pub(crate) mod common; //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/packet.rs b/kubi-udp/src/packet.rs index eb3505b..8b5b8d2 100644 --- a/kubi-udp/src/packet.rs +++ b/kubi-udp/src/packet.rs @@ -1,4 +1,5 @@ use bincode::{Encode, Decode}; +use crate::common::ClientId; #[repr(u8)] #[derive(Encode, Decode)] @@ -9,6 +10,9 @@ pub enum ClientPacket where T: Encode + Decode { Heartbeat, } +#[derive(Encode, Decode)] +pub struct IdClientPacket(pub Option, pub ClientPacket); + #[repr(u8)] #[derive(Encode, Decode)] pub enum ServerPacket where T: Encode + Decode {