The writer will pick up reads from the reader through the channel and switch to the thread_pool to compute their response.
| 177 | // The writer will pick up reads from the reader through the channel and switch to the thread_pool to compute their |
| 178 | // response. |
| 179 | asio::awaitable<bool> writer(BidiStreamingRPC& rpc, Channel& channel, asio::thread_pool& thread_pool) |
| 180 | { |
| 181 | bool ok{true}; |
| 182 | while (ok) |
| 183 | { |
| 184 | const auto [ec, request] = co_await channel.async_receive(asio::as_tuple(asio::use_awaitable)); |
| 185 | if (ec) |
| 186 | { |
| 187 | // Channel got closed by the reader. |
| 188 | break; |
| 189 | } |
| 190 | // In this example we switch to the thread_pool to compute the response. |
| 191 | co_await asio::post(asio::bind_executor(thread_pool, asio::use_awaitable)); |
| 192 | |
| 193 | // Compute the response. |
| 194 | example::v1::Response response; |
| 195 | response.set_integer(request.integer() * 2); |
| 196 | |
| 197 | // rpc.write() is thread-safe so we can interact with it from the thread_pool. |
| 198 | ok = co_await rpc.write(response); |
| 199 | // Now we are back on the main thread. |
| 200 | } |
| 201 | co_return ok; |
| 202 | } |
| 203 | |
| 204 | auto bidirectional_streaming_rpc_handler(asio::thread_pool& thread_pool) |
| 205 | { |
no test coverage detected