Пример #1
0
int main(void) try {

    // setup asio::io_service
    std::shared_ptr<boost::asio::io_service> p_io_service(
        std::make_shared<boost::asio::io_service>());

    // Initialize SSL context
    std::shared_ptr<boost::asio::ssl::context> ctx =
        std::make_shared<boost::asio::ssl::context>(
            boost::asio::ssl::context::sslv23);
    ctx->set_options(boost::asio::ssl::context::default_workarounds |
                     boost::asio::ssl::context::no_sslv2 |
                     boost::asio::ssl::context::single_dh_use);

    // Set keys
    ctx->set_password_callback(password_callback);
    ctx->use_certificate_chain_file("server.pem");
    ctx->use_private_key_file("server.pem", boost::asio::ssl::context::pem);
    ctx->use_tmp_dh_file("dh512.pem");

    // setup the async server
    handler request_handler;
    std::shared_ptr<server> p_server_instance(std::make_shared<server>(
                server::options(request_handler)
                .address("0.0.0.0")
                .port("8442")
                .io_service(p_io_service)
                .reuse_address(true)
                .thread_pool(
                    std::make_shared<boost::network::utils::thread_pool>(2))
                .context(ctx)));

    // setup clean shutdown
    boost::asio::signal_set signals(*p_io_service, SIGINT, SIGTERM);
    signals.async_wait([=] (boost::system::error_code const &ec, int signal) {
        shut_me_down(ec, signal, p_server_instance);
    });

    // run the async server
    p_server_instance->run();

    // we are stopped - shutting down

    p_io_service->stop();

    std::cout << "Terminated normally" << std::endl;
    exit(EXIT_SUCCESS);
}
catch (const std::exception& e) {
    std::cout << "Abnormal termination - exception:" << e.what() << std::endl;
    exit(EXIT_FAILURE);
}
Пример #2
0
int main(void) try
  {
    // the thread group
    boost::shared_ptr< boost::thread_group > p_threads(
      boost::make_shared< boost::thread_group>());

    // setup asio::io_service
    boost::shared_ptr< boost::asio::io_service > p_io_service(
      boost::make_shared< boost::asio::io_service >());
    boost::shared_ptr< boost::asio::io_service::work > p_work(
      boost::make_shared< boost::asio::io_service::work >(
        boost::ref(*p_io_service)));

    // io_service threads
    {
      int n_threads = 1;
      while(0 < n_threads--) {
        p_threads->create_thread(
          boost::bind(&boost::asio::io_service::run, p_io_service));
      }
    }

    // the shared work queue
    work_queue queue;

    // worker threads that will process the request; off the queue
    {
      int n_threads = 12;
      while(0 < n_threads--) {
        p_threads->create_thread(
          boost::bind(process_request, boost::ref(queue)));
      }
    }

    // setup the async server
    handler request_handler(queue);
    boost::shared_ptr< server > p_server_instance(
      boost::make_shared<server>(
        server::options(request_handler).
        address("0.0.0.0")
        .port("8800")
        .io_service(p_io_service)
        .reuse_address(true)
        .thread_pool(
          boost::make_shared<boost::network::utils::thread_pool>(
            2 , p_io_service, p_threads))));

    // setup clean shutdown
    boost::asio::signal_set signals(*p_io_service, SIGINT, SIGTERM);
    signals.async_wait(boost::bind(shut_me_down, _1, _2, p_server_instance));

    // run the async server
    p_server_instance->run();

    // we are stopped - shutting down

    p_threads->interrupt_all();

    p_work.reset();
    p_io_service->stop();

    p_threads->join_all();

    Log("Terminated normally");
    exit(EXIT_SUCCESS);
  }
 catch(const std::exception& e)
   {
     Log("Abnormal termination - exception:"<<e.what());
     exit(EXIT_FAILURE);
   }