Add a DisconnectReason enum.

This commit is contained in:
Joshua Yanovski 2020-09-14 08:16:09 +02:00
parent bdff4bc04c
commit 19485b6a00
6 changed files with 43 additions and 25 deletions

View File

@ -23,9 +23,9 @@ use common::{
InventoryManip, InventoryUpdateEvent, InventoryManip, InventoryUpdateEvent,
}, },
msg::{ msg::{
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, InviteAnswer, validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, DisconnectReason,
Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError, ServerInfo, InviteAnswer, Notification, PlayerInfo, PlayerListUpdate, RegisterError, RequestStateError,
ServerMsg, MAX_BYTES_CHAT_MSG, ServerInfo, ServerMsg, MAX_BYTES_CHAT_MSG,
}, },
outcome::Outcome, outcome::Outcome,
recipe::RecipeBook, recipe::RecipeBook,
@ -1110,7 +1110,20 @@ impl Client {
ServerMsg::TooManyPlayers => { ServerMsg::TooManyPlayers => {
return Err(Error::ServerWentMad); 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::InitialSync { .. } => return Err(Error::ServerWentMad),
ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(list)) => { ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(list)) => {
self.player_list = list self.player_list = list
@ -1390,16 +1403,6 @@ impl Client {
error, state 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) => { ServerMsg::CharacterListUpdate(character_list) => {
self.character_list.characters = character_list; self.character_list.characters = character_list;
self.character_list.loading = false; self.character_list.loading = false;

View File

@ -7,8 +7,8 @@ pub use self::{
client::ClientMsg, client::ClientMsg,
ecs_packet::EcsCompPacket, ecs_packet::EcsCompPacket,
server::{ server::{
CharacterInfo, InviteAnswer, Notification, PlayerInfo, PlayerListUpdate, RegisterError, CharacterInfo, DisconnectReason, InviteAnswer, Notification, PlayerInfo, PlayerListUpdate,
RequestStateError, ServerInfo, ServerMsg, RegisterError, RequestStateError, ServerInfo, ServerMsg,
}, },
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -182,6 +182,16 @@ pub enum Notification {
WaypointSaved, 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 /// Messages sent from the server to the client
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ServerMsg { pub enum ServerMsg {
@ -238,9 +248,7 @@ pub enum ServerMsg {
chunk: Result<Box<TerrainChunk>, ()>, chunk: Result<Box<TerrainChunk>, ()>,
}, },
TerrainBlockUpdates(HashMap<Vec3<i32>, Block>), TerrainBlockUpdates(HashMap<Vec3<i32>, Block>),
Disconnect, Disconnect(DisconnectReason),
Kicked(String),
Shutdown,
TooManyPlayers, TooManyPlayers,
/// Send a popup notification such as "Waypoint Saved" /// Send a popup notification such as "Waypoint Saved"
Notification(Notification), Notification(Notification),

View File

@ -9,7 +9,7 @@ use common::{
cmd::{ChatCommand, CHAT_COMMANDS, CHAT_SHORTCUTS}, cmd::{ChatCommand, CHAT_COMMANDS, CHAT_SHORTCUTS},
comp::{self, item::ItemAsset, ChatType, Item, LightEmitter, WaypointArea}, comp::{self, item::ItemAsset, ChatType, Item, LightEmitter, WaypointArea},
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
msg::{Notification, PlayerListUpdate, ServerMsg}, msg::{DisconnectReason, Notification, PlayerListUpdate, ServerMsg},
npc::{self, get_npc_name}, npc::{self, get_npc_name},
state::TimeOfDay, state::TimeOfDay,
sync::{Uid, WorldSyncExt}, sync::{Uid, WorldSyncExt},
@ -1821,7 +1821,10 @@ fn kick_player(server: &mut Server, target_player: EcsEntity, reason: &str) {
.ecs() .ecs()
.read_resource::<EventBus<ServerEvent>>() .read_resource::<EventBus<ServerEvent>>()
.emit_now(ServerEvent::ClientDisconnect(target_player)); .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( fn handle_kick(

View File

@ -34,7 +34,7 @@ use common::{
cmd::ChatCommand, cmd::ChatCommand,
comp::{self, ChatType}, comp::{self, ChatType},
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
msg::{server::WorldMapMsg, ClientState, ServerInfo, ServerMsg}, msg::{server::WorldMapMsg, ClientState, DisconnectReason, ServerInfo, ServerMsg},
outcome::Outcome, outcome::Outcome,
recipe::default_recipe_book, recipe::default_recipe_book,
state::{State, TimeOfDay}, state::{State, TimeOfDay},
@ -817,5 +817,8 @@ impl Server {
} }
impl Drop for 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));
}
} }

View File

@ -11,7 +11,8 @@ use common::{
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
msg::{ msg::{
validate_chat_msg, CharacterInfo, ChatMsgValidationError, ClientMsg, ClientState, 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, span,
state::{BlockChange, Time}, state::{BlockChange, Time},
@ -342,7 +343,7 @@ impl Sys {
ClientMsg::Ping => client.notify(ServerMsg::Pong), ClientMsg::Ping => client.notify(ServerMsg::Pong),
ClientMsg::Pong => {}, ClientMsg::Pong => {},
ClientMsg::Disconnect => { ClientMsg::Disconnect => {
client.notify(ServerMsg::Disconnect); client.notify(ServerMsg::Disconnect(DisconnectReason::Requested));
}, },
ClientMsg::Terminate => { ClientMsg::Terminate => {
debug!(?entity, "Client send message to termitate session"); debug!(?entity, "Client send message to termitate session");