Verify Ctrl+C works.

master
Adam Gu 7 years ago
parent 10607afa70
commit 02b217abda

@ -17,7 +17,7 @@ HttpServer::HttpServer(unsigned short port, std::size_t workers)
// Register to handle the signals that indicate when the server should exit. // Register to handle the signals that indicate when the server should exit.
// It is safe to register for the same signal multiple times in a program, // It is safe to register for the same signal multiple times in a program,
// provided all registration for the specified signal is made through asio. // provided all registration for the specified signal is made through asio.
signals_.add(SIGINT); signals_.add(SIGINT); // Ctrl+C
signals_.add(SIGTERM); signals_.add(SIGTERM);
#if defined(SIGQUIT) #if defined(SIGQUIT)
signals_.add(SIGQUIT); signals_.add(SIGQUIT);
@ -29,7 +29,9 @@ HttpServer::HttpServer(unsigned short port, std::size_t workers)
// "reuse_addr=true" means option SO_REUSEADDR will be set. // "reuse_addr=true" means option SO_REUSEADDR will be set.
// For more details about SO_REUSEADDR, see: // For more details about SO_REUSEADDR, see:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx
// https://stackoverflow.com/a/3233022
// http://www.andy-pearce.com/blog/posts/2013/Feb/so_reuseaddr-on-windows/ // http://www.andy-pearce.com/blog/posts/2013/Feb/so_reuseaddr-on-windows/
// When |reuse_addr| is true, multiple servers can listen on the same port.
acceptor_.reset(new tcp::acceptor(io_context_, acceptor_.reset(new tcp::acceptor(io_context_,
tcp::endpoint(tcp::v4(), port), tcp::endpoint(tcp::v4(), port),
true)); // reuse_addr true)); // reuse_addr
@ -76,10 +78,11 @@ void HttpServer::AsyncAccept() {
void HttpServer::AsyncAwaitStop() { void HttpServer::AsyncAwaitStop() {
signals_.async_wait( signals_.async_wait(
[this](boost::system::error_code, int /*signo*/) { [this](boost::system::error_code, int signo) {
// The server is stopped by canceling all outstanding asynchronous // The server is stopped by canceling all outstanding asynchronous
// operations. Once all operations have finished the io_context::run() // operations. Once all operations have finished the io_context::run()
// call will exit. // call will exit.
LOG_INFO("On signal %d, stopping the server...", signo);
acceptor_->close(); acceptor_->close();
GetRequestHandler()->Stop(); GetRequestHandler()->Stop();
}); });

Loading…
Cancel
Save