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