Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactored HTTP client v2.#476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
deanberris merged 17 commits intocpp-netlib:masterfromglynos:master
Jan 2, 2015
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
0b49d2b
Added a facility to track upload/download progress.
glynosFeb 16, 2014
1bc435b
Removed ssl_connection from client.
glynosFeb 17, 2014
2b1c0e2
Added transfer direction callback.
glynosMar 23, 2014
d00f99f
Renamed request_helper as request_context.
glynosMar 23, 2014
47ced49
Updated uri submodule.
glynosMar 26, 2014
03dddb2
Removed noexcept from destructors; added some flags for chunked trans…
glynosApr 12, 2014
ccebaeb
Formatted source code and add source comments.
glynosMay 11, 2014
ad273ec
Added some comments; fixed tests so that they compile for Boost.Optio…
glynosSep 10, 2014
862263d
Updated uri submodule.
glynosSep 10, 2014
4f2f0e2
Formatted files with clang-format.
glynosSep 10, 2014
907a176
Updated uri submodule.
glynosSep 21, 2014
ff5f714
Merge remote-tracking branch 'upstream/master'
glynosDec 19, 2014
4e0add7
Use std::unique_ptr for pimpl idiom in HTTP client.
glynosDec 24, 2014
6771a55
Added connection timeout check to HTTP client.
glynosDec 24, 2014
50f3a4c
Fixed bug in error handling in the HTTP client.
glynosDec 24, 2014
e2da305
Minor refactoring in the HTTP client.
glynosDec 24, 2014
42ab79d
Simplified the HTTP client, request and response classes.
glynosDec 25, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Added connection timeout check to HTTP client.
  • Loading branch information
@glynos
glynos committedDec 24, 2014
commit6771a559964c827219354008394055d6c5548102
90 changes: 70 additions & 20 deletionshttp/src/http/v2/client/client.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,12 +33,6 @@ namespace network {
boost::asio::streambuf request_buffer_;
boost::asio::streambuf response_buffer_;

// TODO configure chunked transfer encoding
bool chunked_;

// TODO configure deadline timer for timeouts
bool timedout_;

std::uint64_t total_bytes_written_, total_bytes_read_;

request_context(
Expand All@@ -47,8 +41,6 @@ namespace network {
: connection_(connection),
request_(request),
options_(options),
chunked_(false),
timedout_(false),
total_bytes_written_(0),
total_bytes_read_(0) {}
};
Expand All@@ -63,8 +55,14 @@ namespace network {

~impl();

void set_error(const boost::system::error_code &ec,
std::shared_ptr<request_context> context);

std::future<response> execute(std::shared_ptr<request_context> context);

void timeout(const boost::system::error_code &ec,
std::shared_ptr<request_context> context);

void connect(const boost::system::error_code &ec,
tcp::resolver::iterator endpoint_iterator,
std::shared_ptr<request_context> context);
Expand DownExpand Up@@ -101,7 +99,11 @@ namespace network {
boost::asio::io_service::strand strand_;
std::unique_ptr<client_connection::async_resolver> resolver_;
std::shared_ptr<client_connection::async_connection> mock_connection_;
// TODO configure deadline timer for timeouts
bool timedout_;
boost::asio::deadline_timer timer_;
std::thread lifetime_thread_;

};

client::impl::impl(client_options options)
Expand All@@ -110,6 +112,8 @@ namespace network {
strand_(io_service_),
resolver_(new client_connection::tcp_resolver(
io_service_, options_.cache_resolved())),
timedout_(false),
timer_(io_service_),
lifetime_thread_([=]() { io_service_.run(); }) {}

client::impl::impl(
Expand All@@ -120,13 +124,23 @@ namespace network {
sentinel_(new boost::asio::io_service::work(io_service_)),
strand_(io_service_),
resolver_(std::move(mock_resolver)),
timedout_(false),
timer_(io_service_),
lifetime_thread_([=]() { io_service_.run(); }) {}

client::impl::~impl() {
sentinel_.reset();
lifetime_thread_.join();
}


void client::impl::set_error(const boost::system::error_code &ec,
std::shared_ptr<request_context> context) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
timer_.cancel();
}

std::future<response> client::impl::execute(
std::shared_ptr<request_context> context) {
std::future<response> res = context->response_promise_.get_future();
Expand All@@ -153,15 +167,29 @@ namespace network {
connect(ec, endpoint_iterator, context);
}));

if (options_.timeout() > std::chrono::milliseconds(0)) {
timer_.expires_from_now(boost::posix_time::milliseconds(options_.timeout().count()));
timer_.async_wait(strand_.wrap([=](const boost::system::error_code &ec) {
timeout(ec, context);
}));
}

return res;
}

void client::impl::timeout(const boost::system::error_code &ec,
std::shared_ptr<request_context> context) {
if (!ec) {
context->connection_->disconnect();
}
timedout_ = true;
}

void client::impl::connect(const boost::system::error_code &ec,
tcp::resolver::iterator endpoint_iterator,
std::shared_ptr<request_context> context) {
if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand All@@ -188,9 +216,13 @@ namespace network {
void client::impl::write_request(
const boost::system::error_code &ec,
std::shared_ptr<request_context> context) {
if (timedout_) {
set_error(boost::asio::error::timed_out, context);
return;
}

if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand All@@ -200,6 +232,7 @@ namespace network {
if (!request_stream) {
context->response_promise_.set_exception(std::make_exception_ptr(
client_exception(client_error::invalid_request)));
timer_.cancel();
}

context->connection_->async_write(
Expand All@@ -213,9 +246,13 @@ namespace network {
void client::impl::write_body(const boost::system::error_code &ec,
std::size_t bytes_written,
std::shared_ptr<request_context> context) {
if (timedout_) {
set_error(boost::asio::error::timed_out, context);
return;
}

if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand DownExpand Up@@ -245,9 +282,13 @@ namespace network {
void client::impl::read_response(
const boost::system::error_code &ec, std::size_t bytes_written,
std::shared_ptr<request_context> context) {
if (timedout_) {
set_error(boost::asio::error::timed_out, context);
return;
}

if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand All@@ -272,9 +313,13 @@ namespace network {
const boost::system::error_code &ec, std::size_t,
std::shared_ptr<request_context> context,
std::shared_ptr<response> res) {
if (timedout_) {
set_error(boost::asio::error::timed_out, context);
return;
}

if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand DownExpand Up@@ -304,9 +349,13 @@ namespace network {
const boost::system::error_code &ec, std::size_t,
std::shared_ptr<request_context> context,
std::shared_ptr<response> res) {
if (timedout_) {
set_error(boost::asio::error::timed_out, context);
return;
}

if (ec) {
context->response_promise_.set_exception(std::make_exception_ptr(
std::system_error(ec.value(), std::system_category())));
set_error(ec, context);
return;
}

Expand DownExpand Up@@ -370,6 +419,7 @@ namespace network {
// If there's no data else to read, then set the response and exit.
if (bytes_read == 0) {
context->response_promise_.set_value(*res);
timer_.cancel();
return;
}

Expand Down
1 change: 1 addition & 0 deletionshttp/src/network/http/v2/client/client.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
#include <vector>
#include <chrono>
#include <boost/asio/io_service.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/optional.hpp>
#include <network/config.hpp>
#include <network/version.hpp>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,6 +94,11 @@ namespace network {
virtual void async_read(boost::asio::streambuf &command_streambuf,
read_callback callback) = 0;

/**
* \brief Breaks the connection.
*/
virtual void disconnect() = 0;

/**
* \brief Cancels an operation on a connection.
*/
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,6 +75,16 @@ namespace network {
boost::asio::transfer_at_least(1), callback);
}

virtual void disconnect() {
if (socket_ && socket_->is_open()) {
boost::system::error_code ec;
socket_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if (!ec) {
socket_->close(ec);
}
}
}

virtual void cancel() {
socket_->cancel();
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -100,8 +100,18 @@ namespace network {
boost::asio::transfer_at_least(1), callback);
}

virtual void disconnect() {
if (socket_ && socket_->lowest_layer().is_open()) {
boost::system::error_code ec;
socket_->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if (!ec) {
socket_->lowest_layer().close(ec);
}
}
}

virtual void cancel() {
//socket_->cancel();
socket_->lowest_layer().cancel();
}

private:
Expand Down
2 changes: 2 additions & 0 deletionshttp/test/v2/client/units/client_resolution_test.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,6 +51,8 @@ class fake_async_connection : public http_cc::async_connection {
virtual void async_read(boost::asio::streambuf &,
read_callback) { }

virtual void disconnect() { }

virtual void cancel() { }

};
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp