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

View file

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

View file

@ -19,8 +19,7 @@ pub enum DisconnectReason {
NotConnected,
ClientDisconnected,
KickedByServer(Option<String>),
ClientTimeout,
ServerTimeout,
Timeout,
}
#[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<()> {
log::info!("client connect called");
if self.status != ClientStatus::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 {
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, false).map_err(|_| {
let _ = self.disconnect_inner(DisconnectReason::Timeout, false).map_err(|_| {
log::warn!("Failed to send disconnect packet");
});
return Ok(())
@ -162,15 +162,16 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
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));
//this should never fail but we're handling the error anyway
self.disconnect_inner(reason, true)?;
self.disconnect_inner(reason, true)?; //this should never fail but we're handling the error anyway
return Ok(())
},
ServerPacket::Data(message) => {

View file

@ -4,7 +4,7 @@ use kubi_udp::{
};
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 StcMessage = u64;
@ -14,6 +14,9 @@ const STC_MSG: StcMessage = 0xdead_beef_cafe_face;
#[test]
fn test_connection() {
//Init logging
kubi_logging::init();
//Create server and client
let mut server: Server<StcMessage, CtsMessage> = Server::bind(
TEST_ADDR.parse().expect("Invalid TEST_ADDR"),
@ -82,6 +85,6 @@ fn test_connection() {
}
});
client_handle.join().unwrap();
server_handle.join().unwrap();
client_handle.join().unwrap();
}