rework client using async api
This commit is contained in:
+24
-24
@@ -23,9 +23,6 @@ Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port,
|
||||
: protocol_(protocol),
|
||||
port_(port),
|
||||
doc_root_(doc_root),
|
||||
buffer_size_(kBufferSize),
|
||||
file_chunk_size_(1024),
|
||||
running_(false),
|
||||
acceptor_(io_context_),
|
||||
signals_(io_context_) {
|
||||
AddSignals();
|
||||
@@ -35,12 +32,12 @@ void Server::Run(std::size_t workers, std::size_t loops) {
|
||||
assert(workers > 0);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(state_mutex_);
|
||||
std::lock_guard<std::mutex> lock{ state_mutex_ };
|
||||
|
||||
assert(worker_threads_.empty());
|
||||
|
||||
if (IsRunning()) {
|
||||
LOG_WARN("Server is already running.");
|
||||
LOG_WARN("Server is already running");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -48,11 +45,11 @@ void Server::Run(std::size_t workers, std::size_t loops) {
|
||||
io_context_.restart();
|
||||
|
||||
if (!Listen(port_)) {
|
||||
LOG_ERRO("Server is NOT going to run.");
|
||||
LOG_ERRO("Server is NOT going to run");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO("Server is going to run...");
|
||||
LOG_INFO("Server is going to run");
|
||||
|
||||
AsyncWaitSignals();
|
||||
|
||||
@@ -70,7 +67,7 @@ void Server::Run(std::size_t workers, std::size_t loops) {
|
||||
// asynchronous operation outstanding: the asynchronous accept call waiting
|
||||
// for new incoming connections.
|
||||
|
||||
LOG_INFO("Loop is running in %u thread(s).", loops);
|
||||
LOG_INFO("Loop is running in %u thread(s)", loops);
|
||||
|
||||
if (loops == 1) {
|
||||
// Run the loop in current thread.
|
||||
@@ -88,7 +85,7 @@ void Server::Run(std::size_t workers, std::size_t loops) {
|
||||
}
|
||||
|
||||
void Server::Stop() {
|
||||
std::lock_guard<std::mutex> lock(state_mutex_);
|
||||
std::lock_guard<std::mutex> lock{ state_mutex_ };
|
||||
|
||||
DoStop();
|
||||
}
|
||||
@@ -112,7 +109,7 @@ void Server::AsyncWaitSignals() {
|
||||
// The server is stopped by canceling all outstanding asynchronous
|
||||
// operations. Once all operations have finished the io_context::run()
|
||||
// call will exit.
|
||||
LOG_INFO("On signal %d, stopping the server...", signo);
|
||||
LOG_INFO("On signal %d, stop the server", signo);
|
||||
|
||||
DoStop();
|
||||
});
|
||||
@@ -121,12 +118,12 @@ void Server::AsyncWaitSignals() {
|
||||
bool Server::Listen(std::uint16_t port) {
|
||||
boost::system::error_code ec;
|
||||
|
||||
tcp::endpoint endpoint(protocol_, port);
|
||||
tcp::endpoint endpoint{ protocol_, port };
|
||||
|
||||
// Open the acceptor.
|
||||
acceptor_.open(endpoint.protocol(), ec);
|
||||
if (ec) {
|
||||
LOG_ERRO("Acceptor open error (%s).", ec.message().c_str());
|
||||
LOG_ERRO("Acceptor open error (%s)", ec.message().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -141,7 +138,7 @@ bool Server::Listen(std::uint16_t port) {
|
||||
// Bind to the server address.
|
||||
acceptor_.bind(endpoint, ec);
|
||||
if (ec) {
|
||||
LOG_ERRO("Acceptor bind error (%s).", ec.message().c_str());
|
||||
LOG_ERRO("Acceptor bind error (%s)", ec.message().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -150,7 +147,7 @@ bool Server::Listen(std::uint16_t port) {
|
||||
// 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());
|
||||
LOG_ERRO("Acceptor listen error (%s)", ec.message().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -167,9 +164,10 @@ void Server::AsyncAccept() {
|
||||
}
|
||||
|
||||
if (!ec) {
|
||||
LOG_INFO("Accepted a connection.");
|
||||
LOG_INFO("Accepted a connection");
|
||||
|
||||
using namespace std::placeholders;
|
||||
|
||||
auto view_matcher = std::bind(&Server::MatchViewOrStatic, this, _1,
|
||||
_2, _3);
|
||||
|
||||
@@ -205,16 +203,16 @@ void Server::DoStop() {
|
||||
}
|
||||
|
||||
void Server::WorkerRoutine() {
|
||||
LOG_INFO("Worker is running.");
|
||||
LOG_INFO("Worker is running");
|
||||
|
||||
for (;;) {
|
||||
auto connection = queue_.PopOrWait();
|
||||
|
||||
if (!connection) {
|
||||
LOG_INFO("Worker is going to stop.");
|
||||
LOG_INFO("Worker is going to stop");
|
||||
|
||||
// For stopping next worker.
|
||||
queue_.Push(ConnectionPtr());
|
||||
queue_.Push({});
|
||||
|
||||
// Stop this worker.
|
||||
break;
|
||||
@@ -225,13 +223,13 @@ void Server::WorkerRoutine() {
|
||||
}
|
||||
|
||||
void Server::StopWorkers() {
|
||||
LOG_INFO("Stopping workers...");
|
||||
LOG_INFO("Stop workers");
|
||||
|
||||
// Clear/drop pending connections.
|
||||
// The connections will be closed later (see DoStop).
|
||||
// Alternatively, we can wait for the pending connections to be handled.
|
||||
if (queue_.Size() != 0) {
|
||||
LOG_INFO("Clear pending connections...");
|
||||
LOG_INFO("Clear pending connections");
|
||||
queue_.Clear();
|
||||
}
|
||||
|
||||
@@ -252,7 +250,7 @@ void Server::StopWorkers() {
|
||||
// last worker thread.
|
||||
queue_.Clear();
|
||||
|
||||
LOG_INFO("All workers have been stopped.");
|
||||
LOG_INFO("Workers stopped");
|
||||
}
|
||||
|
||||
void Server::Handle(ConnectionPtr connection) {
|
||||
@@ -307,7 +305,9 @@ bool Server::MatchViewOrStatic(const std::string& method,
|
||||
// Try to match a static file.
|
||||
if (method == methods::kGet && !doc_root_.empty()) {
|
||||
bfs::path path = doc_root_ / url;
|
||||
if (!bfs::is_directory(path) && bfs::exists(path)) {
|
||||
|
||||
boost::system::error_code ec;
|
||||
if (!bfs::is_directory(path, ec) && bfs::exists(path, ec)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -319,7 +319,7 @@ ResponsePtr Server::ServeStatic(RequestPtr request) {
|
||||
assert(request->method() == methods::kGet);
|
||||
|
||||
if (doc_root_.empty()) {
|
||||
LOG_INFO("The doc root was not specified.");
|
||||
LOG_INFO("The doc root was not specified");
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -340,7 +340,7 @@ ResponsePtr Server::ServeStatic(RequestPtr request) {
|
||||
return response;
|
||||
|
||||
} catch (const Error& error) {
|
||||
LOG_ERRO("File error: %s.", error.message().c_str());
|
||||
LOG_ERRO("File error: %s", error.message().c_str());
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user