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.
This commit is contained in:
dongresource 2020-08-27 21:42:04 +02:00
parent 3c43dd0193
commit c8c2f4b05f

View File

@ -17,10 +17,21 @@
#endif #endif
#include <string> #include <string>
CNShardServer *shardServer;
std::thread *shardThread;
void startShard(CNShardServer* server) { void startShard(CNShardServer* server) {
server->start(); 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() { int main() {
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsaData; WSADATA wsaData;
@ -31,6 +42,7 @@ int main() {
#else #else
// tell the OS to not kill us if you use a broken pipe, just let us know thru recv() or send() // 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(SIGPIPE, SIG_IGN);
signal(SIGINT, terminate); // TODO: use sigaction() instead
#endif #endif
settings::init(); settings::init();
std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl; std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl;
@ -46,14 +58,14 @@ int main() {
std::cout << "[INFO] Starting Server Threads..." << std::endl; std::cout << "[INFO] Starting Server Threads..." << std::endl;
CNLoginServer loginServer(settings::LOGINPORT); 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(); loginServer.start();
shardServer.kill(); shardServer->kill();
shardThread.join(); shardThread->join();
#ifdef _WIN32 #ifdef _WIN32
WSACleanup(); WSACleanup();