| 95 | |
| 96 | template <typename CompletionToken> |
| 97 | auto async_write_message(tcp::socket& socket, |
| 98 | const char* message, CompletionToken&& token) |
| 99 | // The return type of the initiating function is deduced from the combination |
| 100 | // of: |
| 101 | // |
| 102 | // - the CompletionToken type, |
| 103 | // - the completion handler signature, and |
| 104 | // - the asynchronous operation's initiation function object. |
| 105 | // |
| 106 | // When the completion token is a simple callback, the return type is always |
| 107 | // void. In this example, when the completion token is boost::asio::yield_context |
| 108 | // (used for stackful coroutines) the return type would also be void, as |
| 109 | // there is no non-error argument to the completion handler. When the |
| 110 | // completion token is boost::asio::use_future it would be std::future<void>. When |
| 111 | // the completion token is boost::asio::deferred, the return type differs for each |
| 112 | // asynchronous operation. |
| 113 | // |
| 114 | // In C++11 we deduce the type from the call to boost::asio::async_initiate. |
| 115 | -> decltype( |
| 116 | boost::asio::async_initiate< |
| 117 | CompletionToken, void(boost::system::error_code)>( |
| 118 | async_write_message_initiation(), |
| 119 | token, std::ref(socket), message)) |
| 120 | { |
| 121 | // The boost::asio::async_initiate function takes: |
| 122 | // |
| 123 | // - our initiation function object, |
| 124 | // - the completion token, |
| 125 | // - the completion handler signature, and |
| 126 | // - any additional arguments we need to initiate the operation. |
| 127 | // |
| 128 | // It then asks the completion token to create a completion handler (i.e. a |
| 129 | // callback) with the specified signature, and invoke the initiation function |
| 130 | // object with this completion handler as well as the additional arguments. |
| 131 | // The return value of async_initiate is the result of our operation's |
| 132 | // initiating function. |
| 133 | // |
| 134 | // Note that we wrap non-const reference arguments in std::reference_wrapper |
| 135 | // to prevent incorrect decay-copies of these objects. |
| 136 | return boost::asio::async_initiate< |
| 137 | CompletionToken, void(boost::system::error_code)>( |
| 138 | async_write_message_initiation(), |
| 139 | token, std::ref(socket), message); |
| 140 | } |
| 141 | |
| 142 | //------------------------------------------------------------------------------ |
| 143 | |