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

Function async_write_messages

example/cpp14/operations/composed_8.cpp:46–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44
45template <typename T, typename CompletionToken>
46auto async_write_messages(tcp::socket& socket,
47 const T& message, std::size_t repeat_count,
48 CompletionToken&& token)
49 // The return type of the initiating function is deduced from the combination
50 // of:
51 //
52 // - the CompletionToken type,
53 // - the completion handler signature, and
54 // - the asynchronous operation's initiation function object.
55 //
56 // When the completion token is a simple callback, the return type is always
57 // void. In this example, when the completion token is boost::asio::yield_context
58 // (used for stackful coroutines) the return type would also be void, as
59 // there is no non-error argument to the completion handler. When the
60 // completion token is boost::asio::use_future it would be std::future<void>. When
61 // the completion token is boost::asio::deferred, the return type differs for each
62 // asynchronous operation.
63 //
64 // In C++14 we can omit the return type as it is automatically deduced from
65 // the return type of boost::asio::async_compose.
66{
67 // Encode the message and copy it into an allocated buffer. The buffer will
68 // be maintained for the lifetime of the composed asynchronous operation.
69 std::ostringstream os;
70 os << message;
71 std::unique_ptr<std::string> encoded_message(new std::string(os.str()));
72
73 // Create a steady_timer to be used for the delay between messages.
74 std::unique_ptr<boost::asio::steady_timer> delay_timer(
75 new boost::asio::steady_timer(socket.get_executor()));
76
77 // The boost::asio::async_compose function takes:
78 //
79 // - our asynchronous operation implementation,
80 // - the completion token,
81 // - the completion handler signature, and
82 // - any I/O objects (or executors) used by the operation
83 //
84 // It then wraps our implementation, which is implemented here as a stackless
85 // coroutine in a lambda, in an intermediate completion handler that meets the
86 // requirements of a conforming asynchronous operation. This includes
87 // tracking outstanding work against the I/O executors associated with the
88 // operation (in this example, this is the socket's executor).
89 //
90 // The first argument to our lambda is a reference to the enclosing
91 // intermediate completion handler. This intermediate completion handler is
92 // provided for us by the boost::asio::async_compose function, and takes care
93 // of all the details required to implement a conforming asynchronous
94 // operation. When calling an underlying asynchronous operation, we pass it
95 // this enclosing intermediate completion handler as the completion token.
96 //
97 // All arguments to our lambda after the first must be defaulted to allow the
98 // state machine to be started, as well as to allow the completion handler to
99 // match the completion signature of both the async_write and
100 // steady_timer::async_wait operations.
101 return boost::asio::async_compose<
102 CompletionToken, void(boost::system::error_code)>(
103 [

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 2

coroutineClass · 0.85
get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56