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,
},
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;

View File

@ -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};

View File

@ -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<Box<TerrainChunk>, ()>,
},
TerrainBlockUpdates(HashMap<Vec3<i32>, Block>),
Disconnect,
Kicked(String),
Shutdown,
Disconnect(DisconnectReason),
TooManyPlayers,
/// Send a popup notification such as "Waypoint Saved"
Notification(Notification),

View File

@ -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::<EventBus<ServerEvent>>()
.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(

View File

@ -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));
}
}

View File

@ -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");