int main(int argc, char *argv[]) { // Get a port number from the user if (argc <= 1) { std::cerr << "Usage: simple_http_server [port number]\n" << "E.g. simple_http_server 80" << std::endl; return 1; } std::string port(argv[1]); unsigned short portNumber(atoi(port.c_str())); std::cout << "HTTP server port: " << portNumber << std::endl; try { // create an io_service for the tcp port boost::asio::io_service io_service; // create an http_server http_server_type http_server(io_service, portNumber); // connect the request and chunk received callback functions http_server.request_received_event(request_receiver); http_server.chunk_received_event(chunk_receiver); // start accepting http connections http_server.start_accept(); // The signal set is used to register for termination notifications boost::asio::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) signals_.async_wait(boost::bind(&handle_stop)); // run the io_service to start communications io_service.run(); } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server ASIO::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the optional handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // set the connection timeout (10 seconds) http_server.set_timeout(10000); // set the connection buffer sizes http_server.set_rx_buffer_size(16384); http_server.tcp_server()->set_receive_buffer_size(16384); http_server.tcp_server()->set_send_buffer_size(16384); // start accepting http connections on the port ASIO_ERROR_CODE error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register termination notifications ASIO::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (ASIO_ERROR_CODE const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // run the io_service to start communications io_service.run(); std::cout << "io_service.run complete, shutdown successful" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }
int main(int argc, char *argv[]) { std::string app_name(argv[0]); unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT); // Get a port number from the user (the default is 80) if (argc > 2) { std::cerr << "Usage: " << app_name << " [port number]\n" << "E.g. " << app_name << " " << port_number << std::endl; return 1; } else if (argc == 2) { std::string port(argv[1]); port_number = atoi(port.c_str()); } std::cout << app_name << ": " << port_number << std::endl; try { // create an io_service for the server boost::asio::io_service io_service; // create an http_server and connect the request handler http_server_type http_server(io_service); http_server.request_received_event(request_handler); // connect the handler callback functions http_server.chunk_received_event(chunk_handler); http_server.request_expect_continue_event(expect_continue_handler); http_server.invalid_request_event(invalid_request_handler); http_server.socket_connected_event(connected_handler); http_server.socket_disconnected_event(disconnected_handler); http_server.message_sent_event(message_sent_handler); // start accepting http connections on the given port boost::system::error_code error(http_server.accept_connections(port_number)); if (error) { std::cerr << "Error: " << error.message() << std::endl; return 1; } // The signal set is used to register for termination notifications boost::asio::signal_set signals_(io_service); signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif // #if defined(SIGQUIT) // register the handle_stop callback signals_.async_wait([&http_server] (boost::system::error_code const& error, int signal_number) { handle_stop(error, signal_number, http_server); }); // Determine the number of concurrent threads supported size_t no_of_threads(std::thread::hardware_concurrency()); std::cout << "No of threads: " << no_of_threads << std::endl; if (no_of_threads > 0) { // Create a thread pool for the threads and run the asio io_service // in each of the threads. std::vector<std::shared_ptr<std::thread> > threads; for (std::size_t i = 0; i < no_of_threads; ++i) { std::shared_ptr<std::thread> thread(std::make_shared<std::thread> ([&io_service](){ io_service.run(); })); threads.push_back(thread); } // Wait for all threads in the pool to exit. for (std::size_t i(0); i < threads.size(); ++i) threads[i]->join(); } else io_service.run(); std::cout << "io_service.run, all work has finished" << std::endl; } catch (std::exception& e) { std::cerr << "Exception:" << e.what() << std::endl; } return 0; }