Chris@16
|
1 //
|
Chris@16
|
2 // detail/resolve_op.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_RESOLVE_OP_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_RESOLVE_OP_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/error.hpp>
|
Chris@16
|
20 #include <boost/asio/io_service.hpp>
|
Chris@16
|
21 #include <boost/asio/ip/basic_resolver_iterator.hpp>
|
Chris@16
|
22 #include <boost/asio/ip/basic_resolver_query.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/fenced_block.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
28 #include <boost/asio/detail/operation.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace asio {
|
Chris@16
|
35 namespace detail {
|
Chris@16
|
36
|
Chris@16
|
37 template <typename Protocol, typename Handler>
|
Chris@16
|
38 class resolve_op : public operation
|
Chris@16
|
39 {
|
Chris@16
|
40 public:
|
Chris@16
|
41 BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_op);
|
Chris@16
|
42
|
Chris@16
|
43 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
|
Chris@16
|
44 typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
|
Chris@16
|
45
|
Chris@16
|
46 resolve_op(socket_ops::weak_cancel_token_type cancel_token,
|
Chris@16
|
47 const query_type& query, io_service_impl& ios, Handler& handler)
|
Chris@16
|
48 : operation(&resolve_op::do_complete),
|
Chris@16
|
49 cancel_token_(cancel_token),
|
Chris@16
|
50 query_(query),
|
Chris@16
|
51 io_service_impl_(ios),
|
Chris@16
|
52 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
|
Chris@16
|
53 addrinfo_(0)
|
Chris@16
|
54 {
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 ~resolve_op()
|
Chris@16
|
58 {
|
Chris@16
|
59 if (addrinfo_)
|
Chris@16
|
60 socket_ops::freeaddrinfo(addrinfo_);
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 static void do_complete(io_service_impl* owner, operation* base,
|
Chris@16
|
64 const boost::system::error_code& /*ec*/,
|
Chris@16
|
65 std::size_t /*bytes_transferred*/)
|
Chris@16
|
66 {
|
Chris@16
|
67 // Take ownership of the operation object.
|
Chris@16
|
68 resolve_op* o(static_cast<resolve_op*>(base));
|
Chris@16
|
69 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
|
Chris@16
|
70
|
Chris@16
|
71 if (owner && owner != &o->io_service_impl_)
|
Chris@16
|
72 {
|
Chris@16
|
73 // The operation is being run on the worker io_service. Time to perform
|
Chris@16
|
74 // the resolver operation.
|
Chris@16
|
75
|
Chris@16
|
76 // Perform the blocking host resolution operation.
|
Chris@16
|
77 socket_ops::background_getaddrinfo(o->cancel_token_,
|
Chris@16
|
78 o->query_.host_name().c_str(), o->query_.service_name().c_str(),
|
Chris@16
|
79 o->query_.hints(), &o->addrinfo_, o->ec_);
|
Chris@16
|
80
|
Chris@16
|
81 // Pass operation back to main io_service for completion.
|
Chris@16
|
82 o->io_service_impl_.post_deferred_completion(o);
|
Chris@16
|
83 p.v = p.p = 0;
|
Chris@16
|
84 }
|
Chris@16
|
85 else
|
Chris@16
|
86 {
|
Chris@16
|
87 // The operation has been returned to the main io_service. The completion
|
Chris@16
|
88 // handler is ready to be delivered.
|
Chris@16
|
89
|
Chris@16
|
90 BOOST_ASIO_HANDLER_COMPLETION((o));
|
Chris@16
|
91
|
Chris@16
|
92 // Make a copy of the handler so that the memory can be deallocated
|
Chris@16
|
93 // before the upcall is made. Even if we're not about to make an upcall,
|
Chris@16
|
94 // a sub-object of the handler may be the true owner of the memory
|
Chris@16
|
95 // associated with the handler. Consequently, a local copy of the handler
|
Chris@16
|
96 // is required to ensure that any owning sub-object remains valid until
|
Chris@16
|
97 // after we have deallocated the memory here.
|
Chris@16
|
98 detail::binder2<Handler, boost::system::error_code, iterator_type>
|
Chris@16
|
99 handler(o->handler_, o->ec_, iterator_type());
|
Chris@16
|
100 p.h = boost::asio::detail::addressof(handler.handler_);
|
Chris@16
|
101 if (o->addrinfo_)
|
Chris@16
|
102 {
|
Chris@16
|
103 handler.arg2_ = iterator_type::create(o->addrinfo_,
|
Chris@16
|
104 o->query_.host_name(), o->query_.service_name());
|
Chris@16
|
105 }
|
Chris@16
|
106 p.reset();
|
Chris@16
|
107
|
Chris@16
|
108 if (owner)
|
Chris@16
|
109 {
|
Chris@16
|
110 fenced_block b(fenced_block::half);
|
Chris@16
|
111 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
|
Chris@16
|
112 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
|
Chris@16
|
113 BOOST_ASIO_HANDLER_INVOCATION_END;
|
Chris@16
|
114 }
|
Chris@16
|
115 }
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 private:
|
Chris@16
|
119 socket_ops::weak_cancel_token_type cancel_token_;
|
Chris@16
|
120 query_type query_;
|
Chris@16
|
121 io_service_impl& io_service_impl_;
|
Chris@16
|
122 Handler handler_;
|
Chris@16
|
123 boost::system::error_code ec_;
|
Chris@16
|
124 boost::asio::detail::addrinfo_type* addrinfo_;
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 } // namespace detail
|
Chris@16
|
128 } // namespace asio
|
Chris@16
|
129 } // namespace boost
|
Chris@16
|
130
|
Chris@16
|
131 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
132
|
Chris@16
|
133 #endif // BOOST_ASIO_DETAIL_RESOLVE_OP_HPP
|