src/​examples/​cpp03/​http/​server/​server.​cpp | src/​examples/​cpp11/​http/​server/​server.​cpp
--- | ---
⋮ | ⋮
1 | /​/​ | 1 | /​/​
2 | /​/​·​server.​cpp | 2 | /​/​·​server.​cpp
3 | /​/​·​~~~~~~~~~~ | 3 | /​/​·​~~~~~~~~~~
4 | /​/​ | 4 | /​/​
5 | /​/​·​Copyright·​(c)​·​2003-​2021·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​ | 5 | /​/​·​Copyright·​(c)​·​2003-​2021·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 | /​/​ | 6 | /​/​
7 | /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying | 7 | /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying
8 | /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​ | 8 | /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 | /​/​ | 9 | /​/​
10 |  | 10 |
11 | #include·​"server.​hpp" | 11 | #include·​"server.​hpp"
12 | #include·​<boost/​bind/​bind.​hpp> |
13 | #include·​<signal.​h> | 12 | #include·​<signal.​h>
| 13 | #include·​<utility>
14 |  | 14 |
15 | namespace·​http·​{ | 15 | namespace·​http·​{
16 | namespace·​server·​{ | 16 | namespace·​server·​{
17 |  | 17 |
18 | server:​:​server(const·​std:​:​string&·​address,​·​const·​std:​:​string&·​port,​ | 18 | server:​:​server(const·​std:​:​string&·​address,​·​const·​std:​:​string&·​port,​
19 | ·​·​·​·​const·​std:​:​string&·​doc_root)​ | 19 | ·​·​·​·​const·​std:​:​string&·​doc_root)​
20 | ·​·​:​·​io_context_()​,​ | 20 | ·​·​:​·​io_context_(1)​,​
21 | ·​·​·​·​signals_(io_context_)​,​ | 21 | ·​·​·​·​signals_(io_context_)​,​
22 | ·​·​·​·​acceptor_(io_context_​)​,​ | 22 | ·​·​·​·​acceptor_(io_context_​)​,​
23 | ·​·​·​·​connection_manager_()​,​ | 23 | ·​·​·​·​connection_manager_()​,​
24 | ·​·​·​·​new_connection_()​,​ |
25 | ·​·​·​·​request_handler_(doc_​root)​ | 24 | ·​·​·​·​request_handler_(doc_​root)​
26 | { | 25 | {
27 | ·​·​/​/​·​Register·​to·​handle·​the·​signals·​that·​indicate·​when·​the·​server·​should·​exit.​ | 26 | ·​·​/​/​·​Register·​to·​handle·​the·​signals·​that·​indicate·​when·​the·​server·​should·​exit.​
28 | ·​·​/​/​·​It·​is·​safe·​to·​register·​for·​the·​same·​signal·​multiple·​times·​in·​a·​program,​ | 27 | ·​·​/​/​·​It·​is·​safe·​to·​register·​for·​the·​same·​signal·​multiple·​times·​in·​a·​program,​
29 | ·​·​/​/​·​provided·​all·​registration·​for·​the·​specified·​signal·​is·​made·​through·​Asio.​ | 28 | ·​·​/​/​·​provided·​all·​registration·​for·​the·​specified·​signal·​is·​made·​through·​Asio.​
30 | ·​·​signals_.​add(SIGINT)​;​ | 29 | ·​·​signals_.​add(SIGINT)​;​
31 | ·​·​signals_.​add(SIGTERM)​;​ | 30 | ·​·​signals_.​add(SIGTERM)​;​
32 | #if·​defined(SIGQUIT)​ | 31 | #if·​defined(SIGQUIT)​
33 | ·​·​signals_.​add(SIGQUIT)​;​ | 32 | ·​·​signals_.​add(SIGQUIT)​;​
34 | #endif·​/​/​·​defined(SIGQUIT)​ | 33 | #endif·​/​/​·​defined(SIGQUIT)​
35 | ·​·​s i g n a l s_.​a s y n c_w a i t(b o o s t:​:​b i n d(&s e r v e r:​:​h a n d l e_s t o p,​·​t h i s)​)​;​ | 34 |
| 35 | ·​·​do_await_stop()​;​
36 |  | 36 |
37 | ·​·​/​/​·​Open·​the·​acceptor·​with·​the·​option·​to·​reuse·​the·​address·​(i.​e.​·​SO_REUSEADDR)​.​ | 37 | ·​·​/​/​·​Open·​the·​acceptor·​with·​the·​option·​to·​reuse·​the·​address·​(i.​e.​·​SO_REUSEADDR)​.​
38 | ·​·​asio:​:​ip:​:​tcp:​:​resolver·​resolver(io_context_)​;​ | 38 | ·​·​asio:​:​ip:​:​tcp:​:​resolver·​resolver(io_context_)​;​
39 | ·​·​asio:​:​ip:​:​tcp:​:​endpoint·​endpoint·​= | 39 | ·​·​asio:​:​ip:​:​tcp:​:​endpoint·​endpoint·​=
40 | ·​·​·​·​*resolver.​resolve(address,​·​port)​.​begin()​;​ | 40 | ·​·​·​·​*resolver.​resolve(address,​·​port)​.​begin()​;​
41 | ·​·​acceptor_.​open(endpoint.​protocol()​)​;​ | 41 | ·​·​acceptor_.​open(endpoint.​protocol()​)​;​
42 | ·​·​acceptor_.​set_option(asio:​:​ip:​:​tcp:​:​acceptor:​:​reuse_address(true)​)​;​ | 42 | ·​·​acceptor_.​set_option(asio:​:​ip:​:​tcp:​:​acceptor:​:​reuse_address(true)​)​;​
43 | ·​·​acceptor_.​bind(endpoint)​;​ | 43 | ·​·​acceptor_.​bind(endpoint)​;​
44 | ·​·​acceptor_.​listen()​;​ | 44 | ·​·​acceptor_.​listen()​;​
45 |  | 45 |
46 | ·​·​s t a r t_accept()​;​ | 46 | ·​·​d o_accept()​;​
47 | } | 47 | }
48 |  | 48 |
49 | void·​server:​:​run()​ | 49 | void·​server:​:​run()​
50 | { | 50 | {
51 | ·​·​/​/​·​The·​io_context:​:​run()​·​call·​will·​block·​until·​all·​asynchronous·​operations | 51 | ·​·​/​/​·​The·​io_context:​:​run()​·​call·​will·​block·​until·​all·​asynchronous·​operations
52 | ·​·​/​/​·​have·​finished.​·​While·​the·​server·​is·​running,​·​there·​is·​always·​at·​least·​one | 52 | ·​·​/​/​·​have·​finished.​·​While·​the·​server·​is·​running,​·​there·​is·​always·​at·​least·​one
53 | ·​·​/​/​·​asynchronous·​operation·​outstanding:​·​the·​asynchronous·​accept·​call·​waiting | 53 | ·​·​/​/​·​asynchronous·​operation·​outstanding:​·​the·​asynchronous·​accept·​call·​waiting
54 | ·​·​/​/​·​for·​new·​incoming·​connections.​ | 54 | ·​·​/​/​·​for·​new·​incoming·​connections.​
55 | ·​·​io_context_.​run()​;​ | 55 | ·​·​io_context_.​run()​;​
56 | } | 56 | }
57 |  | 57 |
58 | void·​server:​:​s t a r t_accept()​ | 58 | void·​server:​:​d o_accept()​
59 | { |
60 | ·​·​n e w_c o n n e c t i o n_.​r e s e t(n e w·​c o n n e c t i o n(i o_c o n t e x t​_,​ |
61 | ·​·​·​·​·​·​·​·​c o n n e c t i o n_m a n a g e r_,​·​r e q u e s t_h a n d l e r_)​)​;​ |
62 | ·​·​a c c e p t o r_.​a s y n c_a c c e p t(n e w_c o n n​e c t i o n_-​>s o c k e t()​,​ |
63 | ·​·​·​·​·​·​b o o s t:​:​b i n d(&s e r v e r:​:​h a n d l e_a c c e p t,​·​t h i s,​ |
64 | ·​·​·​·​·​·​·​·​a s i o:​:​p l a c e h o l d e r s:​:​e r r o r)​)​;​ |
65 | } |
66 |  |
67 | v o i d·​s e r v e r:​:​h a n d l e_a c c e p t(c o n s t·​a s i o:​:​e r r o r_c o d e&·​e)​ |
68 | { | 59 | {
69 | ·​·​/​/​·​C h eck·​w h e t her·​th e·​s e r v er·​was·​st o p p e d·​by·​a·​s i gna l·​b e f o r e·​t h i s·​co mpl eti o n | 60 | ·​·​acceptor_.​async_acc ept(
70 | ·​·​/​/​·​ha ndler·​h a d·​a·​ch a nce·​to·​r u n.​ | 61 | ·​·​·​·​·​·​[thi s](s td:​:​er r or_c o d e·​e c,​·​as i o:​:​i p:​:​tcp:​:​s ocket·​soc k e t)​
71 | ·​·​i f·​(!a c c e p t o r_.​i s_o p e n()​)​ | 62 | ·​·​·​·​·​·​{
72 | ·​·​{ | 63 | ·​·​·​·​·​·​·​·​/​/​·​C h e c k·​w h e t h e r·​t h e·​s e r v e r·​w a s·​s t o p p e d·​b y·​a·​s i g n a l·​b e f o r e·​t h i s
73 | ·​·​·​·​return;​ | 64 | ·​·​·​·​·​·​·​·​/​/​·​c o m p l e t i o n·​h a n d l er·​h a d·​a·​c h a n ce·​to·​run.​
74 | ·​·​} | 65 | ·​·​·​·​·​·​·​·​i f·​(!a c c e p t o r_.​i s_o p e n()​)​
75 |  | 66 | ·​·​·​·​·​·​·​·​{
76 | ·​·​i f·​(!e)​ | 67 | ·​·​·​·​·​·​·​·​·​·​ret u r n;​
77 | ·​·​{ | 68 | ·​·​·​·​·​·​·​·​}
78 | ·​·​·​·​c o n n e c t i o n_m a n a g e r_.​s t a r t(n e w_c o n n e c t i o n_​)​;​ | 69 |
79 | ·​·​} | 70 | ·​·​·​·​·​·​·​·​i f·​(!e c)​
| 71 | ·​·​·​·​·​·​·​·​{
| 72 | ·​·​·​·​·​·​·​·​·​·​connection_manager_.​start(std:​:​make_shared<connectio​n>(
| 73 | ·​·​·​·​·​·​·​·​·​·​·​·​·​·​std:​:​move(socket)​,​·​connection_manager_,​·​request_handler_)​)​;​
| 74 | ·​·​·​·​·​·​·​·​}
80 |  | 75 |
81 | ·​·​s t a r t_accept()​;​ | 76 | ·​·​·​·​·​·​·​·​d o_accept()​;​
| 77 | ·​·​·​·​·​·​})​;​
82 | } | 78 | }
83 |  | 79 |
84 | void·​server:​:​han d l e_stop()​ | 80 | void·​server:​:​d o_aw a i t_stop()​
85 | { | 81 | {
86 | ·​·​/​/​·​T h e·​s e r v e r·​i s·​st o p p e d·​b y·​c a n c e l ling·​all·​o u tst a n d i n g·​asynch r o n o u s | 82 | ·​·​signals_.​async_w a i t(
87 | ·​·​/​/​·​o p e r ati o ns.​·​O n c e·​a l l·​o pera t ion s·​h a ve·​f i n i s hed·​t h e·​io_c onte x t:​:​r un()​·​c a l l | 83 | ·​·​·​·​·​·​[th is](s t d:​:​error_c o de·​/​*ec*/​,​·​int·​/​*s i gno*/​)​
88 | ·​·​/​/​·​w i l l·​e x i t.​ | 84 | ·​·​·​·​·​·​{
89 | ·​·​a c ceptor_.​close()​;​ | 85 | ·​·​·​·​·​·​·​·​/​/​·​T h e·​s e r ver·​i s·​stop p e d·​b y·​c a nce l l i n g·​a ll·​ou t s t a n d i n g·​asy n c h r o n o u s
90 | ·​·​connection_mana ger_.​sto p_a l l()​;​ | 86 | ·​·​·​·​·​·​·​·​/​/​·​o p e r a t ions.​·​Onc e·​a l l·​o per ations·​hav e·​f ini s h e d·​t he·​i o_c o nte x t:​:​r u n()​
| 87 | ·​·​·​·​·​·​·​·​/​/​·​call·​will·​exit.​
| 88 | ·​·​·​·​·​·​·​·​acceptor_.​close()​;​
| 89 | ·​·​·​·​·​·​·​·​connection_manager_.​stop_all()​;​
| 90 | ·​·​·​·​·​·​})​;​
91 | } | 91 | }
92 |  | 92 |
93 | }·​/​/​·​namespace·​server | 93 | }·​/​/​·​namespace·​server
94 | }·​/​/​·​namespace·​http | 94 | }·​/​/​·​namespace·​http