forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalHandlers.cpp
More file actions
184 lines (142 loc) · 3.28 KB
/
SignalHandlers.cpp
File metadata and controls
184 lines (142 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "SignalHandlers.h"
#include "System.h"
#ifdef WIN32
#include <Windows.h>
#include <thread>
static std::thread threadMessageLoop;
extern ::TCHAR myWndClassName[];
#endif
#include <csignal>
static HttpServer::Server *globalServerPtr = nullptr;
static void handlerSigTerm(const int sig)
{
if (globalServerPtr)
{
globalServerPtr->stopProcess();
}
}
static void handlerSigInt(const int sig)
{
if (globalServerPtr)
{
globalServerPtr->stopProcess();
}
}
static void handlerSigUsr1(const int sig)
{
if (globalServerPtr)
{
globalServerPtr->setRestart();
globalServerPtr->stopProcess();
}
}
static void handlerSigUsr2(const int sig)
{
if (globalServerPtr)
{
globalServerPtr->setUpdateModule();
globalServerPtr->unsetProcess();
globalServerPtr->setProcessQueue();
}
}
#ifdef WIN32
/**
* Note: PostQuitMessage(0)
* It doesn't work in case the program was launched and was
* attempted to finish under different remote sessions.
*/
static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const ::WPARAM wParam, const ::LPARAM lParam)
{
switch (message)
{
case SIGTERM:
{
handlerSigTerm(message);
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
break;
}
case SIGINT:
{
handlerSigInt(message);
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
break;
}
case SIGUSR1:
{
handlerSigUsr1(message);
break;
}
case SIGUSR2:
{
handlerSigUsr2(message);
break;
}
default:
{
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event *pCreatedWindow)
{
const ::HWND hWnd = ::CreateWindow(myWndClassName, nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, hInstance, nullptr);
pCreatedWindow->notify();
if (0 == hWnd)
{
return 0;
}
::MSG msg;
while (::GetMessage(&msg, hWnd, 0, 0) )
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
#endif
bool bindSignalHandlers(HttpServer::Server *server)
{
globalServerPtr = server;
#ifdef WIN32
const int sig_int = 2; // SIGINT
const int sig_term = 15; // SIGTERM
::signal(sig_int, handlerSigInt);
::signal(sig_term, handlerSigTerm);
::_set_abort_behavior(0, _WRITE_ABORT_MSG);
const ::HINSTANCE hInstance = ::GetModuleHandle(nullptr);
::WNDCLASSEX wcex = {};
wcex.cbSize = sizeof(::WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = myWndClassName;
if (0 == ::RegisterClassEx(&wcex) )
{
return false;
}
HttpServer::Event createdWindow;
threadMessageLoop = std::thread(mainMessageLoop, hInstance, &createdWindow);
createdWindow.wait();
#elif POSIX
struct ::sigaction act = {};
act.sa_handler = handlerSigInt;
::sigaction(SIGINT, &act, nullptr);
act.sa_handler = handlerSigTerm;
::sigaction(SIGTERM, &act, nullptr);
act.sa_handler = handlerSigUsr1;
::sigaction(SIGUSR1, &act, nullptr);
act.sa_handler = handlerSigUsr2;
::sigaction(SIGUSR2, &act, nullptr);
#else
#error "Undefine platform"
#endif
return true;
}
void stopSignalHandlers()
{
#ifdef WIN32
System::sendSignal(::GetCurrentProcessId(), SIGINT);
threadMessageLoop.join();
#endif
}