mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-29 02:08:48 -06:00
id packets
This commit is contained in:
parent
19bc67a78f
commit
a1ff9e1d30
|
@ -3,7 +3,11 @@ use std::{
|
||||||
marker::PhantomData, time::{Instant, Duration}
|
marker::PhantomData, time::{Instant, Duration}
|
||||||
};
|
};
|
||||||
use bincode::{Encode, Decode};
|
use bincode::{Encode, Decode};
|
||||||
use crate::{BINCODE_CONFIG, packet::ClientPacket};
|
use crate::{
|
||||||
|
BINCODE_CONFIG,
|
||||||
|
packet::{ClientPacket, IdClientPacket},
|
||||||
|
common::{ClientId, DisconnectReason}
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum ClientStatus {
|
pub enum ClientStatus {
|
||||||
|
@ -18,19 +22,6 @@ pub struct ClientConfig {
|
||||||
pub heartbeat_interval: Duration,
|
pub heartbeat_interval: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ClientId = u8;
|
|
||||||
|
|
||||||
#[derive(Default, Encode, Decode)]
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum DisconnectReason {
|
|
||||||
#[default]
|
|
||||||
NotConnected,
|
|
||||||
ClientDisconnected,
|
|
||||||
KickedByServer,
|
|
||||||
ClientTimeout,
|
|
||||||
ServerTimeout,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
config: ClientConfig,
|
config: ClientConfig,
|
||||||
|
@ -69,20 +60,21 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
self.timeout = Instant::now();
|
self.timeout = Instant::now();
|
||||||
self.last_heartbeat = Instant::now();
|
self.last_heartbeat = Instant::now();
|
||||||
self.socket.connect(self.addr)?;
|
self.socket.connect(self.addr)?;
|
||||||
self.send_raw_packet(&ClientPacket::Connect)?;
|
self.send_raw_packet(ClientPacket::Connect)?;
|
||||||
Ok(())
|
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 id_packet = IdClientPacket(self.client_id, packet);
|
||||||
|
let bytes = bincode::encode_to_vec(id_packet, BINCODE_CONFIG)?;
|
||||||
self.socket.send(&bytes)?;
|
self.socket.send(&bytes)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
||||||
self.send_raw_packet(&ClientPacket::Data(message))?;
|
self.send_raw_packet(ClientPacket::Data(message))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> {
|
fn disconnect_inner(&mut self, reason: DisconnectReason) -> anyhow::Result<()> {
|
||||||
self.send_raw_packet(&ClientPacket::Disconnect)?;
|
self.send_raw_packet(ClientPacket::Disconnect)?;
|
||||||
self.status = ClientStatus::Disconnected;
|
self.status = ClientStatus::Disconnected;
|
||||||
self.disconnect_reason = DisconnectReason::ClientDisconnected;
|
self.disconnect_reason = DisconnectReason::ClientDisconnected;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -108,7 +100,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
}
|
}
|
||||||
if self.last_heartbeat.elapsed() > self.config.heartbeat_interval {
|
if self.last_heartbeat.elapsed() > self.config.heartbeat_interval {
|
||||||
log::trace!("Sending heartbeat packet");
|
log::trace!("Sending heartbeat packet");
|
||||||
self.send_raw_packet(&ClientPacket::Heartbeat)?;
|
self.send_raw_packet(ClientPacket::Heartbeat)?;
|
||||||
self.last_heartbeat = Instant::now();
|
self.last_heartbeat = Instant::now();
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
15
kubi-udp/src/common.rs
Normal file
15
kubi-udp/src/common.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
use std::num::NonZeroU8;
|
||||||
|
use bincode::{Encode, Decode};
|
||||||
|
|
||||||
|
pub type ClientId = NonZeroU8;
|
||||||
|
|
||||||
|
#[derive(Default, Encode, Decode)]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum DisconnectReason {
|
||||||
|
#[default]
|
||||||
|
NotConnected,
|
||||||
|
ClientDisconnected,
|
||||||
|
KickedByServer,
|
||||||
|
ClientTimeout,
|
||||||
|
ServerTimeout,
|
||||||
|
}
|
|
@ -1,9 +1,7 @@
|
||||||
pub(crate) mod client;
|
pub mod client;
|
||||||
pub(crate) mod server;
|
pub mod server;
|
||||||
pub(crate) mod packet;
|
pub(crate) mod packet;
|
||||||
|
pub(crate) mod common;
|
||||||
pub use server::Server;
|
|
||||||
pub use client::Client;
|
|
||||||
|
|
||||||
//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {}
|
//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {}
|
||||||
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
pub(crate) const BINCODE_CONFIG: bincode::config::Configuration<bincode::config::LittleEndian, bincode::config::Varint, bincode::config::SkipFixedArrayLength> = bincode::config::standard()
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use bincode::{Encode, Decode};
|
use bincode::{Encode, Decode};
|
||||||
|
use crate::common::ClientId;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Encode, Decode)]
|
#[derive(Encode, Decode)]
|
||||||
|
@ -9,6 +10,9 @@ pub enum ClientPacket<T> where T: Encode + Decode {
|
||||||
Heartbeat,
|
Heartbeat,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Encode, Decode)]
|
||||||
|
pub struct IdClientPacket<T: Encode + Decode>(pub Option<ClientId>, pub ClientPacket<T>);
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Encode, Decode)]
|
#[derive(Encode, Decode)]
|
||||||
pub enum ServerPacket<T> where T: Encode + Decode {
|
pub enum ServerPacket<T> where T: Encode + Decode {
|
||||||
|
|
Loading…
Reference in a new issue