forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalsHandles.cpp
More file actions
157 lines (120 loc) · 2.36 KB
/
SignalsHandles.cpp
File metadata and controls
157 lines (120 loc) · 2.36 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
#include "SignalsHandles.h"
void handlerSigTerm(int sig)
{
if (globalServerPtr)
{
globalServerPtr->stopProcess();
}
}
void handlerSigInt(int sig)
{
if (globalServerPtr)
{
globalServerPtr->stopProcess();
}
}
void handlerSigUsr1(int sig)
{
if (globalServerPtr)
{
globalServerPtr->setRestart();
globalServerPtr->stopProcess();
}
}
void handlerSigUsr2(int sig)
{
if (globalServerPtr)
{
globalServerPtr->setUpdateModule();
globalServerPtr->unsetProcess();
globalServerPtr->setProcessQueue();
}
}
#ifdef WIN32
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case SIGTERM:
{
handlerSigTerm(message);
PostQuitMessage(0);
break;
}
case SIGINT:
{
handlerSigInt(message);
PostQuitMessage(0);
break;
}
case SIGUSR1:
{
handlerSigUsr1(message);
break;
}
case SIGUSR2:
{
handlerSigUsr2(message);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return 0;
}
WPARAM mainMessageLoop(HINSTANCE hInstance, HttpServer::Event *pCreatedWindow)
{
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
int bindSignalsHandles(HttpServer::Server *server)
{
globalServerPtr = server;
#ifdef WIN32
signal(SIGINT, handlerSigInt);
signal(SIGTERM, handlerSigTerm);
_set_abort_behavior(0, _WRITE_ABORT_MSG);
HINSTANCE hInstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex = {
sizeof(WNDCLASSEX)
};
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = myWndClassName;
if (0 == RegisterClassEx(&wcex) )
{
return 0;
}
HttpServer::Event createdWindow;
threadMessageLoop = std::thread(mainMessageLoop, hInstance, &createdWindow);
createdWindow.wait();
#elif POSIX
struct sigaction act = {0};
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 1;
}