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};
|
2023-02-03 12:36:52 -06:00
|
|
|
use crate::{
|
|
|
|
BINCODE_CONFIG,
|
|
|
|
packet::{ClientPacket, IdClientPacket},
|
|
|
|
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-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)?;
|
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 12:36:52 -06:00
|
|
|
fn send_raw_packet(&self, packet: ClientPacket<S>) -> anyhow::Result<()> {
|
|
|
|
let id_packet = IdClientPacket(self.client_id, packet);
|
|
|
|
let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?;
|
2023-01-31 20:16:23 -06:00
|
|
|
self.socket.send(&bytes)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-31 20:24:06 -06:00
|
|
|
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
2023-02-03 12:36:52 -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<()> {
|
2023-02-03 12:36:52 -06:00
|
|
|
self.send_raw_packet(ClientPacket::Disconnect)?;
|
2023-02-03 12:29:38 -06:00
|
|
|
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-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();
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-02-01 18:40:48 -06:00
|
|
|
}
|
2023-01-31 20:16:23 -06:00
|
|
|
}
|