From d6e0d881136b9a505347aa5214b53a99d7afbc05 Mon Sep 17 00:00:00 2001 From: Lorenzo Mangani Date: Sat, 19 Oct 2024 00:02:15 +0200 Subject: [PATCH] Windows SIGINT handler --- src/httpserver_extension.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/httpserver_extension.cpp b/src/httpserver_extension.cpp index dcf3b2a..c7a4258 100644 --- a/src/httpserver_extension.cpp +++ b/src/httpserver_extension.cpp @@ -388,18 +388,40 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1"); if (run_in_same_thread) { +#ifdef _WIN32 + // Windows-specific handler for Ctrl+C + BOOL WINAPI consoleHandler(DWORD signal) { + if (signal == CTRL_C_EVENT) { + if (global_state.server) { + global_state.server->stop(); + } + global_state.is_running = false; // Update the running state + return TRUE; // Indicate that the signal was handled + } + return FALSE; + } + + // Set the console control handler for Windows + if (!SetConsoleCtrlHandler(consoleHandler, TRUE)) { + std::cerr << "Error setting up signal handler" << std::endl; + throw IOException("Failed to set up signal handler"); + } +#else + // POSIX signal handler for SIGINT (Linux/macOS) signal(SIGINT, [](int) { if (global_state.server) { global_state.server->stop(); } + global_state.is_running = false; // Update the running state }); - +#endif + // Run the server in the same thread if (!global_state.server->listen(host_str.c_str(), port)) { global_state.is_running = false; throw IOException("Failed to start HTTP server on " + host_str + ":" + std::to_string(port)); } - + // The server has stopped (due to CTRL-C or other reasons) global_state.is_running = false; } else {