Rename async functions and the handlers like DoXxx, OnXxx.

master
Chunting Gu 7 years ago
parent c0b61a31a2
commit 3cab68cf33

@ -35,7 +35,7 @@ void HttpAsyncClient::Request(std::shared_ptr<HttpRequest> request,
response_handler_ = response_handler;
resolver_.async_resolve(tcp::v4(), request->host(), request->port(kHttpPort),
std::bind(&HttpAsyncClient::ResolveHandler,
std::bind(&HttpAsyncClient::OnResolve,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
@ -58,8 +58,8 @@ void HttpAsyncClient::Stop() {
}
}
void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
tcp::resolver::results_type endpoints) {
void HttpAsyncClient::OnResolve(boost::system::error_code ec,
tcp::resolver::results_type endpoints) {
if (ec) {
LOG_ERRO("Can't resolve host (%s): %s, %s", ec.message().c_str(),
request_->host().c_str(), request_->port().c_str());
@ -67,15 +67,15 @@ void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
} else {
// ConnectHandler: void(boost::system::error_code, tcp::endpoint)
boost::asio::async_connect(socket_, endpoints,
std::bind(&HttpAsyncClient::ConnectHandler,
std::bind(&HttpAsyncClient::OnConnect,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
}
void HttpAsyncClient::ConnectHandler(boost::system::error_code ec,
tcp::endpoint endpoint) {
void HttpAsyncClient::OnConnect(boost::system::error_code ec,
tcp::endpoint endpoint) {
if (ec) {
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
Stop();
@ -95,22 +95,22 @@ void HttpAsyncClient::ConnectHandler(boost::system::error_code ec,
}
// Connection established.
AsyncWrite();
DoWrite();
}
void HttpAsyncClient::AsyncWrite() {
void HttpAsyncClient::DoWrite() {
if (stopped_) {
return;
}
boost::asio::async_write(socket_,
request_->ToBuffers(),
std::bind(&HttpAsyncClient::WriteHandler,
std::bind(&HttpAsyncClient::OnWrite,
shared_from_this(),
std::placeholders::_1));
}
void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
void HttpAsyncClient::OnWrite(boost::system::error_code ec) {
if (stopped_) {
return;
}
@ -120,22 +120,22 @@ void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
response_handler_(response_, kSocketWriteError, timed_out_);
} else {
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_),
std::bind(&HttpAsyncClient::ReadHandler,
std::bind(&HttpAsyncClient::OnRead,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
std::size_t length) {
void HttpAsyncClient::OnRead(boost::system::error_code ec,
std::size_t length) {
LOG_VERB("Socket async read handler.");
if (ec || length == 0) {
@ -173,16 +173,16 @@ void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
}
if (!stopped_) {
AsyncRead();
DoRead();
}
}
void HttpAsyncClient::AsyncWaitDeadline() {
deadline_.async_wait(std::bind(&HttpAsyncClient::DeadlineHandler,
void HttpAsyncClient::DoWaitDeadline() {
deadline_.async_wait(std::bind(&HttpAsyncClient::OnDeadline,
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.");
if (ec == boost::asio::error::operation_aborted) {

@ -45,19 +45,19 @@ class HttpAsyncClient : public std::enable_shared_from_this<HttpAsyncClient> {
private:
using tcp = boost::asio::ip::tcp;
void ResolveHandler(boost::system::error_code ec,
tcp::resolver::results_type results);
void OnResolve(boost::system::error_code ec,
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 WriteHandler(boost::system::error_code ec);
void DoWrite();
void OnWrite(boost::system::error_code ec);
void AsyncRead();
void ReadHandler(boost::system::error_code ec, std::size_t length);
void DoRead();
void OnRead(boost::system::error_code ec, std::size_t length);
void AsyncWaitDeadline();
void DeadlineHandler(boost::system::error_code ec);
void DoWaitDeadline();
void OnDeadline(boost::system::error_code ec);
tcp::resolver resolver_;
tcp::socket socket_;

@ -20,7 +20,7 @@ HttpConnection::HttpConnection(boost::asio::ip::tcp::socket socket,
}
void HttpConnection::Start() {
AsyncRead();
DoRead();
}
void HttpConnection::Close() {
@ -41,19 +41,19 @@ void HttpConnection::SetResponseContent(std::string&& content,
void HttpConnection::SendResponse(HttpStatus::Enum status) {
response_.set_status(status);
response_.UpdateStartLine();
AsyncWrite();
DoWrite();
}
void HttpConnection::AsyncRead() {
void HttpConnection::DoRead() {
socket_.async_read_some(boost::asio::buffer(buffer_),
std::bind(&HttpConnection::ReadHandler,
std::bind(&HttpConnection::OnRead,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void HttpConnection::ReadHandler(boost::system::error_code ec,
std::size_t length) {
void HttpConnection::OnRead(boost::system::error_code ec,
std::size_t length) {
if (ec) {
LOG_ERRO("Socket read error: %s", ec.message().c_str());
if (ec != boost::asio::error::operation_aborted) {
@ -66,13 +66,13 @@ void HttpConnection::ReadHandler(boost::system::error_code ec,
// Bad request.
LOG_ERRO("Failed to parse HTTP request.");
response_ = HttpResponse::Fault(HttpStatus::kBadRequest);
AsyncWrite();
DoWrite();
return;
}
if (!request_parser_.finished()) {
// Continue to read the request.
AsyncRead();
DoRead();
return;
}
@ -83,12 +83,12 @@ void HttpConnection::ReadHandler(boost::system::error_code ec,
request_handler_->Enqueue(shared_from_this());
}
void HttpConnection::AsyncWrite() {
void HttpConnection::DoWrite() {
LOG_VERB("HTTP response:\n%s", response_.Dump(4, "> ").c_str());
boost::asio::async_write(socket_,
response_.ToBuffers(),
std::bind(&HttpConnection::WriteHandler,
std::bind(&HttpConnection::OnWrite,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
@ -98,8 +98,8 @@ void HttpConnection::AsyncWrite() {
// This write handler will be called from main thread (the thread calling
// io_context.run), even though AsyncWrite() is invoked by worker threads.
// This is ensured by Asio.
void HttpConnection::WriteHandler(boost::system::error_code ec,
std::size_t length) {
void HttpConnection::OnWrite(boost::system::error_code ec,
std::size_t length) {
if (ec) {
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);
private:
void AsyncRead();
void ReadHandler(boost::system::error_code ec, std::size_t length);
void DoRead();
void OnRead(boost::system::error_code ec, std::size_t length);
void AsyncWrite();
void WriteHandler(boost::system::error_code ec, std::size_t length);
void DoWrite();
void OnWrite(boost::system::error_code ec, std::size_t length);
// Shutdown the socket.
void Shutdown();

@ -61,9 +61,9 @@ void HttpServer::Run() {
LOG_INFO("Server is going to run...");
AsyncAwaitStop();
DoAwaitStop();
AsyncAccept();
DoAccept();
// Start worker threads.
GetRequestHandler()->Start(workers_);
@ -84,7 +84,7 @@ void HttpServer::RegisterSignals() {
#endif
}
void HttpServer::AsyncAccept() {
void HttpServer::DoAccept() {
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket) {
// Check whether the server was stopped by a signal before this
@ -96,17 +96,15 @@ void HttpServer::AsyncAccept() {
if (!ec) {
LOG_INFO("Accepted a connection.");
HttpConnectionPtr connection{
new HttpConnection(std::move(socket), GetRequestHandler())
};
connection->Start();
std::make_shared<HttpConnection>(std::move(socket),
GetRequestHandler())->Start();
}
AsyncAccept();
DoAccept();
});
}
void HttpServer::AsyncAwaitStop() {
void HttpServer::DoAwaitStop() {
signals_.async_wait(
[this](boost::system::error_code, int signo) {
// The server is stopped by canceling all outstanding asynchronous

@ -32,10 +32,10 @@ class HttpServer {
void RegisterSignals();
// Initiate an asynchronous accept operation.
void AsyncAccept();
void DoAccept();
// Wait for a request to stop the server.
void AsyncAwaitStop();
void DoAwaitStop();
// Get the handler for incoming requests.
virtual HttpRequestHandler* GetRequestHandler() = 0;

Loading…
Cancel
Save