mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-25 16:28:42 -06:00
disconnect reason
This commit is contained in:
parent
127460347a
commit
19bc67a78f
|
@ -8,3 +8,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bincode = "2.0.0-rc"
|
bincode = "2.0.0-rc"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
nohash-hasher = "0.2.0"
|
||||||
|
log = "0.4"
|
||||||
|
|
|
@ -18,6 +18,19 @@ 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,
|
||||||
|
@ -25,6 +38,8 @@ pub struct Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
status: ClientStatus,
|
status: ClientStatus,
|
||||||
timeout: Instant,
|
timeout: Instant,
|
||||||
last_heartbeat: Instant,
|
last_heartbeat: Instant,
|
||||||
|
client_id: Option<ClientId>,
|
||||||
|
disconnect_reason: DisconnectReason,
|
||||||
_s: PhantomData<*const S>,
|
_s: PhantomData<*const S>,
|
||||||
_r: PhantomData<*const R>,
|
_r: PhantomData<*const R>,
|
||||||
}
|
}
|
||||||
|
@ -40,6 +55,8 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
status: ClientStatus::Disconnected,
|
status: ClientStatus::Disconnected,
|
||||||
timeout: Instant::now(),
|
timeout: Instant::now(),
|
||||||
last_heartbeat: Instant::now(),
|
last_heartbeat: Instant::now(),
|
||||||
|
client_id: None,
|
||||||
|
disconnect_reason: DisconnectReason::default(),
|
||||||
_s: PhantomData,
|
_s: PhantomData,
|
||||||
_r: PhantomData,
|
_r: PhantomData,
|
||||||
})
|
})
|
||||||
|
@ -64,12 +81,17 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
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<()> {
|
||||||
|
self.send_raw_packet(&ClientPacket::Disconnect)?;
|
||||||
|
self.status = ClientStatus::Disconnected;
|
||||||
|
self.disconnect_reason = DisconnectReason::ClientDisconnected;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
pub fn disconnect(&mut self) -> anyhow::Result<()> {
|
pub fn disconnect(&mut self) -> anyhow::Result<()> {
|
||||||
if self.status != ClientStatus::Connected {
|
if self.status != ClientStatus::Connected {
|
||||||
anyhow::bail!("Not Connected");
|
anyhow::bail!("Not Connected");
|
||||||
}
|
}
|
||||||
self.send_raw_packet(&ClientPacket::Disconnect)?;
|
self.disconnect_inner(DisconnectReason::ClientDisconnected)?;
|
||||||
self.status = ClientStatus::Disconnected;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn update(&mut self) -> anyhow::Result<()> {
|
pub fn update(&mut self) -> anyhow::Result<()> {
|
||||||
|
@ -77,12 +99,15 @@ impl<S, R> Client<S, R> where S: Encode + Decode, R: Encode + Decode {
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
if self.timeout.elapsed() > self.config.timeout {
|
if self.timeout.elapsed() > self.config.timeout {
|
||||||
//We don't care if this packet actually gets sent becauseserver is likely dead
|
log::warn!("Client timed out");
|
||||||
let _ = self.send_raw_packet(&ClientPacket::Disconnect);
|
//We don't care if this packet actually gets sent because the server is likely dead
|
||||||
self.status = ClientStatus::Disconnected;
|
let _ = self.disconnect_inner(DisconnectReason::ClientDisconnected).map_err(|_| {
|
||||||
|
log::warn!("Failed to send disconnect packet");
|
||||||
|
});
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
if self.last_heartbeat.elapsed() > self.config.heartbeat_interval {
|
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();
|
self.last_heartbeat = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue