mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
Move signal handling into its own translation unit to limit overlap
This commit is contained in:
parent
a2f92b5791
commit
38b934bc0f
@ -82,7 +82,7 @@ add_executable(BeamMP-Server
|
||||
include/TSentry.h src/TSentry.cpp
|
||||
include/TPPSMonitor.h src/TPPSMonitor.cpp
|
||||
include/TNetwork.h src/TNetwork.cpp
|
||||
include/SignalHandling.h)
|
||||
include/SignalHandling.h src/SignalHandling.cpp)
|
||||
|
||||
target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}")
|
||||
|
||||
|
@ -1,68 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
#ifdef __unix
|
||||
#include <csignal>
|
||||
static void UnixSignalHandler(int sig) {
|
||||
switch (sig) {
|
||||
case SIGPIPE:
|
||||
warn("ignoring SIGPIPE");
|
||||
break;
|
||||
case SIGTERM:
|
||||
info("gracefully shutting down via SIGTERM");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
case SIGINT:
|
||||
info("gracefully shutting down via SIGINT");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
default:
|
||||
debug("unhandled signal: " + std::to_string(sig));
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif // __unix
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
// return TRUE if handled, FALSE if not
|
||||
BOOL WINAPI Win32CtrlC_Handler(DWORD CtrlType) {
|
||||
switch (CtrlType) {
|
||||
case CTRL_C_EVENT:
|
||||
info("gracefully shutting down via CTRL+C");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
case CTRL_BREAK_EVENT:
|
||||
info("gracefully shutting down via CTRL+BREAK");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
case CTRL_CLOSE_EVENT:
|
||||
info("gracefully shutting down via close");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
}
|
||||
// we dont care for any others like CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT
|
||||
return FALSE;
|
||||
}
|
||||
#endif // WIN32
|
||||
|
||||
// clang-format off
|
||||
static void SetupSignalHandlers() {
|
||||
// signal handlers for unix
|
||||
#ifdef __unix
|
||||
trace("registering handlers for SIGINT, SIGTERM, SIGPIPE");
|
||||
signal(SIGPIPE, UnixSignalHandler);
|
||||
signal(SIGTERM, UnixSignalHandler);
|
||||
#ifndef DEBUG
|
||||
signal(SIGINT, UnixSignalHandler);
|
||||
#endif // DEBUG
|
||||
#endif // __unix
|
||||
|
||||
// signal handlers for win32
|
||||
#ifdef WIN32
|
||||
trace("registering handlers for CTRL_*_EVENTs");
|
||||
SetConsoleCtrlHandler(Win32CtrlC_Handler, TRUE);
|
||||
#endif // WIN32
|
||||
}
|
||||
// clang-format on
|
||||
void SetupSignalHandlers();
|
||||
|
65
src/SignalHandling.cpp
Normal file
65
src/SignalHandling.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include "SignalHandling.h"
|
||||
#include "Common.h"
|
||||
|
||||
#ifdef __unix
|
||||
#include <csignal>
|
||||
static void UnixSignalHandler(int sig) {
|
||||
switch (sig) {
|
||||
case SIGPIPE:
|
||||
warn("ignoring SIGPIPE");
|
||||
break;
|
||||
case SIGTERM:
|
||||
info("gracefully shutting down via SIGTERM");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
case SIGINT:
|
||||
info("gracefully shutting down via SIGINT");
|
||||
Application::GracefullyShutdown();
|
||||
break;
|
||||
default:
|
||||
debug("unhandled signal: " + std::to_string(sig));
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif // __unix
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
// return TRUE if handled, FALSE if not
|
||||
BOOL WINAPI Win32CtrlC_Handler(DWORD CtrlType) {
|
||||
switch (CtrlType) {
|
||||
case CTRL_C_EVENT:
|
||||
info("gracefully shutting down via CTRL+C");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
case CTRL_BREAK_EVENT:
|
||||
info("gracefully shutting down via CTRL+BREAK");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
case CTRL_CLOSE_EVENT:
|
||||
info("gracefully shutting down via close");
|
||||
Application::GracefullyShutdown();
|
||||
return TRUE;
|
||||
}
|
||||
// we dont care for any others like CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT
|
||||
return FALSE;
|
||||
}
|
||||
#endif // WIN32
|
||||
|
||||
void SetupSignalHandlers() {
|
||||
// signal handlers for unix#include <windows.h>
|
||||
#ifdef __unix
|
||||
trace("registering handlers for SIGINT, SIGTERM, SIGPIPE");
|
||||
signal(SIGPIPE, UnixSignalHandler);
|
||||
signal(SIGTERM, UnixSignalHandler);
|
||||
#ifndef DEBUG
|
||||
signal(SIGINT, UnixSignalHandler);
|
||||
#endif // DEBUG
|
||||
#endif // __unix
|
||||
|
||||
// signal handlers for win32
|
||||
#ifdef WIN32
|
||||
trace("registering handlers for CTRL_*_EVENTs");
|
||||
SetConsoleCtrlHandler(Win32CtrlC_Handler, TRUE);
|
||||
#endif // WIN32
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user