| 246 | //------------------------------------------------------------------------------ |
| 247 | |
| 248 | int main(int argc, char* argv[]) |
| 249 | { |
| 250 | // Check command line arguments. |
| 251 | if (argc != 4) |
| 252 | { |
| 253 | std::cerr << |
| 254 | "Usage: websocket-server-async <address> <port> <threads>\n" << |
| 255 | "Example:\n" << |
| 256 | " websocket-server-async 0.0.0.0 8080 1\n"; |
| 257 | return EXIT_FAILURE; |
| 258 | } |
| 259 | auto const address = net::ip::make_address(argv[1]); |
| 260 | auto const port = static_cast<unsigned short>(std::atoi(argv[2])); |
| 261 | auto const threads = std::max<int>(1, std::atoi(argv[3])); |
| 262 | |
| 263 | // The io_context is required for all I/O |
| 264 | net::io_context ioc{threads}; |
| 265 | |
| 266 | // Create and launch a listening port |
| 267 | std::make_shared<listener>(ioc, tcp::endpoint{address, port})->run(); |
| 268 | |
| 269 | // Run the I/O service on the requested number of threads |
| 270 | std::vector<std::thread> v; |
| 271 | v.reserve(threads - 1); |
| 272 | for(auto i = threads - 1; i > 0; --i) |
| 273 | v.emplace_back( |
| 274 | [&ioc] |
| 275 | { |
| 276 | ioc.run(); |
| 277 | }); |
| 278 | ioc.run(); |
| 279 | |
| 280 | return EXIT_SUCCESS; |
| 281 | } |