kubi/kubi-udp/src/client.rs

117 lines
3.4 KiB
Rust
Raw Normal View History

2023-01-31 20:16:23 -06:00
use std::{
net::{UdpSocket, SocketAddr},
2023-02-01 19:13:32 -06:00
marker::PhantomData, time::{Instant, Duration}
2023-01-31 20:16:23 -06:00
};
use bincode::{Encode, Decode};
use crate::{BINCODE_CONFIG, packet::ClientPacket};
2023-02-01 18:54:12 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClientStatus {
Disconnected,
Connecting,
Connected,
}
2023-02-01 18:40:48 -06:00
#[derive(Clone, Copy, Debug)]
pub struct ClientConfig {
2023-02-01 19:13:32 -06:00
pub timeout: Duration,
pub heartbeat_interval: Duration,
2023-02-01 18:40:48 -06:00
}
2023-02-03 12:29:38 -06:00
pub type ClientId = u8;
#[derive(Default, Encode, Decode)]
#[repr(u8)]
pub enum DisconnectReason {
#[default]
NotConnected,
ClientDisconnected,
KickedByServer,
ClientTimeout,
ServerTimeout,
}
2023-01-31 20:24:06 -06:00
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
2023-02-01 18:40:48 -06:00
addr: SocketAddr,
config: ClientConfig,
2023-01-31 20:16:23 -06:00
socket: UdpSocket,
2023-02-01 18:54:12 -06:00
status: ClientStatus,
2023-02-01 19:13:32 -06:00
timeout: Instant,
2023-02-01 18:40:48 -06:00
last_heartbeat: Instant,
2023-02-03 12:29:38 -06:00
client_id: Option<ClientId>,
disconnect_reason: DisconnectReason,
2023-01-31 20:25:12 -06:00
_s: PhantomData<*const S>,
_r: PhantomData<*const R>,
2023-01-31 20:16:23 -06:00
}
2023-01-31 20:24:06 -06:00
impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
2023-02-01 18:54:12 -06:00
pub fn new(addr: SocketAddr, config: ClientConfig) -> anyhow::Result<Self> {
2023-01-31 20:16:23 -06:00
let bind_addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
let socket = UdpSocket::bind(bind_addr)?;
socket.set_nonblocking(true)?;
2023-02-01 18:54:12 -06:00
Ok(Self {
2023-02-01 18:40:48 -06:00
addr,
config,
2023-01-31 20:16:23 -06:00
socket,
2023-02-01 18:54:12 -06:00
status: ClientStatus::Disconnected,
2023-02-01 19:13:32 -06:00
timeout: Instant::now(),
2023-02-01 18:40:48 -06:00
last_heartbeat: Instant::now(),
2023-02-03 12:29:38 -06:00
client_id: None,
disconnect_reason: DisconnectReason::default(),
2023-01-31 20:24:06 -06:00
_s: PhantomData,
_r: PhantomData,
2023-02-01 18:54:12 -06:00
})
}
pub fn connect(&mut self) -> anyhow::Result<()> {
if self.status != ClientStatus::Disconnected {
2023-02-01 19:13:32 -06:00
anyhow::bail!("Not Disconnected");
2023-02-01 18:54:12 -06:00
}
self.status = ClientStatus::Connecting;
2023-02-01 19:13:32 -06:00
self.timeout = Instant::now();
self.last_heartbeat = Instant::now();
2023-02-01 18:54:12 -06:00
self.socket.connect(self.addr)?;
self.send_raw_packet(&ClientPacket::Connect)?;
Ok(())
2023-01-31 20:16:23 -06:00
}
2023-02-01 18:40:48 -06:00
fn send_raw_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> {
2023-01-31 20:16:23 -06:00
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
self.socket.send(&bytes)?;
Ok(())
}
2023-01-31 20:24:06 -06:00
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
2023-02-01 18:40:48 -06:00
self.send_raw_packet(&ClientPacket::Data(message))?;
2023-01-31 20:16:23 -06:00
Ok(())
}
2023-02-03 12:29:38 -06:00
fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> {
self.send_raw_packet(&ClientPacket::Disconnect)?;
self.status = ClientStatus::Disconnected;
self.disconnect_reason = DisconnectReason::ClientDisconnected;
Ok(())
}
2023-02-01 19:13:32 -06:00
pub fn disconnect(&mut self) -> anyhow::Result<()> {
if self.status != ClientStatus::Connected {
anyhow::bail!("Not Connected");
}
2023-02-03 12:29:38 -06:00
self.disconnect_inner(DisconnectReason::ClientDisconnected)?;
2023-01-31 20:16:23 -06:00
Ok(())
}
2023-02-01 19:13:32 -06:00
pub fn update(&mut self) -> anyhow::Result<()> {
if self.status == ClientStatus::Disconnected {
return Ok(())
}
if self.timeout.elapsed() > self.config.timeout {
2023-02-03 12:29:38 -06:00
log::warn!("Client timed out");
//We don't care if this packet actually gets sent because the server is likely dead
let _ = self.disconnect_inner(DisconnectReason::ClientDisconnected).map_err(|_| {
log::warn!("Failed to send disconnect packet");
});
2023-02-01 19:13:32 -06:00
return Ok(())
}
if self.last_heartbeat.elapsed() > self.config.heartbeat_interval {
2023-02-03 12:29:38 -06:00
log::trace!("Sending heartbeat packet");
2023-02-01 19:13:32 -06:00
self.send_raw_packet(&ClientPacket::Heartbeat)?;
self.last_heartbeat = Instant::now();
}
Ok(())
2023-02-01 18:40:48 -06:00
}
2023-01-31 20:16:23 -06:00
}