This commit is contained in:
griffi-gh 2023-02-02 01:54:12 +01:00
parent 557e738976
commit 9a29366322
2 changed files with 31 additions and 8 deletions

View file

@ -5,6 +5,13 @@ use std::{
use bincode::{Encode, Decode}; use bincode::{Encode, Decode};
use crate::{BINCODE_CONFIG, packet::ClientPacket}; use crate::{BINCODE_CONFIG, packet::ClientPacket};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientStatus {
Disconnected,
Connecting,
Connected,
}
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct ClientConfig { pub struct ClientConfig {
@ -14,26 +21,34 @@ pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
addr: SocketAddr, addr: SocketAddr,
config: ClientConfig, config: ClientConfig,
socket: UdpSocket, socket: UdpSocket,
status: ClientStatus,
last_heartbeat: Instant, last_heartbeat: Instant,
_s: PhantomData<*const S>, _s: PhantomData<*const S>,
_r: PhantomData<*const R>, _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 connect(addr: SocketAddr, config: ClientConfig) -> anyhow::Result<Self> { pub fn new(addr: SocketAddr, config: ClientConfig) -> anyhow::Result<Self> {
let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = UdpSocket::bind(bind_addr)?; let socket = UdpSocket::bind(bind_addr)?;
socket.set_nonblocking(true)?; socket.set_nonblocking(true)?;
socket.connect(addr)?; Ok(Self {
let client = Self {
addr, addr,
config, config,
socket, socket,
status: ClientStatus::Disconnected,
last_heartbeat: Instant::now(), last_heartbeat: Instant::now(),
_s: PhantomData, _s: PhantomData,
_r: PhantomData, _r: PhantomData,
}; })
client.send_raw_packet(&ClientPacket::Connect)?; }
Ok(client) pub fn connect(&mut self) -> anyhow::Result<()> {
if self.status != ClientStatus::Disconnected {
anyhow::bail!("Already {:?}", self.status);
}
self.status = ClientStatus::Connecting;
self.socket.connect(self.addr)?;
self.send_raw_packet(&ClientPacket::Connect)?;
Ok(())
} }
fn send_raw_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> { fn send_raw_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> {
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?; let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
@ -44,7 +59,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
self.send_raw_packet(&ClientPacket::Data(message))?; self.send_raw_packet(&ClientPacket::Data(message))?;
Ok(()) Ok(())
} }
pub fn disconnect(self) -> anyhow::Result<()> { pub fn disconnect(&self) -> anyhow::Result<()> {
self.send_raw_packet(&ClientPacket::Disconnect)?; self.send_raw_packet(&ClientPacket::Disconnect)?;
Ok(()) Ok(())
} }

View file

@ -3,8 +3,16 @@ use bincode::{Encode, Decode};
#[repr(u8)] #[repr(u8)]
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
pub enum ClientPacket<T> where T: Encode + Decode { pub enum ClientPacket<T> where T: Encode + Decode {
Data(T),
Connect, Connect,
Disconnect, Disconnect,
Heartbeat, Heartbeat,
Data(T) }
#[repr(u8)]
#[derive(Encode, Decode)]
pub enum ServerPacket<T> where T: Encode + Decode {
Data(T),
Connected,
Disconnected,
} }