Skip to content

Commit f9dbec6

Browse files
committedJul 16, 2015
Kick players when shutting down server and there is a crash due to a Lua stack exception
1 parent 655fc60 commit f9dbec6

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed
 

Diff for: ‎src/environment.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3939
#include "mapblock_mesh.h"
4040
#include "event.h"
4141
#endif
42+
#include "server.h"
4243
#include "daynightratio.h"
4344
#include "map.h"
4445
#include "emerge.h"
@@ -425,6 +426,15 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
425426
return true;
426427
}
427428

429+
void ServerEnvironment::kickAllPlayers(const std::string &reason)
430+
{
431+
for (std::vector<Player*>::iterator it = m_players.begin();
432+
it != m_players.end();
433+
++it) {
434+
((Server*)m_gamedef)->DenyAccess_Legacy((*it)->peer_id, utf8_to_wide(reason));
435+
}
436+
}
437+
428438
void ServerEnvironment::saveLoadedPlayers()
429439
{
430440
std::string players_path = m_path_world + DIR_DELIM "players";

Diff for: ‎src/environment.h

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class ServerEnvironment : public Environment
221221
float getSendRecommendedInterval()
222222
{ return m_recommended_send_interval; }
223223

224+
void kickAllPlayers(const std::string &reason);
224225
// Save players
225226
void saveLoadedPlayers();
226227
void savePlayer(const std::string &playername);

Diff for: ‎src/server.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,13 @@ Server::~Server()
394394
// Execute script shutdown hooks
395395
m_script->on_shutdown();
396396

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

400-
infostream<<"Server: Saving environment metadata"<<std::endl;
400+
infostream << "Server: kick players" << std::endl;
401+
m_env->kickAllPlayers("Server shutting down...");
402+
403+
infostream << "Server: Saving environment metadata" << std::endl;
401404
m_env->saveMeta();
402405
}
403406

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

0 commit comments

Comments
 (0)
Please sign in to comment.