MCPcopy Create free account
hub / github.com/boostorg/asio / async_write_messages

Function async_write_messages

example/cpp11/operations/composed_6.cpp:198–253  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

196
197template <typename T, typename CompletionToken>
198auto async_write_messages(tcp::socket& socket,
199 const T& message, std::size_t repeat_count,
200 CompletionToken&& token)
201 // The return type of the initiating function is deduced from the combination
202 // of:
203 //
204 // - the CompletionToken type,
205 // - the completion handler signature, and
206 // - the asynchronous operation's initiation function object.
207 //
208 // When the completion token is a simple callback, the return type is always
209 // void. In this example, when the completion token is boost::asio::yield_context
210 // (used for stackful coroutines) the return type would also be void, as
211 // there is no non-error argument to the completion handler. When the
212 // completion token is boost::asio::use_future it would be std::future<void>. When
213 // the completion token is boost::asio::deferred, the return type differs for each
214 // asynchronous operation.
215 //
216 // In C++11 we deduce the type from the call to boost::asio::async_initiate.
217 -> decltype(
218 boost::asio::async_initiate<
219 CompletionToken, void(boost::system::error_code)>(
220 async_write_message_initiation(), token, std::ref(socket),
221 std::declval<std::unique_ptr<std::string>>(), repeat_count,
222 std::declval<std::unique_ptr<boost::asio::steady_timer>>()))
223{
224 // Encode the message and copy it into an allocated buffer. The buffer will
225 // be maintained for the lifetime of the composed asynchronous operation.
226 std::ostringstream os;
227 os << message;
228 std::unique_ptr<std::string> encoded_message(new std::string(os.str()));
229
230 // Create a steady_timer to be used for the delay between messages.
231 std::unique_ptr<boost::asio::steady_timer> delay_timer(
232 new boost::asio::steady_timer(socket.get_executor()));
233
234 // The boost::asio::async_initiate function takes:
235 //
236 // - our initiation function object,
237 // - the completion token,
238 // - the completion handler signature, and
239 // - any additional arguments we need to initiate the operation.
240 //
241 // It then asks the completion token to create a completion handler (i.e. a
242 // callback) with the specified signature, and invoke the initiation function
243 // object with this completion handler as well as the additional arguments.
244 // The return value of async_initiate is the result of our operation's
245 // initiating function.
246 //
247 // Note that we wrap non-const reference arguments in std::reference_wrapper
248 // to prevent incorrect decay-copies of these objects.
249 return boost::asio::async_initiate<
250 CompletionToken, void(boost::system::error_code)>(
251 async_write_message_initiation(), token, std::ref(socket),
252 std::move(encoded_message), repeat_count, std::move(delay_timer));
253}
254
255//------------------------------------------------------------------------------

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 3

refClass · 0.50
get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56