Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
240 lines (193 loc) · 4.64 KB

File metadata and controls

240 lines (193 loc) · 4.64 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

#include "SignalHandlers.h"
#include "system/System.h"
#ifdef WIN32
#include <Windows.h>
#include <thread>
static DWORD gMainThreadId;
static std::thread gThreadMessageLoop;
#endif
#include <csignal>
static HttpServer::Server *globalServerPtr = nullptr;
/**
* Terminate signal
*/
static void handlerSigTerm(const int) noexcept {
if (globalServerPtr) {
globalServerPtr->stop();
}
}
/**
* Interrupt signal
*/
static void handlerSigInt(const int) noexcept {
if (globalServerPtr) {
globalServerPtr->stop();
}
}
/**
* Signal to restart
*/
static void handlerSigUsr1(const int) noexcept {
if (globalServerPtr) {
globalServerPtr->restart();
}
}
/**
* Signal to update modules
*/
static void handlerSigUsr2(const int) noexcept {
if (globalServerPtr) {
globalServerPtr->update();
}
}
#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
) noexcept {
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;
}
// Cases WM_QUERYENDSESSION and WM_ENDSESSION run before shutting down the system (or ending user session)
case WM_QUERYENDSESSION: {
handlerSigTerm(message);
break;
}
case WM_ENDSESSION: {
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
::WaitForSingleObject(hThread, INFINITE);
::CloseHandle(hThread);
break;
}
default: {
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
static ::WPARAM mainMessageLoop(
const ::HINSTANCE hInstance,
Utils::Event * const eventWindowCreation
) noexcept {
const ::HWND hWnd = ::CreateWindow(
myWndClassName,
nullptr,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
nullptr,
nullptr,
hInstance,
nullptr
);
eventWindowCreation->notify(); // After this action, eventWindowCreation will be destroyed (in the other thread)
if (0 == hWnd) {
return 0;
}
::MSG msg;
while (::GetMessage(&msg, hWnd, 0, 0) ) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
#ifdef _CONSOLE
static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept
{
switch (ctrlType)
{
case CTRL_CLOSE_EVENT:
// Cases CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT don't happen because... it's Windows %)
// @see my function WndProc -> cases WM_QUERYENDSESSION and WM_ENDSESSION. Only they happen in this program, because the library user32.dll is connected.
// @prooflink: https://msdn.microsoft.com/library/windows/desktop/ms686016(v=vs.85).aspx
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT: {
handlerSigTerm(ctrlType);
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
::WaitForSingleObject(hThread, INFINITE);
::CloseHandle(hThread);
return true;
}
case CTRL_C_EVENT: {
handlerSigInt(ctrlType);
return true;
}
default:
return false;
}
}
#endif // _CONSOLE
#endif // WIN32
bool bindSignalHandlers(HttpServer::Server *server) noexcept
{
globalServerPtr = server;
#ifdef WIN32
#ifdef _CONSOLE
::SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(consoleSignalHandler), true);
#elif
::signal(SIGINT, handlerSigInt);
::signal(SIGTERM, handlerSigTerm);
#endif // _CONSOLE
const ::HINSTANCE hInstance = ::GetModuleHandle(nullptr);
::WNDCLASSEX wcex {};
wcex.cbSize = sizeof(::WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = myWndClassName;
if (::RegisterClassEx(&wcex) == 0) {
return false;
}
Utils::Event eventWindowCreation;
gMainThreadId = ::GetCurrentThreadId();
gThreadMessageLoop = std::thread(mainMessageLoop, hInstance, &eventWindowCreation);
eventWindowCreation.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);
::signal(SIGPIPE, SIG_IGN);
#else
#error "Undefined platform"
#endif
return true;
}
void stopSignalHandlers() noexcept
{
#ifdef WIN32
System::sendSignal(::GetCurrentProcessId(), SIGINT);
gThreadMessageLoop.join();
#endif
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.