|
|
@ -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,7 +58,7 @@ 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(),
|
|
|
@ -67,14 +67,14 @@ 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());
|
|
|
@ -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,21 +120,21 @@ 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.");
|
|
|
|
|
|
|
|
|
|
|
@ -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) {
|
|
|
|