1 //
2 // blocking_token_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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 typedef boost::asio::basic_stream_socket<tcp,
27 boost::asio::io_context::executor_type> tcp_socket;
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 {
close_afterclose_after35 close_after(boost::asio::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 boost::asio::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:
completion_handler_type(const close_after & token)64 completion_handler_type(const close_after& token)
65 : token_(token)
66 {
67 }
68
operator ()(boost::system::error_code ec,T t)69 void operator()(boost::system::error_code ec, T t)
70 {
71 *ec_ = ec;
72 *t_ = t;
73 }
74
75 private:
76 friend class async_result;
77 close_after token_;
78 boost::system::error_code* ec_;
79 T* t_;
80 };
81
82 // The async_result constructor associates the completion handler object with
83 // the result of the initiating function.
async_result(completion_handler_type & h)84 explicit async_result(completion_handler_type& h)
85 : timeout_(h.token_.timeout_),
86 socket_(h.token_.socket_)
87 {
88 h.ec_ = &ec_;
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.
get()99 return_type get()
100 {
101 boost::asio::io_context& io_context = boost::asio::query(
102 socket_.get_executor(), boost::asio::execution::context);
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 ec_ ? throw boost::system::system_error(ec_) : t_;
129 }
130
131 private:
132 boost::asio::chrono::steady_clock::duration timeout_;
133 tcp_socket& socket_;
134 boost::system::error_code ec_;
135 T t_;
136 };
137
138 } // namespace asio
139 } // namespace boost
140
141 //----------------------------------------------------------------------
142
main(int argc,char * argv[])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 tcp::resolver::results_type endpoints =
157 tcp::resolver(io_context).resolve(argv[1], argv[2]);
158
159 tcp_socket socket(io_context);
160
161 // Run an asynchronous connect operation with a timeout.
162 boost::asio::async_connect(socket, endpoints,
163 close_after(boost::asio::chrono::seconds(10), socket));
164
165 boost::asio::chrono::steady_clock::time_point time_sent =
166 boost::asio::chrono::steady_clock::now();
167
168 // Run an asynchronous write operation with a timeout.
169 std::string msg = argv[3] + std::string("\n");
170 boost::asio::async_write(socket, boost::asio::buffer(msg),
171 close_after(boost::asio::chrono::seconds(10), socket));
172
173 for (std::string input_buffer;;)
174 {
175 // Run an asynchronous read operation with a timeout.
176 std::size_t n = boost::asio::async_read_until(socket,
177 boost::asio::dynamic_buffer(input_buffer), '\n',
178 close_after(boost::asio::chrono::seconds(10), socket));
179
180 std::string line(input_buffer.substr(0, n - 1));
181 input_buffer.erase(0, n);
182
183 // Keep going until we get back the line that was sent.
184 if (line == argv[3])
185 break;
186 }
187
188 boost::asio::chrono::steady_clock::time_point time_received =
189 boost::asio::chrono::steady_clock::now();
190
191 std::cout << "Round trip time: ";
192 std::cout << boost::asio::chrono::duration_cast<
193 boost::asio::chrono::microseconds>(
194 time_received - time_sent).count();
195 std::cout << " microseconds\n";
196 }
197 catch (std::exception& e)
198 {
199 std::cerr << "Exception: " << e.what() << "\n";
200 }
201
202 return 0;
203 }
204