-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.h
273 lines (221 loc) · 5.88 KB
/
service.h
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#pragma once
class service
{
public:
service();
int run();
protected:
static service* instance;
HANDLE stop_event;
HANDLE pause_event;
HANDLE continue_event;
HANDLE worker_paused;
HANDLE worker_continued;
void confirm_pause() { SetEvent(worker_paused); }
void confirm_continue() { SetEvent(worker_continued); }
private:
static void WINAPI service_main();
static void WINAPI control_handler(DWORD code);
static DWORD WINAPI worker_thread(LPVOID lpParam);
SERVICE_STATUS status;
SERVICE_STATUS_HANDLE status_handle;
void startup();
void exit();
void error();
void control_stop();
void control_pause();
void control_continue();
void control_stop_on_pause();
void set_state(DWORD state);
void set_controls_accepted(bool need);
private:
virtual DWORD WINAPI worker(LPVOID)
{
return ERROR_SUCCESS;
}
virtual void on_startup() {};
virtual void on_pause() {};
virtual void on_continue() {};
virtual void on_stop() {};
virtual void on_exit() {};
};
service* service::instance;
service::service()
{
instance = this;
status = {};
ZeroMemory(&status, sizeof(status));
status_handle = NULL;
stop_event = INVALID_HANDLE_VALUE;
pause_event = INVALID_HANDLE_VALUE;
continue_event = INVALID_HANDLE_VALUE;
}
wchar_t service_name[] = L"test";
int service::run()
{
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{service_name, (LPSERVICE_MAIN_FUNCTION)service_main},
{NULL, NULL}
};
//StartServiceCtrlDispatcher - ïîäêëþ÷àåò ïîòîê main ïðîöåññà ñëóæáû ê äèñïåò÷åðó óïðàâëåíèÿ ñëóæáàìè, ÷òî ïðèâîäèò ê òîìó, ÷òî ïîòîê áóäåò ïîòîêîì äèñïåò÷åðà óïðàâëåíèÿ ñëóæáîé äëÿ âûçûâàþùåãî ïðîöåññà.
if (!StartServiceCtrlDispatcher(ServiceTable))
logger::write_log("StartServiceCtrlDispatcher error:", GetLastError());
return ERROR_SUCCESS;
}
void WINAPI service::service_main()
{
//RegisterServiceCtrlHandler - ðåãèñòðèðóåò ôóíêöèþ äëÿ îáðàáîòêè çàïðîñîâ óïðàâëåíèÿ ñëóæáîé
instance->status_handle = RegisterServiceCtrlHandler(service_name, control_handler);
if (!instance->status_handle)
return;
instance->startup();
//ñîçäà¸ì îñíîâíîé ïîòîê â êîòîðîì áóäåì ðàáîòàòü
HANDLE hThread = CreateThread(NULL, 0, worker_thread, NULL, 0, NULL);
//áëîêèðóåv îñíîâíîé ïîòîê äî òåõ ïîð, ïîêà íå çàâåðøèòñÿ ðàáîòà worker_thread
WaitForSingleObject(hThread, INFINITE);
instance->exit();
}
void WINAPI service::control_handler(DWORD code)
{
//îáðàáàòûâàåì êîäû óïðàâëåíèÿ ñëóæáàì, íóæíî ÷òî áû ìîæíî áûëî îñòàíîâèòü ñëóæáó èòä.
switch (code)
{
case SERVICE_CONTROL_STOP:
{
if (instance->status.dwCurrentState == SERVICE_PAUSED)
{
instance->control_stop_on_pause();
break;
}
if (instance->status.dwCurrentState != SERVICE_RUNNING)
break;
instance->control_stop();
break;
}
case SERVICE_CONTROL_PAUSE:
{
if (instance->status.dwCurrentState != SERVICE_RUNNING)
break;
instance->control_pause();
break;
}
case SERVICE_CONTROL_CONTINUE:
{
if (instance->status.dwCurrentState != SERVICE_PAUSED)
break;
instance->control_continue();
break;
}
case SERVICE_CONTROL_SHUTDOWN:
{
if (instance->status.dwCurrentState != SERVICE_RUNNING)
break;
instance->control_stop();
break;
}
case SERVICE_CONTROL_INTERROGATE: //íóæíî ÷òî áû äèñïåò÷åð óïðàâëåíèÿ ñëóæáàìè ïîëó÷àë îáíîâëåííîå ñîñòîÿíèå ñëóæáû
{
SetServiceStatus(instance->status_handle, &instance->status);
break;
}
default:
break;
}
}
/*Internal functions*/
void service::startup()
{
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwServiceSpecificExitCode = 0;
set_controls_accepted(false);
set_state(SERVICE_START_PENDING);
on_startup();
stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
pause_event = CreateEvent(NULL, TRUE, FALSE, NULL);
continue_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!stop_event || !pause_event || !continue_event)
{
error();
return;
}
set_controls_accepted(true);
set_state(SERVICE_RUNNING);
}
void service::exit()
{
on_exit();
CloseHandle(stop_event);
CloseHandle(pause_event);
CloseHandle(continue_event);
CloseHandle(worker_paused);
CloseHandle(worker_continued);
set_state(SERVICE_STOPPED);
}
void service::error()
{
logger::write_log("Error:", GetLastError());
set_controls_accepted(false);
status.dwCurrentState = SERVICE_STOPPED;
status.dwWin32ExitCode = GetLastError();
status.dwCheckPoint = 0;
if (!SetServiceStatus(status_handle, &status))
logger::write_log("SetServiceStatus error:", GetLastError());
}
/*Controls*/
void service::control_stop()
{
set_state(SERVICE_STOP_PENDING);
on_stop();
set_controls_accepted(false);
SetEvent(stop_event);
}
void service::control_pause()
{
set_state(SERVICE_PAUSE_PENDING);
on_pause();
SetEvent(pause_event);
WaitForSingleObject(worker_paused, INFINITE);
set_state(SERVICE_PAUSED);
}
void service::control_continue()
{
set_state(SERVICE_CONTINUE_PENDING);
set_controls_accepted(false);
ResetEvent(pause_event);
on_continue();
SetEvent(continue_event);
WaitForSingleObject(worker_continued, INFINITE);
set_controls_accepted(true);
ResetEvent(continue_event);
set_state(SERVICE_RUNNING);
}
void service::control_stop_on_pause()
{
set_controls_accepted(false);
set_state(SERVICE_STOP_PENDING);
on_continue();
SetEvent(stop_event);
SetEvent(continue_event);
}
void service::set_controls_accepted(bool need)
{
status.dwControlsAccepted = 0;
if (need)
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
if (!SetServiceStatus(status_handle, &status))
logger::write_log("SetServiceStatus error:", GetLastError());
}
void service::set_state(DWORD state)
{
status.dwCurrentState = state;
status.dwWin32ExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
if (!SetServiceStatus(status_handle, &status))
logger::write_log("SetServiceStatus error:", GetLastError());
}
DWORD WINAPI service::worker_thread(LPVOID param)
{
return instance->worker(param);
}