This commit is contained in:
griffi-gh 2023-02-12 02:33:48 +01:00
parent 22054c6143
commit 00f90156bd
3 changed files with 14 additions and 7 deletions

View file

@ -11,3 +11,6 @@ anyhow = "1.0"
hashbrown = "0.13" hashbrown = "0.13"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
log = "0.4" log = "0.4"
[dev-dependencies]
kubi-logging = { path = "../kubi-logging" }

View file

@ -19,8 +19,7 @@ pub enum DisconnectReason {
NotConnected, NotConnected,
ClientDisconnected, ClientDisconnected,
KickedByServer(Option<String>), KickedByServer(Option<String>),
ClientTimeout, Timeout,
ServerTimeout,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@ -105,6 +104,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
pub fn connect(&mut self) -> Result<()> { pub fn connect(&mut self) -> Result<()> {
log::info!("client connect called");
if self.status != ClientStatus::Disconnected { if self.status != ClientStatus::Disconnected {
bail!("Not Disconnected"); bail!("Not Disconnected");
} }
@ -139,7 +139,7 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
if self.timeout.elapsed() > self.config.timeout { if self.timeout.elapsed() > self.config.timeout {
log::warn!("Client timed out"); log::warn!("Client timed out");
//We don't care if this packet actually gets sent because the server is likely dead //We don't care if this packet actually gets sent because the server is likely dead
let _ = self.disconnect_inner(DisconnectReason::ClientDisconnected, false).map_err(|_| { let _ = self.disconnect_inner(DisconnectReason::Timeout, false).map_err(|_| {
log::warn!("Failed to send disconnect packet"); log::warn!("Failed to send disconnect packet");
}); });
return Ok(()) return Ok(())
@ -162,15 +162,16 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
self.reset_timeout(); self.reset_timeout();
match packet { match packet {
ServerPacket::Connected(client_id) => { ServerPacket::Connected(client_id) => {
log::info!("client connected with id {client_id}");
self.client_id = Some(client_id); self.client_id = Some(client_id);
self.status = ClientStatus::Connected; self.status = ClientStatus::Connected;
self.event_queue.push_back(ClientEvent::Connected(client_id)); self.event_queue.push_back(ClientEvent::Connected(client_id));
return Ok(()) return Ok(())
}, },
ServerPacket::Disconnected(reason) => { ServerPacket::Disconnected(reason) => {
log::info!("client kicked: {reason}");
let reason = DisconnectReason::KickedByServer(Some(reason)); let reason = DisconnectReason::KickedByServer(Some(reason));
//this should never fail but we're handling the error anyway self.disconnect_inner(reason, true)?; //this should never fail but we're handling the error anyway
self.disconnect_inner(reason, true)?;
return Ok(()) return Ok(())
}, },
ServerPacket::Data(message) => { ServerPacket::Data(message) => {

View file

@ -4,7 +4,7 @@ use kubi_udp::{
}; };
use std::{thread, time::Duration}; use std::{thread, time::Duration};
const TEST_ADDR: &str = "127.0.0.1:12345"; const TEST_ADDR: &str = "127.0.0.1:22342";
type CtsMessage = u32; type CtsMessage = u32;
type StcMessage = u64; type StcMessage = u64;
@ -14,6 +14,9 @@ const STC_MSG: StcMessage = 0xdead_beef_cafe_face;
#[test] #[test]
fn test_connection() { fn test_connection() {
//Init logging
kubi_logging::init();
//Create server and client //Create server and client
let mut server: Server<StcMessage, CtsMessage> = Server::bind( let mut server: Server<StcMessage, CtsMessage> = Server::bind(
TEST_ADDR.parse().expect("Invalid TEST_ADDR"), TEST_ADDR.parse().expect("Invalid TEST_ADDR"),
@ -82,6 +85,6 @@ fn test_connection() {
} }
}); });
client_handle.join().unwrap();
server_handle.join().unwrap(); server_handle.join().unwrap();
client_handle.join().unwrap();
} }