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

Function async_write_messages

example/cpp11/operations/composed_7.cpp:118–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116
117template <typename T, typename CompletionToken>
118auto async_write_messages(tcp::socket& socket,
119 const T& message, std::size_t repeat_count,
120 CompletionToken&& token)
121 // The return type of the initiating function is deduced from the combination
122 // of:
123 //
124 // - the CompletionToken type,
125 // - the completion handler signature, and
126 // - the asynchronous operation's initiation function object.
127 //
128 // When the completion token is a simple callback, the return type is always
129 // void. In this example, when the completion token is boost::asio::yield_context
130 // (used for stackful coroutines) the return type would also be void, as
131 // there is no non-error argument to the completion handler. When the
132 // completion token is boost::asio::use_future it would be std::future<void>. When
133 // the completion token is boost::asio::deferred, the return type differs for each
134 // asynchronous operation.
135 //
136 // In C++11 we deduce the type from the call to boost::asio::async_compose.
137 -> decltype(
138 boost::asio::async_compose<
139 CompletionToken, void(boost::system::error_code)>(
140 std::declval<async_write_messages_implementation>(),
141 token, socket))
142{
143 // Encode the message and copy it into an allocated buffer. The buffer will
144 // be maintained for the lifetime of the composed asynchronous operation.
145 std::ostringstream os;
146 os << message;
147 std::unique_ptr<std::string> encoded_message(new std::string(os.str()));
148
149 // Create a steady_timer to be used for the delay between messages.
150 std::unique_ptr<boost::asio::steady_timer> delay_timer(
151 new boost::asio::steady_timer(socket.get_executor()));
152
153 // The boost::asio::async_compose function takes:
154 //
155 // - our asynchronous operation implementation,
156 // - the completion token,
157 // - the completion handler signature, and
158 // - any I/O objects (or executors) used by the operation
159 //
160 // It then wraps our implementation in an intermediate completion handler
161 // that meets the requirements of a conforming asynchronous operation. This
162 // includes tracking outstanding work against the I/O executors associated
163 // with the operation (in this example, this is the socket's executor).
164 return boost::asio::async_compose<
165 CompletionToken, void(boost::system::error_code)>(
166 async_write_messages_implementation{
167 socket, std::move(encoded_message),
168 repeat_count, std::move(delay_timer),
169 async_write_messages_implementation::starting},
170 token, socket);
171}
172
173//------------------------------------------------------------------------------
174

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 1

get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56