| 308 | }; |
| 309 | |
| 310 | int main(int argc, char* argv[]) |
| 311 | { |
| 312 | try |
| 313 | { |
| 314 | // Check command line arguments. |
| 315 | if (argc != 6) |
| 316 | { |
| 317 | std::cerr << "Usage: http_server_fast <address> <port> <doc_root> <num_workers> {spin|block}\n"; |
| 318 | std::cerr << " For IPv4, try:\n"; |
| 319 | std::cerr << " http_server_fast 0.0.0.0 80 . 100 block\n"; |
| 320 | std::cerr << " For IPv6, try:\n"; |
| 321 | std::cerr << " http_server_fast 0::0 80 . 100 block\n"; |
| 322 | return EXIT_FAILURE; |
| 323 | } |
| 324 | |
| 325 | auto const address = net::ip::make_address(argv[1]); |
| 326 | unsigned short port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 327 | std::string doc_root = argv[3]; |
| 328 | int num_workers = std::atoi(argv[4]); |
| 329 | bool spin = (std::strcmp(argv[5], "spin") == 0); |
| 330 | |
| 331 | net::io_context ioc{1}; |
| 332 | tcp::acceptor acceptor{ioc, {address, port}}; |
| 333 | |
| 334 | std::list<http_worker> workers; |
| 335 | for (int i = 0; i < num_workers; ++i) |
| 336 | { |
| 337 | workers.emplace_back(acceptor, doc_root); |
| 338 | workers.back().start(); |
| 339 | } |
| 340 | |
| 341 | if (spin) |
| 342 | for (;;) ioc.poll(); |
| 343 | else |
| 344 | ioc.run(); |
| 345 | } |
| 346 | catch (const std::exception& e) |
| 347 | { |
| 348 | std::cerr << "Error: " << e.what() << std::endl; |
| 349 | return EXIT_FAILURE; |
| 350 | } |
| 351 | } |