| 87 | //------------------------------------------------------------------------------ |
| 88 | |
| 89 | int main(int argc, char* argv[]) |
| 90 | { |
| 91 | try |
| 92 | { |
| 93 | // Check command line arguments. |
| 94 | if (argc != 3) |
| 95 | { |
| 96 | std::cerr << |
| 97 | "Usage: websocket-server-sync-ssl <address> <port>\n" << |
| 98 | "Example:\n" << |
| 99 | " websocket-server-sync-ssl 0.0.0.0 8080\n"; |
| 100 | return EXIT_FAILURE; |
| 101 | } |
| 102 | auto const address = net::ip::make_address(argv[1]); |
| 103 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 104 | |
| 105 | // The io_context is required for all I/O |
| 106 | net::io_context ioc{1}; |
| 107 | |
| 108 | // The SSL context is required, and holds certificates |
| 109 | ssl::context ctx{ssl::context::tlsv12}; |
| 110 | |
| 111 | // This holds the self-signed certificate used by the server |
| 112 | load_server_certificate(ctx); |
| 113 | |
| 114 | // The acceptor receives incoming connections |
| 115 | tcp::acceptor acceptor{ioc, {address, port}}; |
| 116 | for(;;) |
| 117 | { |
| 118 | // This will receive the new connection |
| 119 | tcp::socket socket{ioc}; |
| 120 | |
| 121 | // Block until we get a connection |
| 122 | acceptor.accept(socket); |
| 123 | |
| 124 | // Launch the session, transferring ownership of the socket |
| 125 | std::thread( |
| 126 | &do_session, |
| 127 | std::move(socket), |
| 128 | std::ref(ctx)).detach(); |
| 129 | } |
| 130 | } |
| 131 | catch (const std::exception& e) |
| 132 | { |
| 133 | std::cerr << "Error: " << e.what() << std::endl; |
| 134 | return EXIT_FAILURE; |
| 135 | } |
| 136 | } |