mirror of
				https://github.com/TES3MP/openmw-tes3mp.git
				synced 2025-10-31 14:26:43 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									01804af100
								
							
						
					
					
						commit
						7316c8aafd
					
				
					 1 changed files with 21 additions and 1 deletions
				
			
		|  | @ -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…
	
		Reference in a new issue