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