From c8c2f4b05f0da51d07b8c2a91d947cb43632fb61 Mon Sep 17 00:00:00 2001 From: dongresource Date: Thu, 27 Aug 2020 21:42:04 +0200 Subject: [PATCH] Catch SIGINT with signal(), to allow for gprof instrumentation. Note: signal() is undefined behaviour in multithreaded programs and is unportable for handling signals in general. This will need to be replaced with sigaction() or something. --- src/main.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cd4f46d..ef79250 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,10 +17,21 @@ #endif #include +CNShardServer *shardServer; +std::thread *shardThread; + void startShard(CNShardServer* server) { server->start(); } +// terminate gracefully on SIGINT (for gprof) +void terminate(int arg) { + std::cout << "OpenFusion: terminating" << std::endl; + shardServer->kill(); + shardThread->join(); + exit(0); +} + int main() { #ifdef _WIN32 WSADATA wsaData; @@ -31,6 +42,7 @@ int main() { #else // tell the OS to not kill us if you use a broken pipe, just let us know thru recv() or send() signal(SIGPIPE, SIG_IGN); + signal(SIGINT, terminate); // TODO: use sigaction() instead #endif settings::init(); std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl; @@ -46,14 +58,14 @@ int main() { std::cout << "[INFO] Starting Server Threads..." << std::endl; CNLoginServer loginServer(settings::LOGINPORT); - CNShardServer shardServer(settings::SHARDPORT); + shardServer = new CNShardServer(settings::SHARDPORT); - std::thread shardThread(startShard, (CNShardServer*)&shardServer); + shardThread = new std::thread(startShard, (CNShardServer*)shardServer); loginServer.start(); - shardServer.kill(); - shardThread.join(); + shardServer->kill(); + shardThread->join(); #ifdef _WIN32 WSACleanup();