Added functionality for TES3MP server to catch system signals

This should permit the TES3MP server to catch when a "SIGTERM"(Standard program stop) or "SIGINT"(Ctrl+C) is sent to the process and allow that to initiate the standard shutdown procedure by halting the networking loop peacefully.
pull/532/head
nalal 5 years ago committed by GitHub
parent 01804af100
commit 7316c8aafd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,7 @@
#include <Script/API/TimerAPI.hpp>
#include <chrono>
#include <thread>
#include <csignal>
#include "Networking.hpp"
#include "MasterClient.hpp"
@ -32,6 +33,7 @@ Networking *Networking::sThis = 0;
static int currentMpNum = 0;
static bool dataFileEnforcementState = true;
static bool scriptErrorIgnoringState = false;
bool killLoop = false;
Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr)
{
@ -469,12 +471,30 @@ void Networking::stopServer(int code)
exitCode = code;
}
void signalHandler(int signum)
{
cout << "Interrupt signal (" << signum << ") received.\n";
//15 is SIGTERM(Normal OS stop call), 2 is SIGINT(Ctrl+C)
if(signum == 15 or signum == 2)
{
killLoop = true;
}
}
int Networking::mainLoop()
{
RakNet::Packet *packet;
while (running)
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = signalHandler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
while (running and !killLoop)
{
sigaction(SIGTERM, &sigIntHandler, NULL);
sigaction(SIGINT, &sigIntHandler, NULL);
if (kbhit() && getch() == '\n')
break;
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())

Loading…
Cancel
Save