| 72 | }; |
| 73 | |
| 74 | int main(int argc, char* argv[]) |
| 75 | { |
| 76 | try |
| 77 | { |
| 78 | if (argc != 2) |
| 79 | { |
| 80 | std::cerr << "Usage: echo_server <port>\n"; |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | boost::asio::io_context io_context; |
| 85 | |
| 86 | boost::asio::spawn(io_context, |
| 87 | [&](boost::asio::yield_context yield) |
| 88 | { |
| 89 | tcp::acceptor acceptor(io_context, |
| 90 | tcp::endpoint(tcp::v4(), std::atoi(argv[1]))); |
| 91 | |
| 92 | for (;;) |
| 93 | { |
| 94 | boost::system::error_code ec; |
| 95 | tcp::socket socket(io_context); |
| 96 | acceptor.async_accept(socket, yield[ec]); |
| 97 | if (!ec) |
| 98 | { |
| 99 | std::make_shared<session>(io_context, std::move(socket))->go(); |
| 100 | } |
| 101 | } |
| 102 | }, |
| 103 | [](std::exception_ptr e) |
| 104 | { |
| 105 | if (e) |
| 106 | std::rethrow_exception(e); |
| 107 | }); |
| 108 | |
| 109 | io_context.run(); |
| 110 | } |
| 111 | catch (std::exception& e) |
| 112 | { |
| 113 | std::cerr << "Exception: " << e.what() << "\n"; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
nothing calls this directly
no test coverage detected