src/​examples/​cpp03/​http/​server/​connection.​hppsrc/​examples/​cpp11/​http/​server/​connection.​hpp
1 /​/​1 /​/​
2 /​/​·​connection.​hpp2 /​/​·​connection.​hpp
3 /​/​·​~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2023·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2023·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​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 #ifndef·​HTTP_CONNECTION_HPP11 #ifndef·​HTTP_CONNECTION_HPP
12 #define·​HTTP_CONNECTION_HPP12 #define·​HTTP_CONNECTION_HPP
13 13
14 #include·​<array>
15 #include·​<memory>
14 #include·​<asio.​hpp>16 #include·​<asio.​hpp>
15 #include·​<boost/​array.​hpp>
16 #include·​<boost/​noncopyable.​hpp>
17 #include·​<boost/​shared_ptr.​hpp>
18 #include·​<boost/​enable_shared_from_th​is.​hpp>
19 #include·​"reply.​hpp"17 #include·​"reply.​hpp"
20 #include·​"request.​hpp"18 #include·​"request.​hpp"
21 #include·​"request_handler.​hpp"19 #include·​"request_handler.​hpp"
22 #include·​"request_parser.​hpp"20 #include·​"request_parser.​hpp"
23 21
24 namespace·​http·​{22 namespace·​http·​{
25 namespace·​server·​{23 namespace·​server·​{
26 24
27 class·​connection_manager;​25 class·​connection_manager;​
28 26
29 /​/​/​·​Represents·​a·​single·​connection·​from·​a·​client.​27 /​/​/​·​Represents·​a·​single·​connection·​from·​a·​client.​
30 class·​connection28 class·​connection
31 ··​:​·​public·boost:​:​enable_shared_from_th​is<connection>,​29 ··​:​·​public·​std:​:​enable_shared_from_th​is<connection>
32 ····private·boost:​:​noncopyable
33 {30 {
34 public:​31 public:​
35 ··/​/​/​·Construct·a·​connection·with·the·given·io_context.​32 ··connection(const·​connection&)​·=·delete;​
36 ··explicit·connection(asio:​:​io_context&·io_context,​33 ··connection&·operator=(const·connection&)​·=·delete;​
34
35 ··​/​/​/​·​Construct·​a·​connection·​with·​the·​given·​socket.​
36 ··​explicit·​connection(asio:​:​ip:​:​tcp:​:​socket·​socket,​
37 ······​connection_manager&·​manager,​·​request_handler&·​handler)​;​37 ······​connection_manager&·​manager,​·​request_handler&·​handler)​;​
38 38
39 ··​/​/​/​·​Get·​the·​socket·​associated·​with·​the·​connection.​
40 ··​asio:​:​ip:​:​tcp:​:​socket&·​socket()​;​
41
42 ··​/​/​/​·​Start·​the·​first·​asynchronous·​operation·​for·​the·​connection.​39 ··​/​/​/​·​Start·​the·​first·​asynchronous·​operation·​for·​the·​connection.​
43 ··​void·​start()​;​40 ··​void·​start()​;​
44 41
45 ··​/​/​/​·​Stop·​all·​asynchronous·​operations·​associated·​with·​the·​connection.​42 ··​/​/​/​·​Stop·​all·​asynchronous·​operations·​associated·​with·​the·​connection.​
46 ··​void·​stop()​;​43 ··​void·​stop()​;​
47 44
48 private:​45 private:​
49 ··​/​/​/​·Handle·completion·of·a·​read·​operation.​46 ··​/​/​/​·Perform·an·asynchronous·​read·​operation.​
50 ··​void·handle_read(const·asio:​:​error_code&·e,​47 ··​void·​do_read()​;​
51 ······std:​:​size_t·bytes_transferred)​;​
52 48
53 ··​/​/​/​·Handle·completion·of·a·​write·​operation.​49 ··​/​/​/​·Perform·an·asynchronous·​write·​operation.​
54 ··​void·handle_write(const·asio:​:​error_code&·e)​;​50 ··​void·​do_write()​;​
55 51
56 ··​/​/​/​·​Socket·​for·​the·​connection.​52 ··​/​/​/​·​Socket·​for·​the·​connection.​
57 ··​asio:​:​ip:​:​tcp:​:​socket·​socket_;​53 ··​asio:​:​ip:​:​tcp:​:​socket·​socket_;​
58 54
59 ··​/​/​/​·​The·​manager·​for·​this·​connection.​55 ··​/​/​/​·​The·​manager·​for·​this·​connection.​
60 ··​connection_manager&·​connection_manager_;​56 ··​connection_manager&·​connection_manager_;​
61 57
62 ··​/​/​/​·​The·​handler·​used·​to·​process·​the·​incoming·​request.​58 ··​/​/​/​·​The·​handler·​used·​to·​process·​the·​incoming·​request.​
63 ··​request_handler&·​request_handler_;​59 ··​request_handler&·​request_handler_;​
64 60
65 ··​/​/​/​·​Buffer·​for·​incoming·​data.​61 ··​/​/​/​·​Buffer·​for·​incoming·​data.​
66 ··boost:​:​array<char,​·​8192>·​buffer_;​62 ··​std:​:​array<char,​·​8192>·​buffer_;​
67 63
68 ··​/​/​/​·​The·​incoming·​request.​64 ··​/​/​/​·​The·​incoming·​request.​
69 ··​request·​request_;​65 ··​request·​request_;​
70 66
71 ··​/​/​/​·​The·​parser·​for·​the·​incoming·​request.​67 ··​/​/​/​·​The·​parser·​for·​the·​incoming·​request.​
72 ··​request_parser·​request_parser_;​68 ··​request_parser·​request_parser_;​
73 69
74 ··​/​/​/​·​The·​reply·​to·​be·​sent·​back·​to·​the·​client.​70 ··​/​/​/​·​The·​reply·​to·​be·​sent·​back·​to·​the·​client.​
75 ··​reply·​reply_;​71 ··​reply·​reply_;​
76 };​72 };​
77 73
78 typedef·boost:​:​shared_ptr<connection​>·​connection_ptr;​74 typedef·​std:​:​shared_ptr<connection​>·​connection_ptr;​
79 75
80 }·​/​/​·​namespace·​server76 }·​/​/​·​namespace·​server
81 }·​/​/​·​namespace·​http77 }·​/​/​·​namespace·​http
82 78
83 #endif·​/​/​·​HTTP_CONNECTION_HPP79 #endif·​/​/​·​HTTP_CONNECTION_HPP