@@ -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
122142bool 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
0 commit comments