Added timeout handling for keep-alive operations.
This commit is contained in:
parent
a0582a3e68
commit
22550d078f
@ -52,11 +52,15 @@ class Connection : public std::enable_shared_from_this<Connection>
|
||||
/// Handle completion of a write operation.
|
||||
void handle_write(const boost::system::error_code &e);
|
||||
|
||||
/// Handle read timeout
|
||||
void handle_timeout();
|
||||
|
||||
std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
|
||||
const http::compression_type compression_type);
|
||||
|
||||
boost::asio::io_service::strand strand;
|
||||
boost::asio::ip::tcp::socket TCP_socket;
|
||||
boost::asio::deadline_timer timer;
|
||||
RequestHandler &request_handler;
|
||||
RequestParser request_parser;
|
||||
boost::array<char, 8192> incoming_data_buffer;
|
||||
@ -68,6 +72,7 @@ class Connection : public std::enable_shared_from_this<Connection>
|
||||
//Keep alive support
|
||||
bool keep_alive = false;
|
||||
short processed_requests = 512;
|
||||
short keepalive_timeout = 5; // In seconds
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ namespace server
|
||||
{
|
||||
|
||||
Connection::Connection(boost::asio::io_service &io_service, RequestHandler &handler)
|
||||
: strand(io_service), TCP_socket(io_service), request_handler(handler)
|
||||
: strand(io_service), TCP_socket(io_service), timer(io_service), request_handler(handler)
|
||||
{
|
||||
}
|
||||
|
||||
@ -28,12 +28,19 @@ boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; }
|
||||
/// Start the first asynchronous operation for the connection.
|
||||
void Connection::start()
|
||||
{
|
||||
TCP_socket.async_read_some(
|
||||
TCP_socket.async_read_some(
|
||||
boost::asio::buffer(incoming_data_buffer),
|
||||
strand.wrap(boost::bind(&Connection::handle_read,
|
||||
this->shared_from_this(),
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred)));
|
||||
|
||||
//init async timer
|
||||
timer.cancel();
|
||||
timer.expires_from_now(boost::posix_time::seconds(keepalive_timeout));
|
||||
timer.async_wait(boost::bind(&Connection::handle_timeout,
|
||||
this->shared_from_this()));
|
||||
|
||||
}
|
||||
|
||||
void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
|
||||
@ -133,12 +140,18 @@ void Connection::handle_write(const boost::system::error_code &error)
|
||||
this->start();
|
||||
} else {
|
||||
// Initiate graceful connection closure.
|
||||
boost::system::error_code ignore_error;
|
||||
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
||||
handle_timeout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle completion of a write operation.
|
||||
void Connection::handle_timeout()
|
||||
{
|
||||
boost::system::error_code ignore_error;
|
||||
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
|
||||
}
|
||||
|
||||
std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data,
|
||||
const http::compression_type compression_type)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user