| 21 | using asio::ip::tcp; |
| 22 | |
| 23 | async_simple::coro::Lazy<void> session(tcp::socket sock) { |
| 24 | int msg_index = 0; |
| 25 | for (;;) { |
| 26 | const size_t max_length = 1024; |
| 27 | char data[max_length]; |
| 28 | auto [error, length] = |
| 29 | co_await async_read_some(sock, asio::buffer(data, max_length)); |
| 30 | msg_index++; |
| 31 | if (error == asio::error::eof) { |
| 32 | std::cout << "Remote client closed at message index: " |
| 33 | << msg_index - 1 << ".\n"; |
| 34 | break; |
| 35 | } else if (error) { |
| 36 | std::cout << error.message() << '\n'; |
| 37 | throw asio::system_error(error); |
| 38 | } |
| 39 | |
| 40 | co_await async_write(sock, asio::buffer(data, length)); |
| 41 | } |
| 42 | |
| 43 | std::error_code ec; |
| 44 | sock.shutdown(asio::ip::tcp::socket::shutdown_both, ec); |
| 45 | sock.close(ec); |
| 46 | |
| 47 | std::cout << "Finished echo message, total: " << msg_index - 1 << ".\n"; |
| 48 | } |
| 49 | |
| 50 | async_simple::coro::Lazy<void> start_server(asio::io_context& io_context, |
| 51 | unsigned short port, |
no test coverage detected