From 557e7389762c5ed7839e37eae7f5aea8ccf96a11 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 2 Feb 2023 01:40:48 +0100 Subject: [PATCH] client --- kubi-udp/src/client.rs | 27 ++++++++++++++++++++------- kubi-udp/src/lib.rs | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/kubi-udp/src/client.rs b/kubi-udp/src/client.rs index 320ba2d..892f6ba 100644 --- a/kubi-udp/src/client.rs +++ b/kubi-udp/src/client.rs @@ -1,41 +1,54 @@ use std::{ net::{UdpSocket, SocketAddr}, - marker::PhantomData + marker::PhantomData, time::Instant }; use bincode::{Encode, Decode}; use crate::{BINCODE_CONFIG, packet::ClientPacket}; +#[derive(Clone, Copy, Debug)] +pub struct ClientConfig { + +} + pub struct Client where S: Encode + Decode, R: Encode + Decode { + addr: SocketAddr, + config: ClientConfig, socket: UdpSocket, + last_heartbeat: Instant, _s: PhantomData<*const S>, _r: PhantomData<*const R>, } impl Client where S: Encode + Decode, R: Encode + Decode { - pub fn connect(addr: SocketAddr) -> anyhow::Result { + pub fn connect(addr: SocketAddr, config: ClientConfig) -> anyhow::Result { let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); let socket = UdpSocket::bind(bind_addr)?; socket.set_nonblocking(true)?; socket.connect(addr)?; let client = Self { + addr, + config, socket, + last_heartbeat: Instant::now(), _s: PhantomData, _r: PhantomData, }; - client.send_packet(&ClientPacket::Connect)?; + client.send_raw_packet(&ClientPacket::Connect)?; Ok(client) } - //maybe move generics here if possible? - fn send_packet(&self, packet: &ClientPacket) -> anyhow::Result<()> { + fn send_raw_packet(&self, packet: &ClientPacket) -> anyhow::Result<()> { let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?; self.socket.send(&bytes)?; Ok(()) } pub fn send_message(&self, message: S) -> anyhow::Result<()> { - self.send_packet(&ClientPacket::Data(message))?; + self.send_raw_packet(&ClientPacket::Data(message))?; Ok(()) } pub fn disconnect(self) -> anyhow::Result<()> { - self.send_packet(&ClientPacket::Disconnect)?; + self.send_raw_packet(&ClientPacket::Disconnect)?; Ok(()) } + pub fn update(&mut self) { + + } } diff --git a/kubi-udp/src/lib.rs b/kubi-udp/src/lib.rs index caf5110..2790ddc 100644 --- a/kubi-udp/src/lib.rs +++ b/kubi-udp/src/lib.rs @@ -5,6 +5,7 @@ pub(crate) mod packet; pub use server::Server; pub use client::Client; +//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {} pub(crate) const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard() .with_little_endian() .with_variable_int_encoding()