Skip to content

Commit d424a62

Browse files
committed
Fixed signal processing (and termination) for Windows
1 parent e284a06 commit d424a62

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/SignalHandlers.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void handlerSigTerm(const int) noexcept
2121
{
2222
if (globalServerPtr)
2323
{
24-
globalServerPtr->controls.stopProcess();
24+
globalServerPtr->stop();
2525
}
2626
}
2727

@@ -32,7 +32,7 @@ static void handlerSigInt(const int) noexcept
3232
{
3333
if (globalServerPtr)
3434
{
35-
globalServerPtr->controls.stopProcess();
35+
globalServerPtr->stop();
3636
}
3737
}
3838

@@ -43,8 +43,7 @@ static void handlerSigUsr1(const int) noexcept
4343
{
4444
if (globalServerPtr)
4545
{
46-
globalServerPtr->controls.setRestart();
47-
globalServerPtr->controls.stopProcess();
46+
globalServerPtr->restart();
4847
}
4948
}
5049

@@ -55,9 +54,7 @@ static void handlerSigUsr2(const int) noexcept
5554
{
5655
if (globalServerPtr)
5756
{
58-
globalServerPtr->controls.setUpdateModule();
59-
globalServerPtr->controls.setProcess(false);
60-
globalServerPtr->controls.setProcessQueue();
57+
globalServerPtr->update();
6158
}
6259
}
6360

@@ -108,7 +105,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
108105

109106
case WM_ENDSESSION:
110107
{
111-
::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
108+
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
112109
::WaitForSingleObject(hThread, INFINITE);
113110
::CloseHandle(hThread);
114111
break;
@@ -158,7 +155,7 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept
158155
case CTRL_SHUTDOWN_EVENT:
159156
{
160157
handlerSigTerm(ctrlType);
161-
::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
158+
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
162159
::WaitForSingleObject(hThread, INFINITE);
163160
::CloseHandle(hThread);
164161
return true;
@@ -190,7 +187,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept
190187

191188
const ::HINSTANCE hInstance = ::GetModuleHandle(nullptr);
192189

193-
::WNDCLASSEX wcex = {};
190+
::WNDCLASSEX wcex {};
194191

195192
wcex.cbSize = sizeof(::WNDCLASSEX);
196193
wcex.lpfnWndProc = WndProc;
@@ -211,7 +208,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept
211208

212209
#elif POSIX
213210

214-
struct ::sigaction act = {};
211+
struct ::sigaction act {};
215212

216213
act.sa_handler = handlerSigInt;
217214
::sigaction(SIGINT, &act, nullptr);

src/server/Server.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,36 @@ namespace HttpServer
10091009
return code;
10101010
}
10111011

1012+
static void close_liseners(std::vector<Socket::Socket> &liseners)
1013+
{
1014+
for (auto &sock : liseners)
1015+
{
1016+
sock.close();
1017+
}
1018+
}
1019+
1020+
void Server::stop()
1021+
{
1022+
this->controls.stopProcess();
1023+
1024+
close_liseners(this->liseners);
1025+
}
1026+
1027+
void Server::restart()
1028+
{
1029+
this->controls.setRestart();
1030+
this->controls.stopProcess();
1031+
1032+
close_liseners(this->liseners);
1033+
}
1034+
1035+
void Server::update()
1036+
{
1037+
this->controls.setUpdateModule();
1038+
this->controls.setProcess(false);
1039+
this->controls.setProcessQueue();
1040+
}
1041+
10121042
System::native_processid_type Server::getServerProcessId(const std::string &serverName)
10131043
{
10141044
System::native_processid_type pid = 0;

src/server/Server.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ namespace HttpServer
5353
public:
5454
Server() = default;
5555

56+
void stop();
57+
void restart();
58+
void update();
59+
5660
int command_help(const int argc, const char *argv[]) const;
5761
int command_start(const int argc, const char *argv[]);
5862
int command_restart(const int argc, const char *argv[]) const;

0 commit comments

Comments
 (0)