]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp14/operations/composed_7.cpp
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #include <boost/asio/compose.hpp>
12 #include <boost/asio/io_context.hpp>
13 #include <boost/asio/ip/tcp.hpp>
14 #include <boost/asio/steady_timer.hpp>
15 #include <boost/asio/use_future.hpp>
16 #include <boost/asio/write.hpp>
22 #include <type_traits>
25 using boost::asio::ip::tcp
;
27 // NOTE: This example requires the new boost::asio::async_compose function. For
28 // an example that works with the Networking TS style of completion tokens,
29 // please see an older version of asio.
31 //------------------------------------------------------------------------------
33 // This composed operation shows composition of multiple underlying operations.
34 // It automatically serialises a message, using its I/O streams insertion
35 // operator, before sending it N times on the socket. To do this, it must
36 // allocate a buffer for the encoded message and ensure this buffer's validity
37 // until all underlying async_write operation complete. A one second delay is
38 // inserted prior to each write operation, using a steady_timer.
40 template <typename T
, typename CompletionToken
>
41 auto async_write_messages(tcp::socket
& socket
,
42 const T
& message
, std::size_t repeat_count
,
43 CompletionToken
&& token
)
44 // The return type of the initiating function is deduced from the combination
45 // of CompletionToken type and the completion handler's signature. When the
46 // completion token is a simple callback, the return type is always void.
47 // In this example, when the completion token is boost::asio::yield_context
48 // (used for stackful coroutines) the return type would be also be void, as
49 // there is no non-error argument to the completion handler. When the
50 // completion token is boost::asio::use_future it would be std::future<void>.
52 // In C++14 we can omit the return type as it is automatically deduced from
53 // the return type of boost::asio::async_initiate.
55 // Encode the message and copy it into an allocated buffer. The buffer will
56 // be maintained for the lifetime of the composed asynchronous operation.
57 std::ostringstream os
;
59 std::unique_ptr
<std::string
> encoded_message(new std::string(os
.str()));
61 // Create a steady_timer to be used for the delay between messages.
62 std::unique_ptr
<boost::asio::steady_timer
> delay_timer(
63 new boost::asio::steady_timer(socket
.get_executor()));
65 // To manage the cycle between the multiple underlying asychronous
66 // operations, our implementation is a state machine.
67 enum { starting
, waiting
, writing
};
69 // The boost::asio::async_compose function takes:
71 // - our asynchronous operation implementation,
72 // - the completion token,
73 // - the completion handler signature, and
74 // - any I/O objects (or executors) used by the operation
76 // It then wraps our implementation, which is implemented here as a state
77 // machine in a lambda, in an intermediate completion handler that meets the
78 // requirements of a conforming asynchronous operation. This includes
79 // tracking outstanding work against the I/O executors associated with the
80 // operation (in this example, this is the socket's executor).
82 // The first argument to our lambda is a reference to the enclosing
83 // intermediate completion handler. This intermediate completion handler is
84 // provided for us by the boost::asio::async_compose function, and takes care
85 // of all the details required to implement a conforming asynchronous
86 // operation. When calling an underlying asynchronous operation, we pass it
87 // this enclosing intermediate completion handler as the completion token.
89 // All arguments to our lambda after the first must be defaulted to allow the
90 // state machine to be started, as well as to allow the completion handler to
91 // match the completion signature of both the async_write and
92 // steady_timer::async_wait operations.
93 return boost::asio::async_compose
<
94 CompletionToken
, void(boost::system::error_code
)>(
96 // The implementation holds a reference to the socket as it is used for
97 // multiple async_write operations.
100 // The allocated buffer for the encoded message. The std::unique_ptr
101 // smart pointer is move-only, and as a consequence our lambda
102 // implementation is also move-only.
103 encoded_message
= std::move(encoded_message
),
105 // The repeat count remaining.
108 // A steady timer used for introducing a delay.
109 delay_timer
= std::move(delay_timer
),
111 // To manage the cycle between the multiple underlying asychronous
112 // operations, our implementation is a state machine.
117 const boost::system::error_code
& error
= {},
127 if (repeat_count
> 0)
131 delay_timer
->expires_after(std::chrono::seconds(1));
132 delay_timer
->async_wait(std::move(self
));
133 return; // Composed operation not yet complete.
135 break; // Composed operation complete, continue below.
138 boost::asio::async_write(socket
,
139 boost::asio::buffer(*encoded_message
), std::move(self
));
140 return; // Composed operation not yet complete.
144 // This point is reached only on completion of the entire composed
147 // Deallocate the encoded message and delay timer before calling the
148 // user-supplied completion handler.
149 encoded_message
.reset();
152 // Call the user-supplied handler with the result of the operation.
153 self
.complete(error
);
158 //------------------------------------------------------------------------------
162 boost::asio::io_context io_context
;
164 tcp::acceptor
acceptor(io_context
, {tcp::v4(), 55555});
165 tcp::socket socket
= acceptor
.accept();
167 // Test our asynchronous operation using a lambda as a callback.
168 async_write_messages(socket
, "Testing callback\r\n", 5,
169 [](const boost::system::error_code
& error
)
173 std::cout
<< "Messages sent\n";
177 std::cout
<< "Error: " << error
.message() << "\n";
184 //------------------------------------------------------------------------------
188 boost::asio::io_context io_context
;
190 tcp::acceptor
acceptor(io_context
, {tcp::v4(), 55555});
191 tcp::socket socket
= acceptor
.accept();
193 // Test our asynchronous operation using the use_future completion token.
194 // This token causes the operation's initiating function to return a future,
195 // which may be used to synchronously wait for the result of the operation.
196 std::future
<void> f
= async_write_messages(
197 socket
, "Testing future\r\n", 5, boost::asio::use_future
);
203 // Get the result of the operation.
205 std::cout
<< "Messages sent\n";
207 catch (const std::exception
& e
)
209 std::cout
<< "Error: " << e
.what() << "\n";
213 //------------------------------------------------------------------------------