Rename async functions and the handlers like DoXxx, OnXxx.
This commit is contained in:
+20
-20
@@ -35,7 +35,7 @@ void HttpAsyncClient::Request(std::shared_ptr<HttpRequest> request,
|
|||||||
response_handler_ = response_handler;
|
response_handler_ = response_handler;
|
||||||
|
|
||||||
resolver_.async_resolve(tcp::v4(), request->host(), request->port(kHttpPort),
|
resolver_.async_resolve(tcp::v4(), request->host(), request->port(kHttpPort),
|
||||||
std::bind(&HttpAsyncClient::ResolveHandler,
|
std::bind(&HttpAsyncClient::OnResolve,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
@@ -58,8 +58,8 @@ void HttpAsyncClient::Stop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
|
void HttpAsyncClient::OnResolve(boost::system::error_code ec,
|
||||||
tcp::resolver::results_type endpoints) {
|
tcp::resolver::results_type endpoints) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Can't resolve host (%s): %s, %s", ec.message().c_str(),
|
LOG_ERRO("Can't resolve host (%s): %s, %s", ec.message().c_str(),
|
||||||
request_->host().c_str(), request_->port().c_str());
|
request_->host().c_str(), request_->port().c_str());
|
||||||
@@ -67,15 +67,15 @@ void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
|
|||||||
} else {
|
} else {
|
||||||
// ConnectHandler: void(boost::system::error_code, tcp::endpoint)
|
// ConnectHandler: void(boost::system::error_code, tcp::endpoint)
|
||||||
boost::asio::async_connect(socket_, endpoints,
|
boost::asio::async_connect(socket_, endpoints,
|
||||||
std::bind(&HttpAsyncClient::ConnectHandler,
|
std::bind(&HttpAsyncClient::OnConnect,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::ConnectHandler(boost::system::error_code ec,
|
void HttpAsyncClient::OnConnect(boost::system::error_code ec,
|
||||||
tcp::endpoint endpoint) {
|
tcp::endpoint endpoint) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
|
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
|
||||||
Stop();
|
Stop();
|
||||||
@@ -95,22 +95,22 @@ void HttpAsyncClient::ConnectHandler(boost::system::error_code ec,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connection established.
|
// Connection established.
|
||||||
AsyncWrite();
|
DoWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::AsyncWrite() {
|
void HttpAsyncClient::DoWrite() {
|
||||||
if (stopped_) {
|
if (stopped_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::asio::async_write(socket_,
|
boost::asio::async_write(socket_,
|
||||||
request_->ToBuffers(),
|
request_->ToBuffers(),
|
||||||
std::bind(&HttpAsyncClient::WriteHandler,
|
std::bind(&HttpAsyncClient::OnWrite,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1));
|
std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
|
void HttpAsyncClient::OnWrite(boost::system::error_code ec) {
|
||||||
if (stopped_) {
|
if (stopped_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -120,22 +120,22 @@ void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
|
|||||||
response_handler_(response_, kSocketWriteError, timed_out_);
|
response_handler_(response_, kSocketWriteError, timed_out_);
|
||||||
} else {
|
} else {
|
||||||
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
|
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
|
||||||
AsyncWaitDeadline();
|
DoWaitDeadline();
|
||||||
|
|
||||||
AsyncRead();
|
DoRead();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::AsyncRead() {
|
void HttpAsyncClient::DoRead() {
|
||||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||||
std::bind(&HttpAsyncClient::ReadHandler,
|
std::bind(&HttpAsyncClient::OnRead,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
|
void HttpAsyncClient::OnRead(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
LOG_VERB("Socket async read handler.");
|
LOG_VERB("Socket async read handler.");
|
||||||
|
|
||||||
if (ec || length == 0) {
|
if (ec || length == 0) {
|
||||||
@@ -173,16 +173,16 @@ void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!stopped_) {
|
if (!stopped_) {
|
||||||
AsyncRead();
|
DoRead();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::AsyncWaitDeadline() {
|
void HttpAsyncClient::DoWaitDeadline() {
|
||||||
deadline_.async_wait(std::bind(&HttpAsyncClient::DeadlineHandler,
|
deadline_.async_wait(std::bind(&HttpAsyncClient::OnDeadline,
|
||||||
shared_from_this(), std::placeholders::_1));
|
shared_from_this(), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpAsyncClient::DeadlineHandler(boost::system::error_code ec) {
|
void HttpAsyncClient::OnDeadline(boost::system::error_code ec) {
|
||||||
LOG_VERB("Deadline handler.");
|
LOG_VERB("Deadline handler.");
|
||||||
|
|
||||||
if (ec == boost::asio::error::operation_aborted) {
|
if (ec == boost::asio::error::operation_aborted) {
|
||||||
|
|||||||
@@ -45,19 +45,19 @@ class HttpAsyncClient : public std::enable_shared_from_this<HttpAsyncClient> {
|
|||||||
private:
|
private:
|
||||||
using tcp = boost::asio::ip::tcp;
|
using tcp = boost::asio::ip::tcp;
|
||||||
|
|
||||||
void ResolveHandler(boost::system::error_code ec,
|
void OnResolve(boost::system::error_code ec,
|
||||||
tcp::resolver::results_type results);
|
tcp::resolver::results_type results);
|
||||||
|
|
||||||
void ConnectHandler(boost::system::error_code ec, tcp::endpoint endpoint);
|
void OnConnect(boost::system::error_code ec, tcp::endpoint endpoint);
|
||||||
|
|
||||||
void AsyncWrite();
|
void DoWrite();
|
||||||
void WriteHandler(boost::system::error_code ec);
|
void OnWrite(boost::system::error_code ec);
|
||||||
|
|
||||||
void AsyncRead();
|
void DoRead();
|
||||||
void ReadHandler(boost::system::error_code ec, std::size_t length);
|
void OnRead(boost::system::error_code ec, std::size_t length);
|
||||||
|
|
||||||
void AsyncWaitDeadline();
|
void DoWaitDeadline();
|
||||||
void DeadlineHandler(boost::system::error_code ec);
|
void OnDeadline(boost::system::error_code ec);
|
||||||
|
|
||||||
tcp::resolver resolver_;
|
tcp::resolver resolver_;
|
||||||
tcp::socket socket_;
|
tcp::socket socket_;
|
||||||
|
|||||||
+12
-12
@@ -20,7 +20,7 @@ HttpConnection::HttpConnection(boost::asio::ip::tcp::socket socket,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::Start() {
|
void HttpConnection::Start() {
|
||||||
AsyncRead();
|
DoRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::Close() {
|
void HttpConnection::Close() {
|
||||||
@@ -41,19 +41,19 @@ void HttpConnection::SetResponseContent(std::string&& content,
|
|||||||
void HttpConnection::SendResponse(HttpStatus::Enum status) {
|
void HttpConnection::SendResponse(HttpStatus::Enum status) {
|
||||||
response_.set_status(status);
|
response_.set_status(status);
|
||||||
response_.UpdateStartLine();
|
response_.UpdateStartLine();
|
||||||
AsyncWrite();
|
DoWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::AsyncRead() {
|
void HttpConnection::DoRead() {
|
||||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||||
std::bind(&HttpConnection::ReadHandler,
|
std::bind(&HttpConnection::OnRead,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::ReadHandler(boost::system::error_code ec,
|
void HttpConnection::OnRead(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Socket read error: %s", ec.message().c_str());
|
LOG_ERRO("Socket read error: %s", ec.message().c_str());
|
||||||
if (ec != boost::asio::error::operation_aborted) {
|
if (ec != boost::asio::error::operation_aborted) {
|
||||||
@@ -66,13 +66,13 @@ void HttpConnection::ReadHandler(boost::system::error_code ec,
|
|||||||
// Bad request.
|
// Bad request.
|
||||||
LOG_ERRO("Failed to parse HTTP request.");
|
LOG_ERRO("Failed to parse HTTP request.");
|
||||||
response_ = HttpResponse::Fault(HttpStatus::kBadRequest);
|
response_ = HttpResponse::Fault(HttpStatus::kBadRequest);
|
||||||
AsyncWrite();
|
DoWrite();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!request_parser_.finished()) {
|
if (!request_parser_.finished()) {
|
||||||
// Continue to read the request.
|
// Continue to read the request.
|
||||||
AsyncRead();
|
DoRead();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,12 +83,12 @@ void HttpConnection::ReadHandler(boost::system::error_code ec,
|
|||||||
request_handler_->Enqueue(shared_from_this());
|
request_handler_->Enqueue(shared_from_this());
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpConnection::AsyncWrite() {
|
void HttpConnection::DoWrite() {
|
||||||
LOG_VERB("HTTP response:\n%s", response_.Dump(4, "> ").c_str());
|
LOG_VERB("HTTP response:\n%s", response_.Dump(4, "> ").c_str());
|
||||||
|
|
||||||
boost::asio::async_write(socket_,
|
boost::asio::async_write(socket_,
|
||||||
response_.ToBuffers(),
|
response_.ToBuffers(),
|
||||||
std::bind(&HttpConnection::WriteHandler,
|
std::bind(&HttpConnection::OnWrite,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
std::placeholders::_2));
|
std::placeholders::_2));
|
||||||
@@ -98,8 +98,8 @@ void HttpConnection::AsyncWrite() {
|
|||||||
// This write handler will be called from main thread (the thread calling
|
// This write handler will be called from main thread (the thread calling
|
||||||
// io_context.run), even though AsyncWrite() is invoked by worker threads.
|
// io_context.run), even though AsyncWrite() is invoked by worker threads.
|
||||||
// This is ensured by Asio.
|
// This is ensured by Asio.
|
||||||
void HttpConnection::WriteHandler(boost::system::error_code ec,
|
void HttpConnection::OnWrite(boost::system::error_code ec,
|
||||||
std::size_t length) {
|
std::size_t length) {
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Socket write error: %s", ec.message().c_str());
|
LOG_ERRO("Socket write error: %s", ec.message().c_str());
|
||||||
|
|
||||||
|
|||||||
@@ -39,11 +39,11 @@ class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
|
|||||||
void SendResponse(HttpStatus::Enum status);
|
void SendResponse(HttpStatus::Enum status);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void AsyncRead();
|
void DoRead();
|
||||||
void ReadHandler(boost::system::error_code ec, std::size_t length);
|
void OnRead(boost::system::error_code ec, std::size_t length);
|
||||||
|
|
||||||
void AsyncWrite();
|
void DoWrite();
|
||||||
void WriteHandler(boost::system::error_code ec, std::size_t length);
|
void OnWrite(boost::system::error_code ec, std::size_t length);
|
||||||
|
|
||||||
// Shutdown the socket.
|
// Shutdown the socket.
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|||||||
@@ -61,9 +61,9 @@ void HttpServer::Run() {
|
|||||||
|
|
||||||
LOG_INFO("Server is going to run...");
|
LOG_INFO("Server is going to run...");
|
||||||
|
|
||||||
AsyncAwaitStop();
|
DoAwaitStop();
|
||||||
|
|
||||||
AsyncAccept();
|
DoAccept();
|
||||||
|
|
||||||
// Start worker threads.
|
// Start worker threads.
|
||||||
GetRequestHandler()->Start(workers_);
|
GetRequestHandler()->Start(workers_);
|
||||||
@@ -84,7 +84,7 @@ void HttpServer::RegisterSignals() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpServer::AsyncAccept() {
|
void HttpServer::DoAccept() {
|
||||||
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
|
||||||
@@ -96,17 +96,15 @@ void HttpServer::AsyncAccept() {
|
|||||||
if (!ec) {
|
if (!ec) {
|
||||||
LOG_INFO("Accepted a connection.");
|
LOG_INFO("Accepted a connection.");
|
||||||
|
|
||||||
HttpConnectionPtr connection{
|
std::make_shared<HttpConnection>(std::move(socket),
|
||||||
new HttpConnection(std::move(socket), GetRequestHandler())
|
GetRequestHandler())->Start();
|
||||||
};
|
|
||||||
connection->Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncAccept();
|
DoAccept();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpServer::AsyncAwaitStop() {
|
void HttpServer::DoAwaitStop() {
|
||||||
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
|
||||||
|
|||||||
+2
-2
@@ -32,10 +32,10 @@ class HttpServer {
|
|||||||
void RegisterSignals();
|
void RegisterSignals();
|
||||||
|
|
||||||
// Initiate an asynchronous accept operation.
|
// Initiate an asynchronous accept operation.
|
||||||
void AsyncAccept();
|
void DoAccept();
|
||||||
|
|
||||||
// Wait for a request to stop the server.
|
// Wait for a request to stop the server.
|
||||||
void AsyncAwaitStop();
|
void DoAwaitStop();
|
||||||
|
|
||||||
// Get the handler for incoming requests.
|
// Get the handler for incoming requests.
|
||||||
virtual HttpRequestHandler* GetRequestHandler() = 0;
|
virtual HttpRequestHandler* GetRequestHandler() = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user