Refine the initialization of server's acceptor.

master
Chunting Gu 7 years ago
parent 921defc0e3
commit 686b415fb7

@ -13,31 +13,52 @@ using tcp = boost::asio::ip::tcp;
namespace webcc { namespace webcc {
HttpServer::HttpServer(std::uint16_t port, std::size_t workers) HttpServer::HttpServer(std::uint16_t port, std::size_t workers)
: signals_(io_context_) , workers_(workers) { : acceptor_(io_context_), signals_(io_context_), workers_(workers) {
// Register to handle the signals that indicate when the server should exit. RegisterSignals();
// 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. boost::system::error_code ec;
signals_.add(SIGINT); // Ctrl+C tcp::endpoint endpoint(tcp::v4(), port);
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif
// NOTE: // Open the acceptor.
// "reuse_addr=true" means option SO_REUSEADDR will be set. acceptor_.open(endpoint.protocol(), ec);
// For more details about SO_REUSEADDR, see: if (ec) {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx LOG_ERRO("Acceptor open error: %s", ec.message().c_str());
// https://stackoverflow.com/a/3233022 return;
// 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_, // Set option SO_REUSEADDR on.
tcp::endpoint(tcp::v4(), port), // When SO_REUSEADDR is set, multiple servers can listen on the same port.
true)); // reuse_addr // This is necessary for restarting the server on the same port.
// More details:
// - https://stackoverflow.com/a/3233022
// - http://www.andy-pearce.com/blog/posts/2013/Feb/so_reuseaddr-on-windows/
acceptor_.set_option(tcp::acceptor::reuse_address(true));
// Bind to the server address.
acceptor_.bind(endpoint, ec);
if (ec) {
LOG_ERRO("Acceptor bind error: %s", ec.message().c_str());
return;
}
// Start listening for connections.
// After listen, the client is able to connect to the server even the server
// has not started to accept the connection yet.
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
if (ec) {
LOG_ERRO("Acceptor listen error: %s", ec.message().c_str());
return;
}
} }
void HttpServer::Run() { void HttpServer::Run() {
assert(GetRequestHandler() != nullptr); assert(GetRequestHandler() != nullptr);
if (!acceptor_.is_open()) {
LOG_ERRO("Server is NOT going to run.");
return;
}
LOG_INFO("Server is going to run..."); LOG_INFO("Server is going to run...");
AsyncAwaitStop(); AsyncAwaitStop();
@ -54,12 +75,21 @@ void HttpServer::Run() {
io_context_.run(); io_context_.run();
} }
void HttpServer::RegisterSignals() {
signals_.add(SIGINT); // Ctrl+C
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif
}
void HttpServer::AsyncAccept() { void HttpServer::AsyncAccept() {
acceptor_->async_accept( acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket) { [this](boost::system::error_code ec, tcp::socket socket) {
// Check whether the server was stopped by a signal before this // Check whether the server was stopped by a signal before this
// completion handler had a chance to run. // completion handler had a chance to run.
if (!acceptor_->is_open()) { if (!acceptor_.is_open()) {
return; return;
} }
@ -83,7 +113,7 @@ void HttpServer::AsyncAwaitStop() {
// 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); LOG_INFO("On signal %d, stopping the server...", signo);
acceptor_->close(); acceptor_.close();
GetRequestHandler()->Stop(); GetRequestHandler()->Stop();
}); });
} }

@ -6,7 +6,6 @@
#include "boost/asio/io_context.hpp" #include "boost/asio/io_context.hpp"
#include "boost/asio/ip/tcp.hpp" #include "boost/asio/ip/tcp.hpp"
#include "boost/asio/signal_set.hpp" #include "boost/asio/signal_set.hpp"
#include "boost/scoped_ptr.hpp"
#include "webcc/globals.h" #include "webcc/globals.h"
#include "webcc/http_connection.h" #include "webcc/http_connection.h"
@ -29,6 +28,9 @@ class HttpServer {
void Run(); void Run();
private: private:
// Register to handle the signals that indicate when the server should exit.
void RegisterSignals();
// Initiate an asynchronous accept operation. // Initiate an asynchronous accept operation.
void AsyncAccept(); void AsyncAccept();
@ -38,17 +40,17 @@ class HttpServer {
// Get the handler for incoming requests. // Get the handler for incoming requests.
virtual HttpRequestHandler* GetRequestHandler() = 0; virtual HttpRequestHandler* GetRequestHandler() = 0;
// The number of worker threads.
std::size_t workers_;
// The io_context used to perform asynchronous operations. // The io_context used to perform asynchronous operations.
boost::asio::io_context io_context_; boost::asio::io_context io_context_;
// Acceptor used to listen for incoming connections.
boost::asio::ip::tcp::acceptor acceptor_;
// The signal_set is used to register for process termination notifications. // The signal_set is used to register for process termination notifications.
boost::asio::signal_set signals_; boost::asio::signal_set signals_;
// Acceptor used to listen for incoming connections. // The number of worker threads.
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_; std::size_t workers_;
}; };
} // namespace webcc } // namespace webcc

Loading…
Cancel
Save