Skip to content

Commit 5d15c75

Browse files
committed
Modified signal processing for Win32
1 parent ba4c4d1 commit 5d15c75

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

src/Module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace HttpServer
8484
if (nullptr == lib_handle)
8585
{
8686
#ifdef POSIX
87-
std::cout << ::dlerror() << std::endl;
87+
std::cout << ::dlerror() << std::endl;
8888
#endif
8989
return false;
9090
}

src/SignalHandlers.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
6262
case SIGTERM:
6363
{
6464
handlerSigTerm(message);
65-
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
65+
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
6666

6767
break;
6868
}
6969

7070
case SIGINT:
7171
{
7272
handlerSigInt(message);
73-
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
73+
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
7474

7575
break;
7676
}
@@ -89,7 +89,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
8989

9090
default:
9191
{
92-
return ::DefWindowProc(hWnd, message, wParam, lParam);
92+
return ::DefWindowProc(hWnd, message, wParam, lParam);
9393
}
9494
}
9595

@@ -107,31 +107,50 @@ static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event *
107107
return 0;
108108
}
109109

110-
::MSG msg;
110+
::MSG msg;
111111

112-
while (::GetMessage(&msg, hWnd, 0, 0) )
112+
while (::GetMessage(&msg, hWnd, 0, 0) )
113113
{
114-
::TranslateMessage(&msg);
115-
::DispatchMessage(&msg);
114+
::TranslateMessage(&msg);
115+
::DispatchMessage(&msg);
116116
}
117117

118118
return msg.wParam;
119119
}
120-
#endif
120+
121+
#ifdef _CONSOLE
122+
static ::BOOL consoleSignalHandler(const ::DWORD ctrlType)
123+
{
124+
switch (ctrlType)
125+
{
126+
case CTRL_CLOSE_EVENT:
127+
handlerSigTerm(ctrlType);
128+
std::this_thread::sleep_for(std::chrono::seconds(60) );
129+
return true;
130+
131+
case CTRL_C_EVENT:
132+
handlerSigInt(ctrlType);
133+
return true;
134+
135+
default:
136+
return false;
137+
}
138+
}
139+
#endif // _CONSOLE
140+
#endif // WIN32
121141

122142
bool bindSignalHandlers(HttpServer::Server *server)
123143
{
124144
globalServerPtr = server;
125145

126146
#ifdef WIN32
127147

128-
const int sig_int = 2; // SIGINT
129-
const int sig_term = 15; // SIGTERM
130-
131-
::signal(sig_int, handlerSigInt);
132-
::signal(sig_term, handlerSigTerm);
133-
134-
::_set_abort_behavior(0, _WRITE_ABORT_MSG);
148+
#ifdef _CONSOLE
149+
::SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(consoleSignalHandler), true);
150+
#elif
151+
::signal(SIGINT, handlerSigInt);
152+
::signal(SIGTERM, handlerSigTerm);
153+
#endif // _CONSOLE
135154

136155
const ::HINSTANCE hInstance = ::GetModuleHandle(nullptr);
137156

@@ -142,7 +161,7 @@ bool bindSignalHandlers(HttpServer::Server *server)
142161
wcex.hInstance = hInstance;
143162
wcex.lpszClassName = myWndClassName;
144163

145-
if (0 == ::RegisterClassEx(&wcex) )
164+
if (0 == ::RegisterClassEx(&wcex) )
146165
{
147166
return false;
148167
}
@@ -158,16 +177,16 @@ bool bindSignalHandlers(HttpServer::Server *server)
158177
struct ::sigaction act = {};
159178

160179
act.sa_handler = handlerSigInt;
161-
::sigaction(SIGINT, &act, nullptr);
180+
::sigaction(SIGINT, &act, nullptr);
162181

163182
act.sa_handler = handlerSigTerm;
164-
::sigaction(SIGTERM, &act, nullptr);
183+
::sigaction(SIGTERM, &act, nullptr);
165184

166185
act.sa_handler = handlerSigUsr1;
167-
::sigaction(SIGUSR1, &act, nullptr);
186+
::sigaction(SIGUSR1, &act, nullptr);
168187

169188
act.sa_handler = handlerSigUsr2;
170-
::sigaction(SIGUSR2, &act, nullptr);
189+
::sigaction(SIGUSR2, &act, nullptr);
171190
#else
172191
#error "Undefine platform"
173192
#endif

src/Utils.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ namespace Utils
396396
{
397397
std::string encoded;
398398

399-
static const std::array<char, 17> hexDigits { "0123456789abcdef" };
399+
static const std::array<char, 17> hexDigits { "0123456789ABCDEF" };
400400

401401
for (size_t i = 0; i < str.length(); ++i)
402402
{
@@ -436,14 +436,12 @@ namespace Utils
436436
{
437437
if (i + 2 < str.length() )
438438
{
439-
const char a = str[i + 1];
440-
const char b = str[i + 2];
439+
const char a = str[++i];
440+
const char b = str[++i];
441441

442442
c = (
443443
(hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b)
444444
);
445-
446-
i += 2;
447445
}
448446
}
449447
else if ('+' == c)

0 commit comments

Comments
 (0)