src/​examples/​cpp03/​timeouts/​blocking_token_tcp_cl​ient.​cppsrc/​examples/​cpp11/​timeouts/​blocking_token_tcp_cl​ient.​cpp
1 /​/​1 /​/​
2 /​/​·​blocking_token_tcp_cl​ient.​cpp2 /​/​·​blocking_token_tcp_cl​ient.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2018·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2018·​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·​"asio/​connect.​hpp"11 #include·​"asio/​connect.​hpp"
12 #include·​"asio/​io_context.​hpp"12 #include·​"asio/​io_context.​hpp"
13 #include·​"asio/​ip/​tcp.​hpp"13 #include·​"asio/​ip/​tcp.​hpp"
14 #include·​"asio/​read_until.​hpp"14 #include·​"asio/​read_until.​hpp"
15 #include·​"asio/​streambuf.​hpp"15 #include·​"asio/​streambuf.​hpp"
16 #include·​"asio/​system_error.​hpp"16 #include·​"asio/​system_error.​hpp"
17 #include·​"asio/​write.​hpp"17 #include·​"asio/​write.​hpp"
18 #include·​<cstdlib>18 #include·​<cstdlib>
19 #include·​<iostream>19 #include·​<iostream>
20 #include·​<memory>20 #include·​<memory>
21 #include·​<string>21 #include·​<string>
22 22
23 using·​asio:​:​ip:​:​tcp;​23 using·​asio:​:​ip:​:​tcp;​
24 24
25 /​/​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​25 /​/​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​
26 26
27 /​/​·​A·​custom·​completion·​token·​that·​makes·​asynchronous·​operations·​behave·​as27 /​/​·​A·​custom·​completion·​token·​that·​makes·​asynchronous·​operations·​behave·​as
28 /​/​·​though·​they·​are·​blocking·​calls·​with·​a·​timeout.​28 /​/​·​though·​they·​are·​blocking·​calls·​with·​a·​timeout.​
29 struct·​close_after29 struct·​close_after
30 {30 {
31 ··​close_after(asio:​:​chrono:​:​steady_clock:​:​duration·​t,​·​tcp:​:​socket&·​s)​31 ··​close_after(std:​:​chrono:​:​steady_clock:​:​duration·​t,​·​tcp:​:​socket&·​s)​
32 ····​:​·​timeout_(t)​,​·​socket_(s)​32 ····​:​·​timeout_(t)​,​·​socket_(s)​
33 ··​{33 ··​{
34 ··​}34 ··​}
35 35
36 ··​/​/​·​The·​maximum·​time·​to·​wait·​for·​an·​asynchronous·​operation·​to·​complete.​36 ··​/​/​·​The·​maximum·​time·​to·​wait·​for·​an·​asynchronous·​operation·​to·​complete.​
37 ··asio:​:​chrono:​:​steady_clock:​:​duration·​timeout_;​37 ··​std:​:​chrono:​:​steady_clock:​:​duration·​timeout_;​
38 38
39 ··​/​/​·​The·​socket·​to·​be·​closed·​if·​the·​operation·​does·​not·​complete·​in·​time.​39 ··​/​/​·​The·​socket·​to·​be·​closed·​if·​the·​operation·​does·​not·​complete·​in·​time.​
40 ··​tcp:​:​socket&·​socket_;​40 ··​tcp:​:​socket&·​socket_;​
41 };​41 };​
42 42
43 namespace·​asio·​{43 namespace·​asio·​{
44 44
45 /​/​·​The·​async_result·​template·​is·​specialised·​to·​allow·​the·​close_after·​token·​to45 /​/​·​The·​async_result·​template·​is·​specialised·​to·​allow·​the·​close_after·​token·​to
46 /​/​·​be·​used·​with·​asynchronous·​operations·​that·​have·​a·​completion·​signature·​of46 /​/​·​be·​used·​with·​asynchronous·​operations·​that·​have·​a·​completion·​signature·​of
47 /​/​·​void(error_code,​·​T)​.​·​Generalising·​this·​for·​all·​completion·​signature·​forms·​is47 /​/​·​void(error_code,​·​T)​.​·​Generalising·​this·​for·​all·​completion·​signature·​forms·​is
48 /​/​·​left·​as·​an·​exercise·​for·​the·​reader.​48 /​/​·​left·​as·​an·​exercise·​for·​the·​reader.​
49 template·​<typename·​T>49 template·​<typename·​T>
50 class·​async_result<close_af​ter,​·​void(asio:​:​error_code,​·​T)​>50 class·​async_result<close_af​ter,​·​void(std:​:​error_code,​·​T)​>
51 {51 {
52 public:​52 public:​
53 ··​/​/​·​An·​asynchronous·​operation's·​initiating·​function·​automatically·​creates·​an53 ··​/​/​·​An·​asynchronous·​operation's·​initiating·​function·​automatically·​creates·​an
54 ··​/​/​·​completion_handler_ty​pe·​object·​from·​the·​token.​·​This·​function·​object·​is54 ··​/​/​·​completion_handler_ty​pe·​object·​from·​the·​token.​·​This·​function·​object·​is
55 ··​/​/​·​then·​called·​on·​completion·​of·​the·​asynchronous·​operation.​55 ··​/​/​·​then·​called·​on·​completion·​of·​the·​asynchronous·​operation.​
56 ··​class·​completion_handler_ty​pe56 ··​class·​completion_handler_ty​pe
57 ··​{57 ··​{
58 ··​public:​58 ··​public:​
59 ····​completion_handler_ty​pe(const·​close_after&·​token)​59 ····​completion_handler_ty​pe(const·​close_after&·​token)​
60 ······​:​·​token_(token)​60 ······​:​·​token_(token)​
61 ····​{61 ····​{
62 ····​}62 ····​}
63 63
64 ····​void·​operator()​(asio:​:​error_code·​ec,​·​T·​t)​64 ····​void·​operator()​(const·std:​:​error_code&·​error,​·​T·​t)​
65 ····​{65 ····​{
66 ······​*ec_·​=·​ec;​66 ······​*error_·​=·​error;​
67 ······​*t_·​=·​t;​67 ······​*t_·​=·​t;​
68 ····​}68 ····​}
69 69
70 ··​private:​70 ··​private:​
71 ····​friend·​class·​async_result;​71 ····​friend·​class·​async_result;​
72 ····​close_after·​token_;​72 ····​close_after·​token_;​
73 ····asio:​:​error_code*·​ec_;​73 ····​std:​:​error_code*·​error_;​
74 ····​T*·​t_;​74 ····​T*·​t_;​
75 ··​};​75 ··​};​
76 76
77 ··​/​/​·​The·​async_result·​constructor·​associates·​the·​completion·​handler·​object·​with77 ··​/​/​·​The·​async_result·​constructor·​associates·​the·​completion·​handler·​object·​with
78 ··​/​/​·​the·​result·​of·​the·​initiating·​function.​78 ··​/​/​·​the·​result·​of·​the·​initiating·​function.​
79 ··​explicit·​async_result(completi​on_handler_type&·​h)​79 ··​explicit·​async_result(completi​on_handler_type&·​h)​
80 ····​:​·​timeout_(h.​token_.​timeout_)​,​80 ····​:​·​timeout_(h.​token_.​timeout_)​,​
81 ······​socket_(h.​token_.​socket_)​81 ······​socket_(h.​token_.​socket_)​
82 ··​{82 ··​{
83 ····​h.​ec_·​=·​&ec_;​83 ····​h.​error_·​=·​&error_;​
84 ····​h.​t_·​=·​&t_;​84 ····​h.​t_·​=·​&t_;​
85 ··​}85 ··​}
86 86
87 ··​/​/​·​The·​return_type·​typedef·​determines·​the·​result·​type·​of·​the·​asynchronous87 ··​/​/​·​The·​return_type·​typedef·​determines·​the·​result·​type·​of·​the·​asynchronous
88 ··​/​/​·​operation's·​initiating·​function.​88 ··​/​/​·​operation's·​initiating·​function.​
89 ··​typedef·​T·​return_type;​89 ··​typedef·​T·​return_type;​
90 90
91 ··​/​/​·​The·​get()​·​function·​is·​used·​to·​obtain·​the·​result·​of·​the·​asynchronous91 ··​/​/​·​The·​get()​·​function·​is·​used·​to·​obtain·​the·​result·​of·​the·​asynchronous
92 ··​/​/​·​operation's·​initiating·​function.​·​For·​the·​close_after·​completion·​token,​·​we92 ··​/​/​·​operation's·​initiating·​function.​·​For·​the·​close_after·​completion·​token,​·​we
93 ··​/​/​·​use·​this·​function·​to·​run·​the·​io_context·​until·​the·​operation·​is·​complete.​93 ··​/​/​·​use·​this·​function·​to·​run·​the·​io_context·​until·​the·​operation·​is·​complete.​
94 ··​return_type·​get()​94 ··​return_type·​get()​
95 ··​{95 ··​{
96 ····​asio:​:​io_context&·​io_context·​=·​socket_.​get_executor()​.​context()​;​96 ····​asio:​:​io_context&·​io_context·​=·​socket_.​get_executor()​.​context()​;​
97 97
98 ····​/​/​·​Restart·​the·​io_context,​·​as·​it·​may·​have·​been·​left·​in·​the·​"stopped"·​state98 ····​/​/​·​Restart·​the·​io_context,​·​as·​it·​may·​have·​been·​left·​in·​the·​"stopped"·​state
99 ····​/​/​·​by·​a·​previous·​operation.​99 ····​/​/​·​by·​a·​previous·​operation.​
100 ····​io_context.​restart()​;​100 ····​io_context.​restart()​;​
101 101
102 ····​/​/​·​Block·​until·​the·​asynchronous·​operation·​has·​completed,​·​or·​timed·​out.​·​If102 ····​/​/​·​Block·​until·​the·​asynchronous·​operation·​has·​completed,​·​or·​timed·​out.​·​If
103 ····​/​/​·​the·​pending·​asynchronous·​operation·​is·​a·​composed·​operation,​·​the·​deadline103 ····​/​/​·​the·​pending·​asynchronous·​operation·​is·​a·​composed·​operation,​·​the·​deadline
104 ····​/​/​·​applies·​to·​the·​entire·​operation,​·​rather·​than·​individual·​operations·​on104 ····​/​/​·​applies·​to·​the·​entire·​operation,​·​rather·​than·​individual·​operations·​on
105 ····​/​/​·​the·​socket.​105 ····​/​/​·​the·​socket.​
106 ····​io_context.​run_for(timeout_)​;​106 ····​io_context.​run_for(timeout_)​;​
107 107
108 ····​/​/​·​If·​the·​asynchronous·​operation·​completed·​successfully·​then·​the·​io_context108 ····​/​/​·​If·​the·​asynchronous·​operation·​completed·​successfully·​then·​the·​io_context
109 ····​/​/​·​would·​have·​been·​stopped·​due·​to·​running·​out·​of·​work.​·​If·​it·​was·​not109 ····​/​/​·​would·​have·​been·​stopped·​due·​to·​running·​out·​of·​work.​·​If·​it·​was·​not
110 ····​/​/​·​stopped,​·​then·​the·​io_context:​:​run_for·​call·​must·​have·​timed·​out·​and·​the110 ····​/​/​·​stopped,​·​then·​the·​io_context:​:​run_for·​call·​must·​have·​timed·​out·​and·​the
111 ····​/​/​·​operation·​is·​still·​incomplete.​111 ····​/​/​·​operation·​is·​still·​incomplete.​
112 ····​if·​(!io_context.​stopped()​)​112 ····​if·​(!io_context.​stopped()​)​
113 ····​{113 ····​{
114 ······​/​/​·​Close·​the·​socket·​to·​cancel·​the·​outstanding·​asynchronous·​operation.​114 ······​/​/​·​Close·​the·​socket·​to·​cancel·​the·​outstanding·​asynchronous·​operation.​
115 ······​socket_.​close()​;​115 ······​socket_.​close()​;​
116 116
117 ······​/​/​·​Run·​the·​io_context·​again·​until·​the·​operation·​completes.​117 ······​/​/​·​Run·​the·​io_context·​again·​until·​the·​operation·​completes.​
118 ······​io_context.​run()​;​118 ······​io_context.​run()​;​
119 ····​}119 ····​}
120 120
121 ····​/​/​·​If·​the·​operation·​failed,​·​throw·​an·​exception.​·​Otherwise·​return·​the·​result.​121 ····​/​/​·​If·​the·​operation·​failed,​·​throw·​an·​exception.​·​Otherwise·​return·​the·​result.​
122 ····​return·​ec_·​?·​throw·asio:​:​system_error(ec_)​·​:​·​t_;​122 ····​return·​error_·​?·​throw·​std:​:​system_error(error_)​·​:​·​t_;​
123 ··​}123 ··​}
124 124
125 private:​125 private:​
126 ··asio:​:​chrono:​:​steady_clock:​:​duration·​timeout_;​126 ··​std:​:​chrono:​:​steady_clock:​:​duration·​timeout_;​
127 ··​tcp:​:​socket&·​socket_;​127 ··​tcp:​:​socket&·​socket_;​
128 ··asio:​:​error_code·​ec_;​128 ··​std:​:​error_code·​error_;​
129 ··​T·​t_;​129 ··​T·​t_;​
130 };​130 };​
131 131
132 }·​/​/​·​namespace·​asio132 }·​/​/​·​namespace·​asio
133 133
134 /​/​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​134 /​/​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​-​
135 135
136 int·​main(int·​argc,​·​char*·​argv[])​136 int·​main(int·​argc,​·​char*·​argv[])​
137 {137 {
138 ··​try138 ··​try
139 ··​{139 ··​{
140 ····​if·​(argc·​!=·​4)​140 ····​if·​(argc·​!=·​4)​
141 ····​{141 ····​{
142 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_tcp_client·​<host>·​<port>·​<message>\n";​142 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_tcp_client·​<host>·​<port>·​<message>\n";​
143 ······​return·​1;​143 ······​return·​1;​
144 ····​}144 ····​}
145 145
146 ····​asio:​:​io_context·​io_context;​146 ····​asio:​:​io_context·​io_context;​
147 147
148 ····​/​/​·​Resolve·​the·​host·​name·​and·​service·​to·​a·​list·​of·​endpoints.​148 ····​/​/​·​Resolve·​the·​host·​name·​and·​service·​to·​a·​list·​of·​endpoints.​
149 ····​tcp:​:​resolver:​:​results_type·endpoints·=149 ····auto·endpoints·=·​tcp:​:​resolver(io_context)​.​resolve(argv[1],​·argv[2])​;​
150 ······tcp:​:​resolver(io_context)​.​resolve(argv[1],​·argv[2])​;​
151 150
152 ····​tcp:​:​socket·​socket(io_context)​;​151 ····​tcp:​:​socket·​socket(io_context)​;​
153 152
154 ····​/​/​·​Run·​an·​asynchronous·​connect·​operation·​with·​a·​timeout.​153 ····​/​/​·​Run·​an·​asynchronous·​connect·​operation·​with·​a·​timeout.​
155 ····​asio:​:​async_connect(socket,​·​endpoints,​154 ····​asio:​:​async_connect(socket,​·​endpoints,​
156 ········​close_after(asio:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​155 ········​close_after(std:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​
157 156
158 ····​asio:​:​chrono:​:​steady_clock:​:​time_point·time_sent·=157 ····​auto·time_sent·=·std:​:​chrono:​:​steady_clock:​:​now()​;​
159 ······asio:​:​chrono:​:​steady_clock:​:​now()​;​
160 158
161 ····​/​/​·​Run·​an·​asynchronous·​write·​operation·​with·​a·​timeout.​159 ····​/​/​·​Run·​an·​asynchronous·​write·​operation·​with·​a·​timeout.​
162 ····​std:​:​string·​msg·​=·​argv[3]·​+·​std:​:​string("\n")​;​160 ····​std:​:​string·​msg·​=·​argv[3]·​+·​std:​:​string("\n")​;​
163 ····​asio:​:​async_write(socket,​·​asio:​:​buffer(msg)​,​161 ····​asio:​:​async_write(socket,​·​asio:​:​buffer(msg)​,​
164 ········​close_after(asio:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​162 ········​close_after(std:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​
165 163
166 ····​for·​(std:​:​string·​input_buffer;​;​)​164 ····​for·​(std:​:​string·​input_buffer;​;​)​
167 ····​{165 ····​{
168 ······​/​/​·​Run·​an·​asynchronous·​read·​operation·​with·​a·​timeout.​166 ······​/​/​·​Run·​an·​asynchronous·​read·​operation·​with·​a·​timeout.​
169 ······​std:​:​size_t·​n·​=·​asio:​:​async_read_until(sock​et,​167 ······​std:​:​size_t·​n·​=·​asio:​:​async_read_until(sock​et,​
170 ··········​asio:​:​dynamic_buffer(input_​buffer)​,​·​'\n',​168 ··········​asio:​:​dynamic_buffer(input_​buffer)​,​·​'\n',​
171 ··········​close_after(asio:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​169 ··········​close_after(std:​:​chrono:​:​seconds(10)​,​·​socket)​)​;​
172 170
173 ······​std:​:​string·​line(input_buffer.​substr(0,​·​n·​-​·​1)​)​;​171 ······​std:​:​string·​line(input_buffer.​substr(0,​·​n·​-​·​1)​)​;​
174 ······​input_buffer.​erase(0,​·​n)​;​172 ······​input_buffer.​erase(0,​·​n)​;​
175 173
176 ······​/​/​·​Keep·​going·​until·​we·​get·​back·​the·​line·​that·​was·​sent.​174 ······​/​/​·​Keep·​going·​until·​we·​get·​back·​the·​line·​that·​was·​sent.​
177 ······​if·​(line·​==·​argv[3])​175 ······​if·​(line·​==·​argv[3])​
178 ········​break;​176 ········​break;​
179 ····​}177 ····​}
180 178
181 ····​asio:​:​chrono:​:​steady_clock:​:​time_point·time_received·=179 ····​auto·time_received·=·std:​:​chrono:​:​steady_clock:​:​now()​;​
182 ······asio:​:​chrono:​:​steady_clock:​:​now()​;​
183 180
184 ····​std:​:​cout·​<<·​"Round·​trip·​time:​·​";​181 ····​std:​:​cout·​<<·​"Round·​trip·​time:​·​";​
185 ····​std:​:​cout·​<<·asio:​:​chrono:​:​duration_cast<182 ····​std:​:​cout·​<<·​std:​:​chrono:​:​duration_cast<
186 ······asio:​:​chrono:​:​microseconds>(183 ······​std:​:​chrono:​:​microseconds>(
187 ········​time_received·​-​·​time_sent)​.​count()​;​184 ········​time_received·​-​·​time_sent)​.​count()​;​
188 ····​std:​:​cout·​<<·​"·​microseconds\n";​185 ····​std:​:​cout·​<<·​"·​microseconds\n";​
189 ··​}186 ··​}
190 ··​catch·​(std:​:​exception&·​e)​187 ··​catch·​(std:​:​exception&·​e)​
191 ··​{188 ··​{
192 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​189 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
193 ··​}190 ··​}
194 191
195 ··​return·​0;​192 ··​return·​0;​
196 }193 }