src/​examples/​cpp03/​local/​stream_server.​cppsrc/​examples/​cpp11/​local/​stream_server.​cpp
⋮⋮
1 /​/​1 /​/​
2 /​/​·​stream_server.​cpp2 /​/​·​stream_server.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2022·​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 #include·​<array>
11 #include·​<cstdio>12 #include·​<cstdio>
12 #include·​<iostream>13 #include·​<iostream>
13 #include·​<boost/​array.​hpp>14 #include·​<memory>
14 #include·​<boost/​bind/​bind.​hpp>
15 #include·​<boost/​enable_shared_from_th​is.​hpp>
16 #include·​<boost/​shared_ptr.​hpp>
17 #include·​"asio.​hpp"15 #include·​"asio.​hpp"
18 16
19 #if·​defined(ASIO_HAS_LOCA​L_SOCKETS)​17 #if·​defined(ASIO_HAS_LOCA​L_SOCKETS)​
20 18
21 using·​asio:​:​local:​:​stream_protocol;​19 using·​asio:​:​local:​:​stream_protocol;​
22 20
23 class·​session21 class·​session
24 ·​·​:​·​public·​boost:​:​enable_shared_from_th​is<session>22 ·​·​:​·​public·​std:​:​enable_shared_from_th​is<session>
25 {23 {
26 public:​24 public:​
27 ·​·​session(asio:​:​io_context&·​io_context)​25 ·​·​session(stream_protoc​ol:​:​socket·​sock)​
28 ·​·​·​·​:​·​socket_(io_context)​26 ·​·​·​·​:​·​socket_(std:​:​move(sock)​)​
29 ·​·​{27 ·​·​{
30 ·​·​}28 ·​·​}
31 29
32 ·​·​stream_protocol:​:​socket&·​socket()​
33 ·​·​{
34 ·​·​·​·​return·​socket_;​
35 ·​·​}
36
37 ·​·​void·​start()​30 ·​·​void·​start()​
38 ·​·​{31 ·​·​{
39 ·​·​·​·​socket_.​async_read_some(asio:​:​buffer(data_)​,​32 ·​·​·​·​do_read()​;​
40 ·​·​·​·​·​·​·​·​boost:​:​bind(&session:​:​handle_read,​
41 ·​·​·​·​·​·​·​·​·​·​shared_from_this()​,​
42 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error,​
43 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​bytes_transferred)​)​;​
44 ·​·​}
45
46 ·​·​void·​handle_read(const·​asio:​:​error_code&·​error,​
47 ·​·​·​·​·​·​size_t·​bytes_transferred)​
48 ·​·​{
49 ·​·​·​·​if·​(!error)​
50 ·​·​·​·​{
51 ·​·​·​·​·​·​asio:​:​async_write(socket_,​
52 ·​·​·​·​·​·​·​·​·​·​asio:​:​buffer(data_,​·​bytes_transferred)​,​
53 ·​·​·​·​·​·​·​·​·​·​boost:​:​bind(&session:​:​handle_write,​
54 ·​·​·​·​·​·​·​·​·​·​·​·​shared_from_this()​,​
55 ·​·​·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error)​)​;​
56 ·​·​·​·​}
57 ·​·​}33 ·​·​}
58 34
59 ·​·​void·​handle_write(const·​asio:​:​error_code&·​error)​35 private:​
36 ·​·​void·​do_read()​
60 ·​·​{37 ·​·​{
61 ·​·​·​·​if·​(!error)​38 ·​·​·​·​auto·​self(shared_from_this​()​)​;​
62 ·​·​·​·​{39 ·​·​·​·​socket_.​async_read_some(asio:​:​buffer(data_)​,​
63 ·​·​·​·​·​·​socket_.​async_read_some(asio:​:​buffer(data_)​,​40 ·​·​·​·​·​·​·​·​[this,​·​self](std:​:​error_code·​ec,​·​std:​:​size_t·​length)​
64 ·​·​·​·​·​·​·​·​·​·​boost:​:​bind(&session:​:​handle_read,​41 ·​·​·​·​·​·​·​·​{
65 ·​·​·​·​·​·​·​·​·​·​·​·​shared_from_this()​,​42 ·​·​·​·​·​·​·​·​·​·​if·​(!ec)​
66 ·​·​·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error,​43 ·​·​·​·​·​·​·​·​·​·​·​·​do_write(length)​;​
67 ·​·​·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​bytes_transferred)​)​;​44 ·​·​·​·​·​·​·​·​})​;​
68 ·​·​·​·​}45 ·​·​}
46
47 ·​·​void·​do_write(std:​:​size_t·​length)​
48 ·​·​{
49 ·​·​·​·​auto·​self(shared_from_this​()​)​;​
50 ·​·​·​·​asio:​:​async_write(socket_,​
51 ·​·​·​·​·​·​·​·​asio:​:​buffer(data_,​·​length)​,​
52 ·​·​·​·​·​·​·​·​[this,​·​self](std:​:​error_code·​ec,​·​std:​:​size_t·​/​*length*/​)​
53 ·​·​·​·​·​·​·​·​{
54 ·​·​·​·​·​·​·​·​·​·​if·​(!ec)​
55 ·​·​·​·​·​·​·​·​·​·​·​·​do_read()​;​
56 ·​·​·​·​·​·​·​·​})​;​
69 ·​·​}57 ·​·​}
70 58
71 private:​
72 ·​·​/​/​·​The·​socket·​used·​to·​communicate·​with·​the·​client.​59 ·​·​/​/​·​The·​socket·​used·​to·​communicate·​with·​the·​client.​
73 ·​·​stream_protocol:​:​socket·​socket_;​60 ·​·​stream_protocol:​:​socket·​socket_;​
74 61
75 ·​·​/​/​·​Buffer·​used·​to·​store·​data·​received·​from·​the·​client.​62 ·​·​/​/​·​Buffer·​used·​to·​store·​data·​received·​from·​the·​client.​
76 ·​·​boost:​:​array<char,​·​1024>·​data_;​63 ·​·​std:​:​array<char,​·​1024>·​data_;​
77 };​64 };​
78 65
79 typedef·​boost:​:​shared_ptr<session>·​session_ptr;​
80
81 class·​server66 class·​server
82 {67 {
83 public:​68 public:​
84 ·​·​server(asio:​:​io_context&·​io_context,​·​const·​std:​:​string&·​file)​69 ·​·​server(asio:​:​io_context&·​io_context,​·​const·​std:​:​string&·​file)​
85 ·​·​·​·​:​·​io_context_(io_contex​t)​,​70 ·​·​·​·​:​·​acceptor_(io_context,​·​stream_protocol:​:​endpoint(file)​)​
86 ·​·​·​·​·​·​acceptor_(io_context,​·​stream_protocol:​:​endpoint(file)​)​
87 ·​·​{71 ·​·​{
88 ·​·​·​·​session_ptr·​new_session(new·​session(io_context_)​)​;​72 ·​·​·​·​do_accept()​;​
89 ·​·​·​·​acceptor_.​async_accept(new_sess​ion-​>socket()​,​
90 ·​·​·​·​·​·​·​·​boost:​:​bind(&server:​:​handle_accept,​·​this,​·​new_session,​
91 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error)​)​;​
92 ·​·​}73 ·​·​}
93 74
94 ·​·​void·​handle_accept(session​_ptr·​new_session,​75 private:​
95 ·​·​·​·​·​·​const·​asio:​:​error_code&·​error)​76 ·​·​void·​do_accept()​
96 ·​·​{77 ·​·​{
97 ·​·​·​·​if·​(!error)​78 ·​·​·​·​acceptor_.​async_accept(
98 ·​·​·​·​{79 ·​·​·​·​·​·​·​·​[this](std:​:​error_code·​ec,​·​stream_protocol:​:​socket·​socket)​
99 ·​·​·​·​·​·​new_session-​>start()​;​80 ·​·​·​·​·​·​·​·​{
100 ·​·​·​·​}81 ·​·​·​·​·​·​·​·​·​·​if·​(!ec)​
82 ·​·​·​·​·​·​·​·​·​·​{
83 ·​·​·​·​·​·​·​·​·​·​·​·​std:​:​make_shared<session>(​std:​:​move(socket)​)​-​>start()​;​
84 ·​·​·​·​·​·​·​·​·​·​}
101 85
102 ·​·​·​·​new_session.​reset(new·​session(io_context_)​)​;​86 ·​·​·​·​·​·​·​·​·​·​do_accept()​;​
103 ·​·​·​·​acceptor_.​async_accept(new_sess​ion-​>socket()​,​87 ·​·​·​·​·​·​·​·​})​;​
104 ·​·​·​·​·​·​·​·​boost:​:​bind(&server:​:​handle_accept,​·​this,​·​new_session,​
105 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error)​)​;​
106 ·​·​}88 ·​·​}
107 89
108 private:​
109 ·​·​asio:​:​io_context&·​io_context_;​
110 ·​·​stream_protocol:​:​acceptor·​acceptor_;​90 ·​·​stream_protocol:​:​acceptor·​acceptor_;​
111 };​91 };​
112 92
113 int·​main(int·​argc,​·​char*·​argv[])​93 int·​main(int·​argc,​·​char*·​argv[])​
114 {94 {
115 ·​·​try95 ·​·​try
116 ·​·​{96 ·​·​{
117 ·​·​·​·​if·​(argc·​!=·​2)​97 ·​·​·​·​if·​(argc·​!=·​2)​
118 ·​·​·​·​{98 ·​·​·​·​{
119 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​stream_server·​<file>\n";​99 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​stream_server·​<file>\n";​
120 ·​·​·​·​·​·​std:​:​cerr·​<<·​"***·​WARNING:​·​existing·​file·​is·​removed·​***\n";​100 ·​·​·​·​·​·​std:​:​cerr·​<<·​"***·​WARNING:​·​existing·​file·​is·​removed·​***\n";​
121 ·​·​·​·​·​·​return·​1;​101 ·​·​·​·​·​·​return·​1;​
122 ·​·​·​·​}102 ·​·​·​·​}
123 103
124 ·​·​·​·​asio:​:​io_context·​io_context;​104 ·​·​·​·​asio:​:​io_context·​io_context;​
125 105
126 ·​·​·​·​std:​:​remove(argv[1])​;​106 ·​·​·​·​std:​:​remove(argv[1])​;​
127 ·​·​·​·​server·​s(io_context,​·​argv[1])​;​107 ·​·​·​·​server·​s(io_context,​·​argv[1])​;​
128 108
129 ·​·​·​·​io_context.​run()​;​109 ·​·​·​·​io_context.​run()​;​
130 ·​·​}110 ·​·​}
131 ·​·​catch·​(std:​:​exception&·​e)​111 ·​·​catch·​(std:​:​exception&·​e)​
132 ·​·​{112 ·​·​{
133 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​113 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
134 ·​·​}114 ·​·​}
135 115
136 ·​·​return·​0;​116 ·​·​return·​0;​
137 }117 }
138 118
139 #else·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​119 #else·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​
140 #·​error·​Local·​sockets·​not·​available·​on·​this·​platform.​120 #·​error·​Local·​sockets·​not·​available·​on·​this·​platform.​
141 #endif·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​121 #endif·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​