|
9 | 9 |
|
10 | 10 | #include <string>
|
11 | 11 | #include <utility>
|
12 |
| -#include <asio/io_service.hpp> |
13 |
| -#include <asio/ip/udp.hpp> |
14 |
| -#include <asio/placeholders.hpp> |
15 |
| -#include <asio/strand.hpp> |
| 12 | +#include <memory> |
| 13 | +#include <unordered_map> |
| 14 | +#include <boost/asio/io_service.hpp> |
| 15 | +#include <boost/asio/ip/udp.hpp> |
| 16 | +#include <boost/asio/placeholders.hpp> |
| 17 | +#include <boost/asio/strand.hpp> |
16 | 18 | #include <functional>
|
17 |
| -#include <boost/enable_shared_from_this.hpp> |
18 | 19 | #include <boost/lexical_cast.hpp>
|
19 | 20 | #include <boost/unordered_map.hpp>
|
20 | 21 | #include <boost/algorithm/string/case_conv.hpp>
|
|
24 | 25 | #include <network/protocol/http/client/connection/async_resolver.hpp>
|
25 | 26 |
|
26 | 27 | namespace network { namespace http {
|
27 |
| -struct async_resolver_pimpl : boost::enable_shared_from_this<async_resolver_pimpl> { |
28 |
| - typedef resolver_delegate::resolve_completion_function resolve_completion_function; |
29 |
| - async_resolver_pimpl(asio::io_service & service, bool cache_resolved); |
30 |
| - void resolve(std::string const & host, |
31 |
| - uint16_t port, |
32 |
| - resolve_completion_function once_resolved); |
33 |
| - void clear_resolved_cache(); |
34 |
| - private: |
35 |
| - asio::ip::udp::resolver resolver_; |
36 |
| - bool cache_resolved_; |
37 |
| - typedef asio::ip::udp::resolver::iterator |
38 |
| - resolver_iterator; |
39 |
| - typedef boost::unordered_map<std::string, std::pair<resolver_iterator,resolver_iterator> > |
40 |
| - endpoint_cache; |
41 |
| - endpoint_cache endpoint_cache_; |
42 |
| - boost::scoped_ptr<asio::io_service::strand> resolver_strand_; |
| 28 | + struct async_resolver_pimpl : std::enable_shared_from_this<async_resolver_pimpl> { |
| 29 | + typedef resolver_delegate::resolve_completion_function resolve_completion_function; |
| 30 | + async_resolver_pimpl(boost::asio::io_service & service, bool cache_resolved); |
| 31 | + void resolve(std::string const & host, |
| 32 | + uint16_t port, |
| 33 | + resolve_completion_function once_resolved); |
| 34 | + void clear_resolved_cache(); |
| 35 | + private: |
| 36 | + boost::asio::ip::udp::resolver resolver_; |
| 37 | + bool cache_resolved_; |
| 38 | + typedef boost::asio::ip::udp::resolver::iterator |
| 39 | + resolver_iterator; |
| 40 | + typedef std::unordered_map<std::string, std::pair<resolver_iterator,resolver_iterator> > |
| 41 | + endpoint_cache; |
| 42 | + endpoint_cache endpoint_cache_; |
| 43 | + std::unique_ptr<boost::asio::io_service::strand> resolver_strand_; |
43 | 44 |
|
44 |
| - void handle_resolve(std::string const & host, |
45 |
| - resolve_completion_function once_resolved, |
46 |
| - asio::error_code const & ec, |
47 |
| - resolver_iterator endpoint_iterator); |
48 |
| -}; |
49 |
| - |
50 |
| -async_resolver_pimpl::async_resolver_pimpl(asio::io_service & service, bool cache_resolved) |
| 45 | + void handle_resolve(std::string const & host, |
| 46 | + resolve_completion_function once_resolved, |
| 47 | + boost::system::error_code const & ec, |
| 48 | + resolver_iterator endpoint_iterator); |
| 49 | + }; |
| 50 | + |
| 51 | + async_resolver_pimpl::async_resolver_pimpl(boost::asio::io_service & service, bool cache_resolved) |
51 | 52 | : resolver_(service),
|
52 |
| - cache_resolved_(cache_resolved), |
53 |
| - endpoint_cache_(), |
54 |
| - resolver_strand_(new(std::nothrow) asio::io_service::strand(service)) |
55 |
| -{ |
56 |
| - // Do nothing |
57 |
| -} |
| 53 | + cache_resolved_(cache_resolved), |
| 54 | + endpoint_cache_(), |
| 55 | + resolver_strand_(new(std::nothrow) boost::asio::io_service::strand(service)) |
| 56 | + { |
| 57 | + // Do nothing |
| 58 | + } |
58 | 59 |
|
59 |
| -void async_resolver_pimpl::clear_resolved_cache() { |
60 |
| - if (cache_resolved_) |
61 |
| - endpoint_cache().swap(endpoint_cache_); |
62 |
| -} |
| 60 | + void async_resolver_pimpl::clear_resolved_cache() { |
| 61 | + if (cache_resolved_) |
| 62 | + endpoint_cache().swap(endpoint_cache_); |
| 63 | + } |
63 | 64 |
|
64 |
| -void async_resolver_pimpl::resolve(std::string const & host, |
65 |
| - boost::uint16_t port, |
66 |
| - resolve_completion_function once_resolved) { |
67 |
| - if (!resolver_strand_.get()) |
68 |
| - BOOST_THROW_EXCEPTION(std::runtime_error( |
69 |
| - "Uninitialized resolver strand, ran out of memory.")); |
| 65 | + void async_resolver_pimpl::resolve(std::string const & host, |
| 66 | + boost::uint16_t port, |
| 67 | + resolve_completion_function once_resolved) { |
| 68 | + if (!resolver_strand_.get()) |
| 69 | + BOOST_THROW_EXCEPTION(std::runtime_error( |
| 70 | + "Uninitialized resolver strand, ran out of memory.")); |
70 | 71 |
|
71 |
| - if (cache_resolved_) { |
72 |
| - endpoint_cache::iterator iter = |
73 |
| - endpoint_cache_.find(boost::to_lower_copy(host)); |
74 |
| - if (iter != endpoint_cache_.end()) { |
75 |
| - asio::error_code ignored; |
76 |
| - once_resolved(ignored, iter->second); |
77 |
| - return; |
| 72 | + if (cache_resolved_) { |
| 73 | + endpoint_cache::iterator iter = |
| 74 | + endpoint_cache_.find(boost::to_lower_copy(host)); |
| 75 | + if (iter != endpoint_cache_.end()) { |
| 76 | + boost::system::error_code ignored; |
| 77 | + once_resolved(ignored, iter->second); |
| 78 | + return; |
| 79 | + } |
78 | 80 | }
|
79 |
| - } |
80 | 81 |
|
81 |
| - std::string port_str = boost::lexical_cast<std::string>(port); |
82 |
| - asio::ip::udp::resolver::query query(host, port_str); |
83 |
| - using namespace std::placeholders; |
84 |
| - resolver_.async_resolve( |
85 |
| - query, |
86 |
| - resolver_strand_->wrap( |
87 |
| - std::bind( |
88 |
| - &async_resolver_pimpl::handle_resolve, |
89 |
| - async_resolver_pimpl::shared_from_this(), |
90 |
| - boost::to_lower_copy(host), |
91 |
| - once_resolved, |
92 |
| - _1, |
93 |
| - _2))); |
94 |
| -} |
| 82 | + std::string port_str = boost::lexical_cast<std::string>(port); |
| 83 | + boost::asio::ip::udp::resolver::query query(host, port_str); |
| 84 | + using namespace std::placeholders; |
| 85 | + resolver_.async_resolve(query, |
| 86 | + resolver_strand_->wrap(std::bind(&async_resolver_pimpl::handle_resolve, |
| 87 | + async_resolver_pimpl::shared_from_this(), |
| 88 | + boost::to_lower_copy(host), |
| 89 | + once_resolved, |
| 90 | + _1, |
| 91 | + _2))); |
| 92 | + } |
95 | 93 |
|
96 |
| -void async_resolver_pimpl::handle_resolve(std::string const & host, |
97 |
| - resolve_completion_function once_resolved, |
98 |
| - asio::error_code const & ec, |
99 |
| - resolver_iterator endpoint_iterator) { |
100 |
| - endpoint_cache::iterator iter; |
101 |
| - bool inserted = false; |
102 |
| - if (!ec && cache_resolved_) { |
103 |
| - boost::fusion::tie(iter, inserted) = |
| 94 | + void async_resolver_pimpl::handle_resolve(std::string const & host, |
| 95 | + resolve_completion_function once_resolved, |
| 96 | + boost::system::error_code const & ec, |
| 97 | + resolver_iterator endpoint_iterator) { |
| 98 | + endpoint_cache::iterator iter; |
| 99 | + bool inserted = false; |
| 100 | + if (!ec && cache_resolved_) { |
| 101 | + boost::fusion::tie(iter, inserted) = |
104 | 102 | endpoint_cache_.insert(
|
105 |
| - std::make_pair(host, |
106 |
| - std::make_pair(endpoint_iterator, |
107 |
| - resolver_iterator()))); |
108 |
| - once_resolved(ec, iter->second); |
109 |
| - } else { |
110 |
| - once_resolved(ec, std::make_pair(endpoint_iterator,resolver_iterator())); |
| 103 | + std::make_pair(host, |
| 104 | + std::make_pair(endpoint_iterator, |
| 105 | + resolver_iterator()))); |
| 106 | + once_resolved(ec, iter->second); |
| 107 | + } else { |
| 108 | + once_resolved(ec, std::make_pair(endpoint_iterator,resolver_iterator())); |
| 109 | + } |
111 | 110 | }
|
112 |
| -} |
113 |
| - |
114 |
| -async_resolver::async_resolver(asio::io_service & service, bool cache_resolved) |
115 |
| -: pimpl(new (std::nothrow) async_resolver_pimpl(service, cache_resolved)) |
116 |
| -{} |
117 |
| - |
118 |
| -void async_resolver::resolve(std::string const & host, |
119 |
| - uint16_t port, |
120 |
| - resolve_completion_function once_resolved) { |
121 |
| - BOOST_ASSERT(pimpl.get() && "Uninitialized pimpl, probably ran out of memory."); |
122 |
| - pimpl->resolve(host, port, once_resolved); |
123 |
| -} |
124 | 111 |
|
125 |
| -void async_resolver::clear_resolved_cache() { |
126 |
| - BOOST_ASSERT(pimpl.get() && "Uninitialized pimpl, probably ran out of memory."); |
127 |
| - pimpl->clear_resolved_cache(); |
128 |
| -} |
129 |
| - |
130 |
| -async_resolver::~async_resolver() { |
131 |
| - // Do nothing |
132 |
| -} |
| 112 | + async_resolver::async_resolver(boost::asio::io_service & service, bool cache_resolved) |
| 113 | + : pimpl(new (std::nothrow) async_resolver_pimpl(service, cache_resolved)) |
| 114 | + {} |
133 | 115 |
|
| 116 | + void async_resolver::resolve(std::string const & host, |
| 117 | + uint16_t port, |
| 118 | + resolve_completion_function once_resolved) { |
| 119 | + BOOST_ASSERT(pimpl.get() && "Uninitialized pimpl, probably ran out of memory."); |
| 120 | + pimpl->resolve(host, port, once_resolved); |
| 121 | + } |
| 122 | + |
| 123 | + void async_resolver::clear_resolved_cache() { |
| 124 | + BOOST_ASSERT(pimpl.get() && "Uninitialized pimpl, probably ran out of memory."); |
| 125 | + pimpl->clear_resolved_cache(); |
| 126 | + } |
| 127 | + |
| 128 | + async_resolver::~async_resolver() { |
| 129 | + // Do nothing |
| 130 | + } |
| 131 | + |
134 | 132 | } // namespace http
|
135 | 133 | } // namespace network
|
136 |
| - |
| 134 | + |
137 | 135 | #endif /* NETWORK_PROTOCOL_HTTP_CLIENT_CONNECTION_ASYNC_RESOLVER_IPP_20111126 */
|
0 commit comments