-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathserver.cc
More file actions
57 lines (46 loc) · 1.38 KB
/
server.cc
File metadata and controls
57 lines (46 loc) · 1.38 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
#include <string.h>
#include <atomic>
#include <workflow/WFHttpServer.h>
// 命令行中用 curl -i "http://127.0.0.1/test"
// https://github.com/sogou/workflow/issues/89
// more elegant way :
int main()
{
WFHttpServer server([&server](WFHttpTask *task) { // 这里的写法相当于一个defer作用
if (strcmp(task->get_req()->get_request_uri(), "/stop") == 0)
{
static std::atomic<int> flag;
if (flag++ == 0) // shutdown只能调用一次,因此我们用了原子变量保护
server.shutdown();
task->get_resp()->append_output_body("<html>server stop</html>");
return;
}
else if (strcmp(task->get_req()->get_request_uri(), "/test") == 0)
{
task->get_resp()->append_output_body("<html>server test</html>");
return;
}
/* Server’s logic */
// ....
});
if (server.start(8888) == 0)
server.wait_finish();
return 0;
}
/*
// WFServer.h
class WFServerBase : protected CommService
{
...
public:
void stop()
{
this->shutdown();
this->wait_finish();
}
void shutdown();
void wait_finish();
};
Server的stop方法无非就是shutdown+wait_finish,上述的方法就是将关停和等待结束分别在两个线程里调用
显然在process里直接调用stop则是一种错误。
*/