mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-06 10:44:08 +00:00
fix: signal handling (#1375)
* fix: signal handling * fix: flush WorldServer logger before main loop * fix: consolidate signal code
This commit is contained in:
@@ -134,7 +134,7 @@ void InstanceManager::RemoveInstance(Instance* instance) {
|
||||
if (m_Instances[i] == instance) {
|
||||
instance->SetShutdownComplete(true);
|
||||
|
||||
if (!Game::shouldShutdown) RedirectPendingRequests(instance);
|
||||
if (!Game::ShouldShutdown()) RedirectPendingRequests(instance);
|
||||
|
||||
delete m_Instances[i];
|
||||
|
||||
|
@@ -47,12 +47,13 @@ namespace Game {
|
||||
InstanceManager* im = nullptr;
|
||||
dConfig* config = nullptr;
|
||||
AssetManager* assetManager = nullptr;
|
||||
bool shouldShutdown = false;
|
||||
Game::signal_t lastSignal = 0;
|
||||
bool universeShutdownRequested = false;
|
||||
std::mt19937 randomEngine;
|
||||
} //namespace Game
|
||||
|
||||
bool shutdownSequenceStarted = false;
|
||||
void ShutdownSequence(int32_t signal = -1);
|
||||
int ShutdownSequence(int32_t signal = -1);
|
||||
int32_t FinalizeShutdown(int32_t signal = -1);
|
||||
Logger* SetupLogger();
|
||||
void HandlePacket(Packet* packet);
|
||||
@@ -73,8 +74,8 @@ int main(int argc, char** argv) {
|
||||
|
||||
//Triggers the shutdown sequence at application exit
|
||||
std::atexit([]() { ShutdownSequence(); });
|
||||
signal(SIGINT, [](int32_t signal) { ShutdownSequence(EXIT_FAILURE); });
|
||||
signal(SIGTERM, [](int32_t signal) { ShutdownSequence(EXIT_FAILURE); });
|
||||
std::signal(SIGINT, Game::OnSignal);
|
||||
std::signal(SIGTERM, Game::OnSignal);
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Game::logger = SetupLogger();
|
||||
@@ -286,7 +287,7 @@ int main(int argc, char** argv) {
|
||||
if (Game::config->GetValue("max_clients") != "") maxClients = std::stoi(Game::config->GetValue("max_clients"));
|
||||
if (Game::config->GetValue("port") != "") ourPort = std::stoi(Game::config->GetValue("port"));
|
||||
|
||||
Game::server = new dServer(Game::config->GetValue("external_ip"), ourPort, 0, maxClients, true, false, Game::logger, "", 0, ServerType::Master, Game::config, &Game::shouldShutdown);
|
||||
Game::server = new dServer(Game::config->GetValue("external_ip"), ourPort, 0, maxClients, true, false, Game::logger, "", 0, ServerType::Master, Game::config, &Game::lastSignal);
|
||||
|
||||
//Query for the database for a server labeled "master"
|
||||
|
||||
@@ -321,7 +322,8 @@ int main(int argc, char** argv) {
|
||||
uint32_t framesSinceLastSQLPing = 0;
|
||||
uint32_t framesSinceKillUniverseCommand = 0;
|
||||
|
||||
while (true) {
|
||||
Game::logger->Flush();
|
||||
while (!Game::ShouldShutdown()) {
|
||||
//In world we'd update our other systems here.
|
||||
|
||||
//Check for packets here:
|
||||
@@ -355,10 +357,10 @@ int main(int argc, char** argv) {
|
||||
framesSinceLastSQLPing++;
|
||||
|
||||
//10m shutdown for universe kill command
|
||||
if (Game::shouldShutdown) {
|
||||
if (Game::universeShutdownRequested) {
|
||||
if (framesSinceKillUniverseCommand >= shutdownUniverseTime) {
|
||||
//Break main loop and exit
|
||||
break;
|
||||
Game::lastSignal = -1;
|
||||
} else
|
||||
framesSinceKillUniverseCommand++;
|
||||
}
|
||||
@@ -402,7 +404,7 @@ int main(int argc, char** argv) {
|
||||
t += std::chrono::milliseconds(masterFrameDelta);
|
||||
std::this_thread::sleep_until(t);
|
||||
}
|
||||
return FinalizeShutdown(EXIT_SUCCESS);
|
||||
return ShutdownSequence(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Logger* SetupLogger() {
|
||||
@@ -799,7 +801,7 @@ void HandlePacket(Packet* packet) {
|
||||
|
||||
case eMasterMessageType::SHUTDOWN_UNIVERSE: {
|
||||
LOG("Received shutdown universe command, shutting down in 10 minutes.");
|
||||
Game::shouldShutdown = true;
|
||||
Game::universeShutdownRequested = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -809,9 +811,11 @@ void HandlePacket(Packet* packet) {
|
||||
}
|
||||
}
|
||||
|
||||
void ShutdownSequence(int32_t signal) {
|
||||
int ShutdownSequence(int32_t signal) {
|
||||
LOG("Recieved Signal %d", signal);
|
||||
if (shutdownSequenceStarted) {
|
||||
return;
|
||||
LOG("Duplicate Shutdown Sequence");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!Game::im) {
|
||||
@@ -820,7 +824,7 @@ void ShutdownSequence(int32_t signal) {
|
||||
|
||||
Game::im->SetIsShuttingDown(true);
|
||||
shutdownSequenceStarted = true;
|
||||
Game::shouldShutdown = true;
|
||||
Game::lastSignal = -1;
|
||||
|
||||
{
|
||||
CBITSTREAM;
|
||||
@@ -889,7 +893,7 @@ void ShutdownSequence(int32_t signal) {
|
||||
}
|
||||
}
|
||||
|
||||
FinalizeShutdown(signal);
|
||||
return FinalizeShutdown(signal);
|
||||
}
|
||||
|
||||
int32_t FinalizeShutdown(int32_t signal) {
|
||||
|
@@ -5,7 +5,7 @@
|
||||
#include "BinaryPathFinder.h"
|
||||
|
||||
void StartChatServer() {
|
||||
if (Game::shouldShutdown) {
|
||||
if (Game::ShouldShutdown()) {
|
||||
LOG("Currently shutting down. Chat will not be restarted.");
|
||||
return;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ void StartChatServer() {
|
||||
}
|
||||
|
||||
void StartAuthServer() {
|
||||
if (Game::shouldShutdown) {
|
||||
if (Game::ShouldShutdown()) {
|
||||
LOG("Currently shutting down. Auth will not be restarted.");
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user