Merge pull request #532 from nalal/patch-1

[Server] Added functionality for TES3MP server to catch system signals
0.7.0
David Cernat 5 years ago committed by GitHub
commit ad9ee80641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,7 @@
#include <Script/API/TimerAPI.hpp> #include <Script/API/TimerAPI.hpp>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <csignal>
#include "Networking.hpp" #include "Networking.hpp"
#include "MasterClient.hpp" #include "MasterClient.hpp"
@ -32,6 +33,7 @@ Networking *Networking::sThis = 0;
static int currentMpNum = 0; static int currentMpNum = 0;
static bool dataFileEnforcementState = true; static bool dataFileEnforcementState = true;
static bool scriptErrorIgnoringState = false; static bool scriptErrorIgnoringState = false;
bool killLoop = false;
Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr) Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr)
{ {
@ -469,12 +471,30 @@ void Networking::stopServer(int code)
exitCode = 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() int Networking::mainLoop()
{ {
RakNet::Packet *packet; 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') if (kbhit() && getch() == '\n')
break; break;
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive()) for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())

Loading…
Cancel
Save