Commit a631ee1e authored by Sergey Lyubka's avatar Sergey Lyubka

Service code refactored

parent 493666da
...@@ -414,32 +414,29 @@ static HICON hIcon; ...@@ -414,32 +414,29 @@ static HICON hIcon;
static SERVICE_STATUS ss; static SERVICE_STATUS ss;
static SERVICE_STATUS_HANDLE hStatus; static SERVICE_STATUS_HANDLE hStatus;
static const char *service_magic_argument = "--"; static const char *service_magic_argument = "--";
static const char *service_name = "Mongoose";
static NOTIFYICONDATA TrayIcon; static NOTIFYICONDATA TrayIcon;
static void WINAPI ControlHandler(DWORD code) { static void WINAPI ControlHandler(DWORD code) {
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ss.dwServiceType = SERVICE_WIN32;
ss.dwWin32ExitCode = NO_ERROR;
ss.dwCurrentState = SERVICE_RUNNING;
if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) { if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
ss.dwWin32ExitCode = 0;
ss.dwCurrentState = SERVICE_STOPPED; ss.dwCurrentState = SERVICE_STOPPED;
} }
SetServiceStatus(hStatus, &ss); SetServiceStatus(hStatus, &ss);
} }
static void WINAPI ServiceMain(void) { static void WINAPI ServiceMain(void) {
ss.dwServiceType = SERVICE_WIN32; hStatus = RegisterServiceCtrlHandler(service_name, ControlHandler);
ss.dwCurrentState = SERVICE_RUNNING; ControlHandler(SERVICE_CONTROL_INTERROGATE);
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
SetServiceStatus(hStatus, &ss);
while (ss.dwCurrentState == SERVICE_RUNNING) { while (ss.dwCurrentState == SERVICE_RUNNING) {
Sleep(1000); Sleep(1000);
} }
mg_stop(ctx); mg_stop(ctx);
ss.dwCurrentState = SERVICE_STOPPED;
ss.dwWin32ExitCode = (DWORD) -1;
SetServiceStatus(hStatus, &ss);
} }
...@@ -719,7 +716,6 @@ static void show_settings_dialog() { ...@@ -719,7 +716,6 @@ static void show_settings_dialog() {
} }
static int manage_service(int action) { static int manage_service(int action) {
static const char *service_name = "Mongoose";
SC_HANDLE hSCM = NULL, hService = NULL; SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_DESCRIPTION descr = {server_name}; SERVICE_DESCRIPTION descr = {server_name};
char path[PATH_MAX + 20]; // Path to executable plus magic argument char path[PATH_MAX + 20]; // Path to executable plus magic argument
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment