kubi/kubi-udp/src/client.rs

270 lines
7.9 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-02-11 19:56:50 -06:00
collections::{VecDeque, vec_deque::Drain as DrainDeque},
io::ErrorKind,
2023-01-31 20:16:23 -06:00
};
2023-02-03 12:36:52 -06:00
use crate::{
BINCODE_CONFIG,
2023-02-12 18:53:55 -06:00
packet::{ClientPacket, IdClientPacket, IdServerPacket, ServerPacket, Message},
common::{ClientId, PROTOCOL_ID, DEFAULT_USER_PROTOCOL_ID}
2023-02-03 12:36:52 -06:00
};
2023-01-31 20:16:23 -06:00
#[derive(Default, Clone, Debug)]
2023-02-06 12:19:02 -06:00
#[repr(u8)]
pub enum DisconnectReason {
#[default]
NotConnected,
ClientDisconnected,
KickedByServer(Option<String>),
2023-02-11 19:33:48 -06:00
Timeout,
2023-02-12 15:28:44 -06:00
ConnectionReset,
2023-02-12 18:53:55 -06:00
InvalidProtocolId,
2023-02-06 12:19:02 -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-12 18:53:55 -06:00
pub protocol_id: u16,
2023-02-01 19:13:32 -06:00
pub timeout: Duration,
pub heartbeat_interval: Duration,
2023-02-01 18:40:48 -06:00
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
2023-02-12 18:53:55 -06:00
protocol_id: DEFAULT_USER_PROTOCOL_ID,
timeout: Duration::from_secs(5),
heartbeat_interval: Duration::from_secs(3),
}
}
}
2023-02-01 18:40:48 -06:00
2023-02-12 18:53:55 -06:00
pub enum ClientEvent<T> where T: Message {
Connected(ClientId),
2023-02-05 19:15:19 -06:00
Disconnected(DisconnectReason),
MessageReceived(T)
}
2023-02-12 18:53:55 -06:00
pub struct Client<S, R> where S: Message, R: Message {
2023-02-12 13:37:06 -06:00
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-02-05 19:15:19 -06:00
event_queue: VecDeque<ClientEvent<R>>,
_s: PhantomData<S>,
2023-01-31 20:16:23 -06:00
}
2023-02-12 18:53:55 -06:00
impl<S, R> Client<S, R> where S: Message, R: Message {
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-03 12:39:22 -06:00
pub fn new(addr: SocketAddr, config: ClientConfig) -> Result<Self> {
2023-02-12 18:53:55 -06:00
if config.protocol_id == 0 {
log::warn!("Warning: using 0 as protocol_id is not recommended");
}
if config.protocol_id == DEFAULT_USER_PROTOCOL_ID {
log::warn!("Warning: using default protocol_id is not recommended");
}
2023-02-11 20:15:31 -06:00
let bind_addr: SocketAddr = "0.0.0.0:0".parse().unwrap();
2023-01-31 20:16:23 -06:00
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-02-05 19:15:19 -06:00
event_queue: VecDeque::new(),
2023-01-31 20:24:06 -06:00
_s: 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;
2023-02-05 19:15:19 -06:00
self.event_queue.push_back(ClientEvent::Disconnected(self.disconnect_reason.clone()));
2023-02-03 19:46:48 -06:00
Ok(())
}
fn reset_timeout(&mut self) {
self.timeout = Instant::now();
}
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-03 12:39:22 -06:00
pub fn connect(&mut self) -> Result<()> {
2023-02-12 18:53:55 -06:00
log::info!("Client connecting..");
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-12 18:53:55 -06:00
self.send_raw_packet(ClientPacket::Connect{
user_protocol: self.config.protocol_id,
inner_protocol: PROTOCOL_ID,
})?;
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
2023-02-12 14:42:29 -06:00
#[inline]
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-12 15:38:51 -06:00
#[inline]
pub fn set_nonblocking(&mut self, is_nonblocking: bool) -> Result<()> {
self.socket.set_nonblocking(is_nonblocking)?;
Ok(())
}
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn get_status(&self) -> ClientStatus {
self.status
}
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn is_connected(&self) -> bool {
self.status == ClientStatus::Connected
}
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn is_connecting(&self) -> bool {
self.status == ClientStatus::Connecting
}
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn is_disconnected(&self) -> bool {
self.status == ClientStatus::Disconnected
}
//Return true if the client has not made any connection attempts yet
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn has_not_made_connection_attempts(&self) -> bool {
matches!(self.status, ClientStatus::Disconnected) &&
matches!(self.disconnect_reason, DisconnectReason::NotConnected)
}
2023-02-12 14:42:29 -06:00
#[inline]
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
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-05 19:15:19 -06:00
pub fn update(&mut self) -> Result<()> { // , callback: fn(ClientEvent<R>) -> 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-11 19:33:48 -06:00
let _ = self.disconnect_inner(DisconnectReason::Timeout, 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
2023-02-11 20:22:42 -06:00
let mut buf = [0; u16::MAX as usize];
2023-02-12 13:37:06 -06:00
loop {
match self.socket.recv(&mut buf) {
Ok(length) => {
//TODO check the first byte of the raw data instead of decoding?
let (packet, _): (IdServerPacket<R>, _) = bincode::decode_from_slice(&buf[..length], BINCODE_CONFIG)?;
let IdServerPacket(user_id, packet) = packet;
if self.client_id.map(|x| Some(x) != user_id).unwrap_or_default() {
2023-02-03 19:46:48 -06:00
return Ok(())
}
2023-02-12 13:37:06 -06:00
self.reset_timeout();
match packet {
ServerPacket::Connected(client_id) => {
log::info!("client connected with id {client_id}");
self.client_id = Some(client_id);
self.status = ClientStatus::Connected;
self.event_queue.push_back(ClientEvent::Connected(client_id));
return Ok(())
},
ServerPacket::Disconnected(reason) => {
log::info!("client kicked: {reason}");
let reason = DisconnectReason::KickedByServer(Some(reason));
self.disconnect_inner(reason, true)?; //this should never fail but we're handling the error anyway
return Ok(())
},
ServerPacket::Data(message) => {
self.event_queue.push_back(ClientEvent::MessageReceived(message));
2023-02-12 18:53:55 -06:00
self.timeout = Instant::now();
},
ServerPacket::Heartbeat => {
self.timeout = Instant::now();
},
ServerPacket::ProtoDisconnect => {
let reason = DisconnectReason::InvalidProtocolId;
self.disconnect_inner(reason, true)?; //this should never fail but we're handling the error anyway
return Ok(());
2023-02-12 13:37:06 -06:00
}
}
},
Err(error) if error.kind() != ErrorKind::WouldBlock => {
2023-02-12 15:28:44 -06:00
match error.kind() {
ErrorKind::ConnectionReset => {
log::error!("Connection interrupted");
self.disconnect_inner(DisconnectReason::ConnectionReset, true)?;
},
_ => {
log::error!("IO error {error}");
return Err(error.into());
},
}
2023-02-12 13:37:06 -06:00
},
_ => break,
}
2023-02-03 19:46:48 -06:00
}
2023-02-01 19:13:32 -06:00
Ok(())
2023-02-01 18:40:48 -06:00
}
2023-02-05 19:15:19 -06:00
2023-02-12 14:42:29 -06:00
#[inline]
2023-02-12 13:37:06 -06:00
pub fn pop_event(&mut self) -> Option<ClientEvent<R>> {
2023-02-05 19:15:19 -06:00
self.event_queue.pop_front()
}
2023-02-12 14:42:29 -06:00
#[inline]
pub fn process_events(&mut self) -> DrainDeque<ClientEvent<R>> {
2023-02-05 19:15:19 -06:00
self.event_queue.drain(..)
}
2023-01-31 20:16:23 -06:00
}