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