MCPcopy Create free account
hub / github.com/boostorg/beast / main

Function main

example/advanced/server/advanced_server.cpp:590–644  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

588//------------------------------------------------------------------------------
589
590int main(int argc, char* argv[])
591{
592 // Check command line arguments.
593 if (argc != 5)
594 {
595 std::cerr <<
596 "Usage: advanced-server <address> <port> <doc_root> <threads>\n" <<
597 "Example:\n" <<
598 " advanced-server 0.0.0.0 8080 . 1\n";
599 return EXIT_FAILURE;
600 }
601 auto const address = net::ip::make_address(argv[1]);
602 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
603 auto const doc_root = std::make_shared<std::string>(argv[3]);
604 auto const threads = std::max<int>(1, std::atoi(argv[4]));
605
606 // The io_context is required for all I/O
607 net::io_context ioc{threads};
608
609 // Create and launch a listening port
610 std::make_shared<listener>(
611 ioc,
612 tcp::endpoint{address, port},
613 doc_root)->run();
614
615 // Capture SIGINT and SIGTERM to perform a clean shutdown
616 net::signal_set signals(ioc, SIGINT, SIGTERM);
617 signals.async_wait(
618 [&](beast::error_code const&, int)
619 {
620 // Stop the `io_context`. This will cause `run()`
621 // to return immediately, eventually destroying the
622 // `io_context` and all of the sockets in it.
623 ioc.stop();
624 });
625
626 // Run the I/O service on the requested number of threads
627 std::vector<std::thread> v;
628 v.reserve(threads - 1);
629 for(auto i = threads - 1; i > 0; --i)
630 v.emplace_back(
631 [&ioc]
632 {
633 ioc.run();
634 });
635 ioc.run();
636
637 // (If we get here, it means we got a SIGINT or SIGTERM)
638
639 // Block until all the threads exit
640 for(auto& t : v)
641 t.join();
642
643 return EXIT_SUCCESS;
644}

Callers

nothing calls this directly

Calls 2

async_waitMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected