From: Marcin Siodelski Date: Wed, 13 Jun 2018 21:03:18 +0000 (+0200) Subject: [5649] Manage HTTP client connection timeout. X-Git-Tag: Kea-1.4.0~1^2~3^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a93e7c28d3892d7f412118ecf17281e3683a67b6;p=thirdparty%2Fkea.git [5649] Manage HTTP client connection timeout. --- diff --git a/src/lib/http/client.cc b/src/lib/http/client.cc index dec4378eb2..bde07f09a5 100644 --- a/src/lib/http/client.cc +++ b/src/lib/http/client.cc @@ -153,6 +153,11 @@ private: void terminate(const boost::system::error_code& ec, const std::string& parsing_error = ""); + /// @brief This method schedules timer or reschedules existing timer. + /// + /// @param request_timeout New timer interval in milliseconds. + void scheduleTimer(const long request_timeout); + /// @brief Asynchronously sends data over the socket. /// /// The data sent over the socket are stored in the @c buf_. @@ -559,6 +564,14 @@ Connection::terminate(const boost::system::error_code& ec, } } +void +Connection::scheduleTimer(const long request_timeout) { + if (request_timeout > 0) { + timer_.setup(boost::bind(&Connection::timerCallback, this), request_timeout, + IntervalTimer::ONE_SHOT); + } +} + void Connection::doSend() { SocketCallback socket_cb(boost::bind(&Connection::sendCallback, shared_from_this(), @@ -598,8 +611,8 @@ Connection::connectCallback(const long request_timeout, const boost::system::err } else { // Setup request timer. - timer_.setup(boost::bind(&Connection::timerCallback, this), request_timeout, - IntervalTimer::ONE_SHOT); + scheduleTimer(request_timeout); + // Start sending the request asynchronously. doSend(); } @@ -621,6 +634,9 @@ Connection::sendCallback(const boost::system::error_code& ec, size_t length) { } } + // Sending is in progress, so push back the timeout. + scheduleTimer(timer_.getInterval()); + // If any data have been sent, remove it from the buffer and only leave the // portion that still has to be sent. if (length > 0) { @@ -654,6 +670,9 @@ Connection::receiveCallback(const boost::system::error_code& ec, size_t length) } } + // Receiving is in progress, so push back the timeout. + scheduleTimer(timer_.getInterval()); + // If we have received any data, let's feed the parser with it. if (length != 0) { parser_->postBuffer(static_cast(input_buf_.data()), length);