Replace deadline_timer with steady_timer.

master
Chunting Gu 6 years ago
parent 2035c1f552
commit 22b783ce97

@ -1,7 +1,5 @@
#include "webcc/client.h" #include "webcc/client.h"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "webcc/logger.h" #include "webcc/logger.h"
#include "webcc/utility.h" #include "webcc/utility.h"
@ -18,6 +16,8 @@ Client::Client()
timer_canceled_(false), timer_canceled_(false),
timed_out_(false), timed_out_(false),
error_(kNoError) { error_(kNoError) {
} }
bool Client::Request(RequestPtr request, bool connect) { bool Client::Request(RequestPtr request, bool connect) {
@ -144,7 +144,8 @@ Error Client::WriteReqeust(RequestPtr request) {
Error Client::ReadResponse() { Error Client::ReadResponse() {
LOG_VERB("Read response (timeout: %ds)...", timeout_); LOG_VERB("Read response (timeout: %ds)...", timeout_);
timer_.expires_from_now(boost::posix_time::seconds(timeout_)); timer_.expires_after(std::chrono::seconds(timeout_));
DoWaitTimer(); DoWaitTimer();
Error error = kNoError; Error error = kNoError;
@ -223,11 +224,11 @@ void Client::DoWaitTimer() {
} }
void Client::OnTimer(boost::system::error_code ec) { void Client::OnTimer(boost::system::error_code ec) {
LOG_VERB("On deadline timer."); LOG_VERB("On timer.");
// timer_.cancel() was called. // timer_.cancel() was called.
if (ec == boost::asio::error::operation_aborted) { if (ec == boost::asio::error::operation_aborted) {
LOG_VERB("Deadline timer canceled."); LOG_VERB("Timer canceled.");
return; return;
} }
@ -236,10 +237,9 @@ void Client::OnTimer(boost::system::error_code ec) {
return; return;
} }
if (timer_.expires_at() <= boost::asio::deadline_timer::traits_type::now()) { if (timer_.expiry() <= boost::asio::steady_timer::clock_type::now()) {
// The deadline has passed. // The deadline has passed. The socket is closed so that any outstanding
// The socket is closed so that any outstanding asynchronous operations // asynchronous operations are canceled.
// are canceled.
LOG_WARN("HTTP client timed out."); LOG_WARN("HTTP client timed out.");
timed_out_ = true; timed_out_ = true;
Close(); Close();
@ -255,7 +255,7 @@ void Client::CancelTimer() {
return; return;
} }
LOG_INFO("Cancel deadline timer..."); LOG_INFO("Cancel timer...");
timer_.cancel(); timer_.cancel();
timer_canceled_ = true; timer_canceled_ = true;

@ -6,9 +6,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "boost/asio/deadline_timer.hpp"
#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/steady_timer.hpp"
#include "webcc/globals.h" #include "webcc/globals.h"
#include "webcc/request.h" #include "webcc/request.h"
@ -92,7 +92,7 @@ private:
ResponseParser response_parser_; ResponseParser response_parser_;
// Timer for the timeout control. // Timer for the timeout control.
boost::asio::deadline_timer timer_; boost::asio::steady_timer timer_;
// The buffer for reading response. // The buffer for reading response.
std::vector<char> buffer_; std::vector<char> buffer_;

Loading…
Cancel
Save