mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-25 16:28:42 -06:00
id packets
This commit is contained in:
parent
19bc67a78f
commit
a1ff9e1d30
|
@ -3,7 +3,11 @@ use std::{
|
|||
marker::PhantomData, time::{Instant, Duration}
|
||||
};
|
||||
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)]
|
||||
pub enum ClientStatus {
|
||||
|
@ -18,19 +22,6 @@ pub struct ClientConfig {
|
|||
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 {
|
||||
addr: SocketAddr,
|
||||
config: ClientConfig,
|
||||
|
@ -69,20 +60,21 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
|||
self.timeout = Instant::now();
|
||||
self.last_heartbeat = Instant::now();
|
||||
self.socket.connect(self.addr)?;
|
||||
self.send_raw_packet(&ClientPacket::Connect)?;
|
||||
self.send_raw_packet(ClientPacket::Connect)?;
|
||||
Ok(())
|
||||
}
|
||||
fn send_raw_packet(&self, packet: &ClientPacket<S>) -> anyhow::Result<()> {
|
||||
let bytes = bincode::encode_to_vec(packet, BINCODE_CONFIG)?;
|
||||
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)?;
|
||||
self.socket.send(&bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn send_message(&self, message: S) -> anyhow::Result<()> {
|
||||
self.send_raw_packet(&ClientPacket::Data(message))?;
|
||||
self.send_raw_packet(ClientPacket::Data(message))?;
|
||||
Ok(())
|
||||
}
|
||||
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.disconnect_reason = DisconnectReason::ClientDisconnected;
|
||||
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 {
|
||||
log::trace!("Sending heartbeat packet");
|
||||
self.send_raw_packet(&ClientPacket::Heartbeat)?;
|
||||
self.send_raw_packet(ClientPacket::Heartbeat)?;
|
||||
self.last_heartbeat = Instant::now();
|
||||
}
|
||||
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(crate) mod server;
|
||||
pub mod client;
|
||||
pub mod server;
|
||||
pub(crate) mod packet;
|
||||
|
||||
pub use server::Server;
|
||||
pub use client::Client;
|
||||
pub(crate) mod common;
|
||||
|
||||
//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()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use bincode::{Encode, Decode};
|
||||
use crate::common::ClientId;
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Encode, Decode)]
|
||||
|
@ -9,6 +10,9 @@ pub enum ClientPacket<T> where T: Encode + Decode {
|
|||
Heartbeat,
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct IdClientPacket<T: Encode + Decode>(pub Option<ClientId>, pub ClientPacket<T>);
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Encode, Decode)]
|
||||
pub enum ServerPacket<T> where T: Encode + Decode {
|
||||
|
|
Loading…
Reference in a new issue