| 353 | } |
| 354 | |
| 355 | int main(int argc, char* argv[]) |
| 356 | { |
| 357 | // Check command line arguments. |
| 358 | if (argc != 5) |
| 359 | { |
| 360 | std::cerr << |
| 361 | "Usage: http-server-coro-ssl <address> <port> <doc_root> <threads>\n" << |
| 362 | "Example:\n" << |
| 363 | " http-server-coro-ssl 0.0.0.0 8080 . 1\n"; |
| 364 | return EXIT_FAILURE; |
| 365 | } |
| 366 | auto const address = net::ip::make_address(argv[1]); |
| 367 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 368 | auto const doc_root = std::make_shared<std::string>(argv[3]); |
| 369 | auto const threads = std::max<int>(1, std::atoi(argv[4])); |
| 370 | |
| 371 | // The io_context is required for all I/O |
| 372 | net::io_context ioc{threads}; |
| 373 | |
| 374 | // The SSL context is required, and holds certificates |
| 375 | ssl::context ctx{ssl::context::tlsv12}; |
| 376 | |
| 377 | // This holds the self-signed certificate used by the server |
| 378 | load_server_certificate(ctx); |
| 379 | |
| 380 | // Spawn a listening port |
| 381 | boost::asio::spawn(ioc, |
| 382 | std::bind( |
| 383 | &do_listen, |
| 384 | std::ref(ioc), |
| 385 | std::ref(ctx), |
| 386 | tcp::endpoint{address, port}, |
| 387 | doc_root, |
| 388 | std::placeholders::_1), |
| 389 | // on completion, spawn will call this function |
| 390 | [](std::exception_ptr ex) |
| 391 | { |
| 392 | // if an exception occurred in the coroutine, |
| 393 | // it's something critical, e.g. out of memory |
| 394 | // we capture normal errors in the ec |
| 395 | // so we just rethrow the exception here, |
| 396 | // which will cause `ioc.run()` to throw |
| 397 | if (ex) |
| 398 | std::rethrow_exception(ex); |
| 399 | }); |
| 400 | |
| 401 | // Run the I/O service on the requested number of threads |
| 402 | std::vector<std::thread> v; |
| 403 | v.reserve(threads - 1); |
| 404 | for(auto i = threads - 1; i > 0; --i) |
| 405 | v.emplace_back( |
| 406 | [&ioc] |
| 407 | { |
| 408 | ioc.run(); |
| 409 | }); |
| 410 | ioc.run(); |
| 411 | |
| 412 | return EXIT_SUCCESS; |
nothing calls this directly
no test coverage detected