| 30 | |
| 31 | template <typename CompletionToken> |
| 32 | auto async_write_message(tcp::socket& socket, |
| 33 | const char* message, CompletionToken&& token) |
| 34 | // The return type of the initiating function is deduced from the combination |
| 35 | // of: |
| 36 | // |
| 37 | // - the CompletionToken type, |
| 38 | // - the completion handler signature, and |
| 39 | // - the asynchronous operation's initiation function object. |
| 40 | // |
| 41 | // When the completion token is a simple callback, the return type is void. |
| 42 | // However, when the completion token is boost::asio::yield_context (used for |
| 43 | // stackful coroutines) the return type would be std::size_t, and when the |
| 44 | // completion token is boost::asio::use_future it would be std::future<std::size_t>. |
| 45 | // When the completion token is boost::asio::deferred, the return type differs for |
| 46 | // each asynchronous operation. |
| 47 | // |
| 48 | // In C++14 we can omit the return type as it is automatically deduced from |
| 49 | // the return type of our underlying asynchronous operation. |
| 50 | { |
| 51 | // When delegating to the underlying operation we must take care to perfectly |
| 52 | // forward the completion token. This ensures that our operation works |
| 53 | // correctly with move-only function objects as callbacks, as well as other |
| 54 | // completion token types. |
| 55 | return boost::asio::async_write(socket, |
| 56 | boost::asio::buffer(message, std::strlen(message)), |
| 57 | std::forward<CompletionToken>(token)); |
| 58 | } |
| 59 | |
| 60 | //------------------------------------------------------------------------------ |
| 61 | |