| 420 | //------------------------------------------------------------------------------ |
| 421 | |
| 422 | int main(int argc, char* argv[]) |
| 423 | { |
| 424 | // Check command line arguments. |
| 425 | if (argc != 5) |
| 426 | { |
| 427 | std::cerr << |
| 428 | "Usage: http-server-async <address> <port> <doc_root> <threads>\n" << |
| 429 | "Example:\n" << |
| 430 | " http-server-async 0.0.0.0 8080 . 1\n"; |
| 431 | return EXIT_FAILURE; |
| 432 | } |
| 433 | auto const address = net::ip::make_address(argv[1]); |
| 434 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 435 | auto const doc_root = std::make_shared<std::string>(argv[3]); |
| 436 | auto const threads = std::max<int>(1, std::atoi(argv[4])); |
| 437 | |
| 438 | // The io_context is required for all I/O |
| 439 | net::io_context ioc{threads}; |
| 440 | |
| 441 | // Create and launch a listening port |
| 442 | std::make_shared<listener>( |
| 443 | ioc, |
| 444 | tcp::endpoint{address, port}, |
| 445 | doc_root)->run(); |
| 446 | |
| 447 | // Run the I/O service on the requested number of threads |
| 448 | std::vector<std::thread> v; |
| 449 | v.reserve(threads - 1); |
| 450 | for(auto i = threads - 1; i > 0; --i) |
| 451 | v.emplace_back( |
| 452 | [&ioc] |
| 453 | { |
| 454 | ioc.run(); |
| 455 | }); |
| 456 | ioc.run(); |
| 457 | |
| 458 | return EXIT_SUCCESS; |
| 459 | } |