Launch an asynchronous operation with a stateful implementation.
template< typename CompletionToken, typename Signature, typename Implementation, typename... IoObjectsOrExecutors> DEDUCED async_compose( Implementation && implementation, CompletionToken & token, IoObjectsOrExecutors &&... io_objects_or_executors);
The async_compose function simplifies the implementation of composed asynchronous operations automatically wrapping a stateful function object with a conforming intermediate completion handler.
A function object that contains the implementation of the composed asynchronous operation. The first argument to the function object is a non-const reference to the enclosing intermediate completion handler. The remaining arguments are any arguments that originate from the completion handlers of any asynchronous operations performed by the implementation.
The completion token.
Zero or more I/O objects or I/O executors for which outstanding work must be maintained.
struct async_echo_implementation { tcp::socket& local_socket; asio::mutable_buffer buffer_; enum { starting, reading, writing } state_; template <typename Self> void operator()(Self& self, asio::error_code error = {}, std::size_t n = 0) { switch (state_) { case starting: state_ = reading; local_socket.async_read_some( buffer_, std::move(self)); break; case reading: if (error) { self.complete(error, 0); } else { state_ = writing; asio::async_write(local_socket, buffer_, asio::transfer_exactly(n), std::move(self)); } break; case writing: self.complete(error, n); break; } } }; template <typename CompletionToken> auto async_echo(tcp::socket& socket, asio::mutable_buffer buffer, CompletionToken&& token) -> decltype( asio::async_compose<CompletionToken, void(asio::error_code, std::size_t)>( std::declval<async_echo_implementation>(), token, socket)) { return asio::async_compose<CompletionToken, void(asio::error_code, std::size_t)>( async_echo_implementation{socket, buffer, async_echo_implementation::starting}, token, socket); }
Header: asio/compose.hpp
Convenience header: asio.hpp