Skip to content

Commit

Permalink
Kick players when shutting down server and there is a crash due to a …
Browse files Browse the repository at this point in the history
…Lua stack exception
  • Loading branch information
nerzhul committed Jul 16, 2015
1 parent 655fc60 commit f9dbec6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/environment.cpp
Expand Up @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock_mesh.h"
#include "event.h"
#endif
#include "server.h"
#include "daynightratio.h"
#include "map.h"
#include "emerge.h"
Expand Down Expand Up @@ -425,6 +426,15 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
return true;
}

void ServerEnvironment::kickAllPlayers(const std::string &reason)
{
for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end();
++it) {
((Server*)m_gamedef)->DenyAccess_Legacy((*it)->peer_id, utf8_to_wide(reason));
}
}

void ServerEnvironment::saveLoadedPlayers()
{
std::string players_path = m_path_world + DIR_DELIM "players";
Expand Down
1 change: 1 addition & 0 deletions src/environment.h
Expand Up @@ -221,6 +221,7 @@ class ServerEnvironment : public Environment
float getSendRecommendedInterval()
{ return m_recommended_send_interval; }

void kickAllPlayers(const std::string &reason);
// Save players
void saveLoadedPlayers();
void savePlayer(const std::string &playername);
Expand Down
8 changes: 6 additions & 2 deletions src/server.cpp
Expand Up @@ -394,10 +394,13 @@ Server::~Server()
// Execute script shutdown hooks
m_script->on_shutdown();

infostream<<"Server: Saving players"<<std::endl;
infostream << "Server: Saving players" << std::endl;
m_env->saveLoadedPlayers();

infostream<<"Server: Saving environment metadata"<<std::endl;
infostream << "Server: kick players" << std::endl;
m_env->kickAllPlayers("Server shutting down...");

infostream << "Server: Saving environment metadata" << std::endl;
m_env->saveMeta();
}

Expand Down Expand Up @@ -499,6 +502,7 @@ void Server::step(float dtime)
throw ServerError(async_err);
}
else {
m_env->kickAllPlayers("The server has crashed. Disconnecting all players. Please reconnect soon...");
errorstream << "UNRECOVERABLE error occurred. Stopping server. "
<< "Please fix the following error:" << std::endl
<< async_err << std::endl;
Expand Down

0 comments on commit f9dbec6

Please sign in to comment.