kubi/kubi-udp/src/client.rs

160 lines
4.6 KiB
Rust
Raw Normal View History

2023-02-03 12:39:22 -06:00
use anyhow::{Result, bail};
2023-01-31 20:16:23 -06:00
use std::{
net::{UdpSocket, SocketAddr},
2023-02-03 19:46:48 -06:00
time::{Instant, Duration},
marker::PhantomData,
2023-01-31 20:16:23 -06:00
};
use bincode::{Encode, Decode};
2023-02-03 12:36:52 -06:00
use crate::{
BINCODE_CONFIG,
2023-02-03 19:46:48 -06:00
packet::{ClientPacket, IdClientPacket, IdServerPacket, ServerPacket},
2023-02-03 12:36:52 -06:00
common::{ClientId, DisconnectReason}
};
2023-01-31 20:16:23 -06:00
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-01-31 20:24:06 -06:00
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
2023-02-03 19:46:48 -06:00
pub config: ClientConfig,
2023-02-01 18:40:48 -06:00
addr: SocketAddr,
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-03 12:39:22 -06:00
pub fn new(addr: SocketAddr, config: ClientConfig) -> 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
})
}
2023-02-03 19:46:48 -06:00
fn send_raw_packet(&self, packet: ClientPacket<S>) -> Result<()> {
let id_packet = IdClientPacket(self.client_id, packet);
let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?;
self.socket.send(&bytes)?;
Ok(())
}
fn disconnect_inner(&mut self, reason: DisconnectReason, silent: bool) -> Result<()> {
if !silent {
self.send_raw_packet(ClientPacket::Disconnect)?;
}
self.client_id = None;
self.status = ClientStatus::Disconnected;
self.disconnect_reason = reason;
Ok(())
}
fn reset_timeout(&mut self) {
self.timeout = Instant::now();
}
2023-02-03 12:39:22 -06:00
pub fn connect(&mut self) -> Result<()> {
2023-02-01 18:54:12 -06:00
if self.status != ClientStatus::Disconnected {
2023-02-03 12:39:22 -06:00
bail!("Not Disconnected");
2023-02-01 18:54:12 -06:00
}
self.status = ClientStatus::Connecting;
2023-02-01 19:13:32 -06:00
self.last_heartbeat = Instant::now();
2023-02-03 19:46:48 -06:00
self.reset_timeout();
2023-02-01 18:54:12 -06:00
self.socket.connect(self.addr)?;
2023-02-03 12:36:52 -06:00
self.send_raw_packet(ClientPacket::Connect)?;
2023-02-01 18:54:12 -06:00
Ok(())
2023-01-31 20:16:23 -06:00
}
2023-02-03 19:46:48 -06:00
pub fn disconnect(&mut self) -> Result<()> {
if self.status != ClientStatus::Connected {
bail!("Not Connected");
}
self.disconnect_inner(DisconnectReason::ClientDisconnected, false)?;
2023-01-31 20:16:23 -06:00
Ok(())
}
2023-02-03 19:46:48 -06:00
2023-02-03 12:39:22 -06:00
pub fn send_message(&self, message: S) -> Result<()> {
2023-02-01 19:13:32 -06:00
if self.status != ClientStatus::Connected {
2023-02-03 12:39:22 -06:00
bail!("Not Connected");
2023-02-01 19:13:32 -06:00
}
2023-02-03 19:46:48 -06:00
self.send_raw_packet(ClientPacket::Data(message))?;
2023-01-31 20:16:23 -06:00
Ok(())
}
2023-02-03 19:46:48 -06:00
pub fn update(&mut self, callback: fn(R) -> Result<()>) -> Result<()> {
2023-02-01 19:13:32 -06:00
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
2023-02-03 19:46:48 -06:00
let _ = self.disconnect_inner(DisconnectReason::ClientDisconnected, false).map_err(|_| {
2023-02-03 12:29:38 -06:00
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-03 12:36:52 -06:00
self.send_raw_packet(ClientPacket::Heartbeat)?;
2023-02-01 19:13:32 -06:00
self.last_heartbeat = Instant::now();
}
2023-02-03 19:46:48 -06:00
//receive
let mut buf = Vec::new();
loop {
if self.socket.recv(&mut buf).is_ok() {
//TODO check the first byte of the raw data instead of decoding?
let (packet, _): (IdServerPacket<R>, _) = bincode::decode_from_slice(&buf, BINCODE_CONFIG)?;
let IdServerPacket(user_id, packet) = packet;
if self.client_id.map(|x| Some(x) != user_id).unwrap_or_default() {
continue
}
self.reset_timeout();
match packet {
ServerPacket::Connected(client_id) => {
self.client_id = Some(client_id);
self.status = ClientStatus::Connected;
return Ok(())
},
2023-02-04 15:20:19 -06:00
ServerPacket::Disconnected(reason) => {
let reason = DisconnectReason::KickedByServer(reason);
2023-02-04 15:27:19 -06:00
//this should never fail but we're handling the error anyway
2023-02-04 15:20:19 -06:00
self.disconnect_inner(reason, true)?;
2023-02-03 19:46:48 -06:00
return Ok(())
},
ServerPacket::Data(message) => {
callback(message)?;
}
}
} else {
break
}
buf.clear();
}
2023-02-01 19:13:32 -06:00
Ok(())
2023-02-01 18:40:48 -06:00
}
2023-01-31 20:16:23 -06:00
}