From aa430416ad672d82eb97b603579e85c4c535c245 Mon Sep 17 00:00:00 2001 From: Joshua Yanovski Date: Mon, 14 Sep 2020 08:16:09 +0200 Subject: [PATCH] Add a DisconnectReason enum. --- client/src/lib.rs | 31 +++++++++++++++++-------------- common/src/msg/mod.rs | 4 ++-- common/src/msg/server.rs | 14 +++++++++++--- server/src/cmd.rs | 7 +++++-- server/src/lib.rs | 7 +++++-- server/src/sys/message.rs | 5 +++-- 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 5300657b31..64a20e35f2 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -23,9 +23,9 @@ use common::{ InventoryManip, InventoryUpdateEvent, }, msg::{ - validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, InviteAnswer, - Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, - ServerMsg, MAX_BYTES_CHAT_MSG, + validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, DisconnectReason, + InviteAnswer, Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, + ServerInfo, ServerMsg, MAX_BYTES_CHAT_MSG, }, outcome::Outcome, recipe::RecipeBook, @@ -1110,7 +1110,20 @@ impl Client { ServerMsg::TooManyPlayers => { return Err(Error::ServerWentMad); }, - ServerMsg::Shutdown => return Err(Error::ServerShutdown), + ServerMsg::Disconnect(reason) => match reason { + DisconnectReason::Shutdown => return Err(Error::ServerShutdown), + DisconnectReason::Requested => { + debug!("finally sending ClientMsg::Terminate"); + frontend_events.push(Event::Disconnect); + self.singleton_stream.send(ClientMsg::Terminate)?; + break Ok(()); + }, + DisconnectReason::Kicked(reason) => { + debug!("sending ClientMsg::Terminate because we got kicked"); + frontend_events.push(Event::Kicked(reason.clone())); + self.singleton_stream.send(ClientMsg::Terminate)?; + }, + }, ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad), ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(list)) => { self.player_list = list @@ -1390,16 +1403,6 @@ impl Client { error, state ); }, - ServerMsg::Disconnect => { - debug!("finally sending ClientMsg::Terminate"); - frontend_events.push(Event::Disconnect); - self.singleton_stream.send(ClientMsg::Terminate)?; - break Ok(()); - }, - ServerMsg::Kicked(reason) => { - frontend_events.push(Event::Kicked(reason.clone())); - self.singleton_stream.send(ClientMsg::Terminate)?; - }, ServerMsg::CharacterListUpdate(character_list) => { self.character_list.characters = character_list; self.character_list.loading = false; diff --git a/common/src/msg/mod.rs b/common/src/msg/mod.rs index 4f2f1585b1..64a6d4be1f 100644 --- a/common/src/msg/mod.rs +++ b/common/src/msg/mod.rs @@ -7,8 +7,8 @@ pub use self::{ client::ClientMsg, ecs_packet::EcsCompPacket, server::{ - CharacterInfo, InviteAnswer, Notification, PlayerInfo, PlayerListUpdate, RegisterError, - RequestStateError, ServerInfo, ServerMsg, + CharacterInfo, DisconnectReason, InviteAnswer, Notification, PlayerInfo, PlayerListUpdate, + RegisterError, RequestStateError, ServerInfo, ServerMsg, }, }; use serde::{Deserialize, Serialize}; diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index a22d71dd9e..18d4ee6f27 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -182,6 +182,16 @@ pub enum Notification { WaypointSaved, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum DisconnectReason { + /// Server shut down + Shutdown, + /// Client sent disconnect message + Requested, + /// Client was kicked + Kicked(String), +} + /// Messages sent from the server to the client #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ServerMsg { @@ -238,9 +248,7 @@ pub enum ServerMsg { chunk: Result, ()>, }, TerrainBlockUpdates(HashMap, Block>), - Disconnect, - Kicked(String), - Shutdown, + Disconnect(DisconnectReason), TooManyPlayers, /// Send a popup notification such as "Waypoint Saved" Notification(Notification), diff --git a/server/src/cmd.rs b/server/src/cmd.rs index d8167a094b..7716be69bd 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -9,7 +9,7 @@ use common::{ cmd::{ChatCommand, CHAT_COMMANDS, CHAT_SHORTCUTS}, comp::{self, item::ItemAsset, ChatType, Item, LightEmitter, WaypointArea}, event::{EventBus, ServerEvent}, - msg::{Notification, PlayerListUpdate, ServerMsg}, + msg::{DisconnectReason, Notification, PlayerListUpdate, ServerMsg}, npc::{self, get_npc_name}, state::TimeOfDay, sync::{Uid, WorldSyncExt}, @@ -1821,7 +1821,10 @@ fn kick_player(server: &mut Server, target_player: EcsEntity, reason: &str) { .ecs() .read_resource::>() .emit_now(ServerEvent::ClientDisconnect(target_player)); - server.notify_client(target_player, ServerMsg::Kicked(reason.to_string())); + server.notify_client( + target_player, + ServerMsg::Disconnect(DisconnectReason::Kicked(reason.to_string())), + ); } fn handle_kick( diff --git a/server/src/lib.rs b/server/src/lib.rs index bea0c8a9c3..9602957082 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -34,7 +34,7 @@ use common::{ cmd::ChatCommand, comp::{self, ChatType}, event::{EventBus, ServerEvent}, - msg::{server::WorldMapMsg, ClientState, ServerInfo, ServerMsg}, + msg::{server::WorldMapMsg, ClientState, DisconnectReason, ServerInfo, ServerMsg}, outcome::Outcome, recipe::default_recipe_book, state::{State, TimeOfDay}, @@ -817,5 +817,8 @@ impl Server { } impl Drop for Server { - fn drop(&mut self) { self.state.notify_registered_clients(ServerMsg::Shutdown); } + fn drop(&mut self) { + self.state + .notify_registered_clients(ServerMsg::Disconnect(DisconnectReason::Shutdown)); + } } diff --git a/server/src/sys/message.rs b/server/src/sys/message.rs index cadaa0c23d..de747c301a 100644 --- a/server/src/sys/message.rs +++ b/server/src/sys/message.rs @@ -11,7 +11,8 @@ use common::{ event::{EventBus, ServerEvent}, msg::{ validate_chat_msg, CharacterInfo, ChatMsgValidationError, ClientMsg, ClientState, - PlayerInfo, PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG, + DisconnectReason, PlayerInfo, PlayerListUpdate, RequestStateError, ServerMsg, + MAX_BYTES_CHAT_MSG, }, span, state::{BlockChange, Time}, @@ -342,7 +343,7 @@ impl Sys { ClientMsg::Ping => client.notify(ServerMsg::Pong), ClientMsg::Pong => {}, ClientMsg::Disconnect => { - client.notify(ServerMsg::Disconnect); + client.notify(ServerMsg::Disconnect(DisconnectReason::Requested)); }, ClientMsg::Terminate => { debug!(?entity, "Client send message to termitate session");