Skip to content

Commit b80b0f5

Browse files
committed
reset the timeout timer when data is received
1 parent 4c8f1fe commit b80b0f5

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,21 @@ namespace web { namespace http
9191
}
9292
request_context::report_error(errorcodeValue, message);
9393
}
94-
94+
95+
void set_timer(const int secs)
96+
{
97+
m_timer.expires_from_now(boost::posix_time::milliseconds(secs * 1000));
98+
m_timer.async_wait(boost::bind(&linux_request_context::cancel, this, boost::asio::placeholders::error));
99+
}
100+
101+
void reset_timer(const int secs)
102+
{
103+
if ( m_timer.expires_from_now(boost::posix_time::milliseconds(secs * 1000)) > 0 )
104+
{
105+
m_timer.async_wait(boost::bind(&linux_request_context::cancel, this, boost::asio::placeholders::error));
106+
}
107+
}
108+
95109
tcp::socket m_socket;
96110
std::unique_ptr<boost::asio::ssl::stream<tcp::socket &>> m_ssl_stream;
97111
size_t m_known_size;
@@ -226,9 +240,7 @@ namespace web { namespace http
226240

227241
tcp::resolver::query query(host, utility::conversions::print_string(port));
228242

229-
const int secs = static_cast<int>(client_config().timeout().count());
230-
ctx->m_timer.expires_from_now(boost::posix_time::milliseconds(secs * 1000));
231-
ctx->m_timer.async_wait(boost::bind(&linux_request_context::cancel, ctx.get(), boost::asio::placeholders::error));
243+
ctx->set_timer(static_cast<int>(client_config().timeout().count()));
232244

233245
m_resolver.async_resolve(query, boost::bind(&linux_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
234246
}
@@ -565,20 +577,17 @@ namespace web { namespace http
565577
void async_read_until_buffersize(size_t size, ReadHandler handler, std::shared_ptr<linux_request_context> ctx)
566578
{
567579
size_t size_to_read = 0;
580+
if (ctx->m_body_buf.size() < size)
581+
{
582+
size_to_read = size - ctx->m_body_buf.size();
583+
}
584+
568585
if (ctx->m_ssl_stream)
569586
{
570-
if (ctx->m_body_buf.size() < size)
571-
{
572-
size_to_read = size - ctx->m_body_buf.size();
573-
}
574587
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
575588
}
576589
else
577590
{
578-
if (ctx->m_body_buf.size() < size)
579-
{
580-
size_to_read = size - ctx->m_body_buf.size();
581-
}
582591
boost::asio::async_read(ctx->m_socket, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
583592
}
584593
}
@@ -643,6 +652,7 @@ namespace web { namespace http
643652
}
644653
else
645654
{
655+
ctx->reset_timer(static_cast<int>(client_config().timeout().count()));
646656
auto writeBuffer = ctx->_get_writebuffer();
647657
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_body_buf.data()), to_read).then([=](pplx::task<size_t> op)
648658
{
@@ -695,6 +705,7 @@ namespace web { namespace http
695705

696706
if (ctx->m_current_size < ctx->m_known_size)
697707
{
708+
ctx->reset_timer(static_cast<int>(client_config().timeout().count()));
698709
// more data need to be read
699710
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_body_buf.data()),
700711
std::min(ctx->m_body_buf.size(), ctx->m_known_size - ctx->m_current_size)).then([=](pplx::task<size_t> op)

0 commit comments

Comments
 (0)