Skip to content

Replace std::bind with lambdas #598

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 26 additions & 24 deletions boost/network/protocol/http/policies/async_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@ struct async_connection_policy : resolver_policy<Tag>::type {
typedef typename resolver_policy<Tag>::type resolver_base;
typedef typename resolver_base::resolver_type resolver_type;
typedef typename resolver_base::resolve_function resolve_function;
typedef typename resolver_base::resolve_completion_function
resolve_completion_function;
typedef std::function<void(iterator_range<char const*> const&,
std::error_code const&)> body_callback_function_type;
std::error_code const&)>
body_callback_function_type;
typedef std::function<bool(string_type&)> body_generator_function_type;

struct connection_impl {
connection_impl(bool follow_redirect, bool always_verify_peer,
resolve_function resolve, resolver_type& resolver,
bool https, int timeout,
optional<string_type> /*unused*/const& certificate_filename,
optional<string_type> const& verify_path,
optional<string_type> const& certificate_file,
optional<string_type> const& private_key_file,
optional<string_type> const& ciphers, long ssl_options) {
pimpl = impl::async_connection_base<
Tag, version_major,
version_minor>::new_connection(resolve, resolver, follow_redirect,
always_verify_peer, https, timeout,
certificate_filename, verify_path,
certificate_file, private_key_file,
ciphers, ssl_options);
connection_impl(
bool follow_redirect, bool always_verify_peer, resolve_function resolve,
resolver_type& resolver, bool https, int timeout,
optional<string_type> /*unused*/ const& certificate_filename,
optional<string_type> const& verify_path,
optional<string_type> const& certificate_file,
optional<string_type> const& private_key_file,
optional<string_type> const& ciphers, long ssl_options) {
pimpl = impl::async_connection_base<Tag, version_major, version_minor>::
new_connection(resolve, resolver, follow_redirect, always_verify_peer,
https, timeout, certificate_filename, verify_path,
certificate_file, private_key_file, ciphers,
ssl_options);
}

basic_response<Tag> send_request(string_type /*unused*/const& method,
basic_response<Tag> send_request(string_type /*unused*/ const& method,
basic_request<Tag> const& request_,
bool get_body,
body_callback_function_type callback,
Expand All @@ -60,14 +61,14 @@ struct async_connection_policy : resolver_policy<Tag>::type {

private:
std::shared_ptr<http::impl::async_connection_base<Tag, version_major,
version_minor> > pimpl;
version_minor> > pimpl;
};

typedef std::shared_ptr<connection_impl> connection_ptr;
connection_ptr get_connection(
resolver_type& resolver, basic_request<Tag> const& request_,
bool always_verify_peer,
optional<string_type> /*unused*/const& certificate_filename =
optional<string_type> const& certificate_filename =
optional<string_type>(),
optional<string_type> const& verify_path = optional<string_type>(),
optional<string_type> const& certificate_file = optional<string_type>(),
Expand All @@ -76,14 +77,15 @@ struct async_connection_policy : resolver_policy<Tag>::type {
long ssl_options = 0) {
string_type protocol_ = protocol(request_);
namespace ph = std::placeholders;
connection_ptr connection_(new connection_impl(
return std::make_shared<connection_impl>(
follow_redirect_, always_verify_peer,
std::bind(&async_connection_policy<Tag, version_major,
version_minor>::resolve, this, ph::_1, ph::_2, ph::_3, ph::_4),
[this](resolver_type& resolver, string_type const& host,
std::uint16_t port, resolve_completion_function once_resolved) {
this->resolve(resolver, host, port, once_resolved);
},
resolver, boost::iequals(protocol_, string_type("https")), timeout_,
certificate_filename, verify_path, certificate_file, private_key_file,
ciphers, ssl_options));
return connection_;
ciphers, ssl_options);
}

void cleanup() {}
Expand Down
28 changes: 12 additions & 16 deletions boost/network/protocol/http/policies/async_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
typedef typename string<Tag>::type string_type;
typedef std::unordered_map<string_type, resolver_iterator_pair>
endpoint_cache;
typedef std::function<
void(std::error_code const &, resolver_iterator_pair)>
typedef std::function<void(std::error_code const &, resolver_iterator_pair)>
resolve_completion_function;
typedef std::function<void(resolver_type &, string_type, std::uint16_t,
resolve_completion_function)> resolve_function;
Expand All @@ -47,8 +46,7 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
: cache_resolved_(cache_resolved), endpoint_cache_() {}

void resolve(resolver_type &resolver_, string_type const &host,
std::uint16_t port,
resolve_completion_function once_resolved) {
std::uint16_t port, resolve_completion_function once_resolved) {
if (cache_resolved_) {
typename endpoint_cache::iterator iter =
endpoint_cache_.find(boost::to_lower_copy(host));
Expand All @@ -59,27 +57,25 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
}
}

typename resolver_type::query q(host,
std::to_string(port));
typename resolver_type::query q(host, std::to_string(port));
auto self = this->shared_from_this();
resolver_.async_resolve(q, resolver_strand_->wrap([=] (std::error_code const &ec,
resolver_iterator endpoint_iterator) {
self->handle_resolve(boost::to_lower_copy(host),
once_resolved,
ec, endpoint_iterator);
}));
resolver_.async_resolve(
q, resolver_strand_->wrap([=](std::error_code const &ec,
resolver_iterator endpoint_iterator) {
self->handle_resolve(boost::to_lower_copy(host), once_resolved, ec,
endpoint_iterator);
}));
}

void handle_resolve(string_type /*unused*/const &host,
void handle_resolve(string_type /*unused*/ const &host,
resolve_completion_function once_resolved,
std::error_code const &ec,
resolver_iterator endpoint_iterator) {
typename endpoint_cache::iterator iter;
bool inserted = false;
if (!ec && cache_resolved_) {
std::tie(iter, inserted) =
endpoint_cache_.insert(std::make_pair(
host, std::make_pair(endpoint_iterator, resolver_iterator())));
std::tie(iter, inserted) = endpoint_cache_.insert(std::make_pair(
host, std::make_pair(endpoint_iterator, resolver_iterator())));
once_resolved(ec, iter->second);
} else {
once_resolved(ec, std::make_pair(endpoint_iterator, resolver_iterator()));
Expand Down
44 changes: 28 additions & 16 deletions boost/network/protocol/http/policies/pooled_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
resolver_type&, string_type const&, string_type const&)>
resolver_function_type;
typedef std::function<void(iterator_range<char const*> const&,
std::error_code const&)> body_callback_function_type;
std::error_code const&)>
body_callback_function_type;
typedef std::function<bool(string_type&)> body_generator_function_type;

void cleanup() { host_connection_map().swap(host_connections_); }
Expand Down Expand Up @@ -103,8 +104,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
basic_response<Tag> response_;
// check if the socket is open first
if (!pimpl->is_open()) {
pimpl->init_socket(request_.host(),
std::to_string(request_.port()));
pimpl->init_socket(request_.host(), std::to_string(request_.port()));
}
response_ = basic_response<Tag>();
response_ << ::boost::network::source(request_.host());
Expand Down Expand Up @@ -132,7 +132,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
pimpl->read_body(response_, response_buffer);
}

typename headers_range<basic_response<Tag> >::type connection_range =
typename headers_range<basic_response<Tag>>::type connection_range =
headers(response_)["Connection"];
if (version_major == 1 && version_minor == 1 &&
!boost::empty(connection_range) &&
Expand All @@ -145,10 +145,10 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
if (connection_follow_redirect_) {
std::uint16_t status = response_.status();
if (status >= 300 && status <= 307) {
typename headers_range<basic_response<Tag> >::type location_range =
typename headers_range<basic_response<Tag>>::type location_range =
headers(response_)["Location"];
typename range_iterator<
typename headers_range<basic_request<Tag> >::type>::type
typename headers_range<basic_request<Tag>>::type>::type
location_header = std::begin(location_range);
if (location_header != std::end(location_range)) {
request_.uri(location_header->second);
Expand All @@ -168,7 +168,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
}

std::shared_ptr<http::impl::sync_connection_base<Tag, version_major,
version_minor> > pimpl;
version_minor>> pimpl;
resolver_type& resolver_;
bool connection_follow_redirect_;
get_connection_function get_connection_;
Expand All @@ -182,7 +182,8 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {

typedef std::shared_ptr<connection_impl> connection_ptr;

typedef std::unordered_map<string_type, std::weak_ptr<connection_impl>> host_connection_map;
typedef std::unordered_map<string_type, std::weak_ptr<connection_impl>>
host_connection_map;
std::mutex host_mutex_;
host_connection_map host_connections_;
bool follow_redirect_;
Expand All @@ -198,7 +199,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
optional<string_type> const& private_key_file = optional<string_type>(),
optional<string_type> const& ciphers = optional<string_type>()) {
string_type index =
(request_.host() + ':') + std::to_string(request_.port());
(request_.host() + ':') + std::to_string(request_.port());
std::unique_lock<std::mutex> lock(host_mutex_);
auto it = host_connections_.find(index);
if (it != host_connections_.end()) {
Expand All @@ -209,19 +210,30 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
}

namespace ph = std::placeholders;
connection_ptr connection(new connection_impl(
auto connection = std::make_shared<connection_impl>(
resolver, follow_redirect_, request_.host(),
std::to_string(request_.port()),
// resolver function
std::bind(&pooled_connection_policy<Tag, version_major,
version_minor>::resolve, this, ph::_1, ph::_2, ph::_3),
[this](
resolver_type& resolver, string_type const& host,
std::uint16_t port,
typename resolver_type::resolve_completion_function once_resolved) {
this->resolve(resolver, host, port, once_resolved);
},
// connection factory
std::bind(&pooled_connection_policy<Tag, version_major,
version_minor>::get_connection,
this, ph::_1, ph::_2, always_verify_peer, ph::_3, ph::_4, ph::_5, ph::_6, ph::_7),
[this, always_verify_peer](
resolver_type& resolver, basic_request<Tag> const& request, bool,
optional<string_type> const& certificate_filename,
optional<string_type> const& verify_path,
optional<string_type> const& private_key_file,
optional<string_type> const& ciphers) {
return this->get_connection(resolver, request, always_verify_peer,
certificate_filename, verify_path,
private_key_file, ciphers);
},
boost::iequals(request_.protocol(), string_type("https")),
always_verify_peer, timeout_, certificate_filename, verify_path,
certificate_file, private_key_file, ciphers, 0));
certificate_file, private_key_file, ciphers, 0);
host_connections_.insert(std::make_pair(index, connection));
return connection;
}
Expand Down
40 changes: 21 additions & 19 deletions boost/network/protocol/http/policies/simple_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
typedef std::function<typename resolver_base::resolver_iterator_pair(
resolver_type&, string_type const&, string_type const&)>
resolver_function_type;
typedef typename resolver_base::resolver_completion_function
resolver_completion_function;
typedef std::function<void(iterator_range<char const*> const&,
std::error_code const&)> body_callback_function_type;
std::error_code const&)>
body_callback_function_type;
typedef std::function<bool(string_type&)> body_generator_function_type;

struct connection_impl {
connection_impl(
resolver_type& resolver, bool follow_redirect, bool always_verify_peer,
string_type /*unused*/const& hostname, string_type const& port,
string_type /*unused*/ const& hostname, string_type const& port,
resolver_function_type resolve, bool https, int timeout,
optional<string_type> const& certificate_filename =
optional<string_type>(),
Expand All @@ -54,16 +57,15 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
(void)hostname;
(void)port;

pimpl.reset(impl::sync_connection_base<
Tag, version_major,
version_minor>::new_connection(resolver, resolve, https,
always_verify_peer, timeout,
certificate_filename, verify_path,
certificate_file, private_key_file,
ciphers, ssl_options));
pimpl.reset(
impl::sync_connection_base<Tag, version_major, version_minor>::
new_connection(resolver, resolve, https, always_verify_peer,
timeout, certificate_filename, verify_path,
certificate_file, private_key_file, ciphers,
ssl_options));
}

basic_response<Tag> send_request(string_type /*unused*/const& method,
basic_response<Tag> send_request(string_type /*unused*/ const& method,
basic_request<Tag> request_, bool get_body,
body_callback_function_type callback,
body_generator_function_type generator) {
Expand All @@ -72,8 +74,7 @@ struct simple_connection_policy : resolver_policy<Tag>::type {

basic_response<Tag> response_;
do {
pimpl->init_socket(request_.host(),
std::to_string(request_.port()));
pimpl->init_socket(request_.host(), std::to_string(request_.port()));
pimpl->send_request_impl(method, request_, generator);

response_ = basic_response<Tag>();
Expand All @@ -99,25 +100,25 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
"Location header not defined in redirect response.");
} else {
break;
}
}
} else {
break;
}
}
} while (true);
return response_;
}

private:
std::shared_ptr<http::impl::sync_connection_base<Tag, version_major,
version_minor> > pimpl;
version_minor> > pimpl;
bool follow_redirect_;
};

typedef std::shared_ptr<connection_impl> connection_ptr;
connection_ptr get_connection(
resolver_type& resolver, basic_request<Tag> const& request_,
bool always_verify_peer,
optional<string_type> /*unused*/const& certificate_filename =
optional<string_type> /*unused*/ const& certificate_filename =
optional<string_type>(),
optional<string_type> const& verify_path = optional<string_type>(),
optional<string_type> const& certificate_file = optional<string_type>(),
Expand All @@ -128,9 +129,10 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
connection_ptr connection_(new connection_impl(
resolver, follow_redirect_, always_verify_peer, request_.host(),
std::to_string(request_.port()),
std::bind(&simple_connection_policy<Tag, version_major,
version_minor>::resolve,
this, ph::_1, ph::_2, ph::_3),
[this](resolver_type& resolver, string_type const& host,
std::uint16_t port, resolver_completion_function once_resolved) {
this->resolve(resolver, host, port, once_resolved);
},
boost::iequals(request_.protocol(), string_type("https")), timeout_,
certificate_filename, verify_path, certificate_file, private_key_file,
ciphers, ssl_options));
Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ cd build
cmake -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \
-DBUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \
-DCPP-NETLIB_ENABLE_HTTPS=$ENABLE_HTTPS \
-DBOOST_INCLUDEDIR="${HOME}/${CC}-boost_${BOOST_VERSION}/include" \
-DBOOST_LIBRARYDIR="${HOME}/${CC}-boost_${BOOST_VERSION}/lib" \
-DCMAKE_CXX_FLAGS="-std=c++11 ${CMAKE_CXX_FLAGS}" \
..
make -j2
Expand Down
8 changes: 7 additions & 1 deletion install-boost.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/sh
set -e

if [ ! -d "${BOOST_ROOT}" ]; then
if [ ! -d "${HOME}/${CC}-boost_${BOOST_VERSION}/include" ]; then
wget -O boost_${BOOST_VERSION}.tar.bz2 http://sourceforge.net/projects/boost/files/boost/${BOOST_VER}/boost_${BOOST_VERSION}.tar.bz2/download
tar jxf boost_${BOOST_VERSION}.tar.bz2
cd boost_${BOOST_VERSION}
./bootstrap.sh --with-toolset=$TOOLSET --prefix=${HOME}/${CC}-boost_${BOOST_VERSION}
./b2 --stagedir=. -j4 --layout=tagged variant=debug,release link=shared threading=multi address-model=64 cxxflags='-std=c++11' install >boost-build.log 2>&1
cd ..
rm -rf boost_${BOOST_VERSION}
rm -rf boost_${BOOST_VERSION}.tar.bz2
fi