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