| 907 | //------------------------------------------------------------------------------ |
| 908 | |
| 909 | int main(int argc, char* argv[]) |
| 910 | { |
| 911 | // Check command line arguments. |
| 912 | if (argc != 5) |
| 913 | { |
| 914 | std::cerr << |
| 915 | "Usage: advanced-server-flex <address> <port> <doc_root> <threads>\n" << |
| 916 | "Example:\n" << |
| 917 | " advanced-server-flex 0.0.0.0 8080 . 1\n"; |
| 918 | return EXIT_FAILURE; |
| 919 | } |
| 920 | auto const address = net::ip::make_address(argv[1]); |
| 921 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 922 | auto const doc_root = std::make_shared<std::string>(argv[3]); |
| 923 | auto const threads = std::max<int>(1, std::atoi(argv[4])); |
| 924 | |
| 925 | // The io_context is required for all I/O |
| 926 | net::io_context ioc{threads}; |
| 927 | |
| 928 | // The SSL context is required, and holds certificates |
| 929 | ssl::context ctx{ssl::context::tlsv12}; |
| 930 | |
| 931 | // This holds the self-signed certificate used by the server |
| 932 | load_server_certificate(ctx); |
| 933 | |
| 934 | // Create and launch a listening port |
| 935 | std::make_shared<listener>( |
| 936 | ioc, |
| 937 | ctx, |
| 938 | tcp::endpoint{address, port}, |
| 939 | doc_root)->run(); |
| 940 | |
| 941 | // Capture SIGINT and SIGTERM to perform a clean shutdown |
| 942 | net::signal_set signals(ioc, SIGINT, SIGTERM); |
| 943 | signals.async_wait( |
| 944 | [&](beast::error_code const&, int) |
| 945 | { |
| 946 | // Stop the `io_context`. This will cause `run()` |
| 947 | // to return immediately, eventually destroying the |
| 948 | // `io_context` and all of the sockets in it. |
| 949 | ioc.stop(); |
| 950 | }); |
| 951 | |
| 952 | // Run the I/O service on the requested number of threads |
| 953 | std::vector<std::thread> v; |
| 954 | v.reserve(threads - 1); |
| 955 | for(auto i = threads - 1; i > 0; --i) |
| 956 | v.emplace_back( |
| 957 | [&ioc] |
| 958 | { |
| 959 | ioc.run(); |
| 960 | }); |
| 961 | ioc.run(); |
| 962 | |
| 963 | // (If we get here, it means we got a SIGINT or SIGTERM) |
| 964 | |
| 965 | // Block until all the threads exit |
| 966 | for(auto& t : v) |
nothing calls this directly
no test coverage detected