]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/resolver_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP | |
12 | #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if !defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
21 | ||
7c673cae | 22 | #include <boost/asio/ip/basic_resolver_query.hpp> |
b32b8144 FG |
23 | #include <boost/asio/ip/basic_resolver_results.hpp> |
24 | #include <boost/asio/detail/concurrency_hint.hpp> | |
25 | #include <boost/asio/detail/memory.hpp> | |
7c673cae | 26 | #include <boost/asio/detail/resolve_endpoint_op.hpp> |
b32b8144 | 27 | #include <boost/asio/detail/resolve_query_op.hpp> |
7c673cae FG |
28 | #include <boost/asio/detail/resolver_service_base.hpp> |
29 | ||
30 | #include <boost/asio/detail/push_options.hpp> | |
31 | ||
32 | namespace boost { | |
33 | namespace asio { | |
34 | namespace detail { | |
35 | ||
36 | template <typename Protocol> | |
b32b8144 FG |
37 | class resolver_service : |
38 | public service_base<resolver_service<Protocol> >, | |
39 | public resolver_service_base | |
7c673cae FG |
40 | { |
41 | public: | |
42 | // The implementation type of the resolver. A cancellation token is used to | |
43 | // indicate to the background thread that the operation has been cancelled. | |
44 | typedef socket_ops::shared_cancel_token_type implementation_type; | |
45 | ||
46 | // The endpoint type. | |
47 | typedef typename Protocol::endpoint endpoint_type; | |
48 | ||
49 | // The query type. | |
50 | typedef boost::asio::ip::basic_resolver_query<Protocol> query_type; | |
51 | ||
b32b8144 FG |
52 | // The results type. |
53 | typedef boost::asio::ip::basic_resolver_results<Protocol> results_type; | |
7c673cae FG |
54 | |
55 | // Constructor. | |
b32b8144 FG |
56 | resolver_service(boost::asio::io_context& io_context) |
57 | : service_base<resolver_service<Protocol> >(io_context), | |
58 | resolver_service_base(io_context) | |
7c673cae FG |
59 | { |
60 | } | |
61 | ||
b32b8144 FG |
62 | // Destroy all user-defined handler objects owned by the service. |
63 | void shutdown() | |
64 | { | |
65 | this->base_shutdown(); | |
66 | } | |
67 | ||
68 | // Perform any fork-related housekeeping. | |
69 | void notify_fork(boost::asio::io_context::fork_event fork_ev) | |
70 | { | |
71 | this->base_notify_fork(fork_ev); | |
72 | } | |
73 | ||
7c673cae | 74 | // Resolve a query to a list of entries. |
b32b8144 | 75 | results_type resolve(implementation_type&, const query_type& query, |
7c673cae FG |
76 | boost::system::error_code& ec) |
77 | { | |
78 | boost::asio::detail::addrinfo_type* address_info = 0; | |
79 | ||
80 | socket_ops::getaddrinfo(query.host_name().c_str(), | |
81 | query.service_name().c_str(), query.hints(), &address_info, ec); | |
82 | auto_addrinfo auto_address_info(address_info); | |
83 | ||
b32b8144 | 84 | return ec ? results_type() : results_type::create( |
7c673cae FG |
85 | address_info, query.host_name(), query.service_name()); |
86 | } | |
87 | ||
88 | // Asynchronously resolve a query to a list of entries. | |
89 | template <typename Handler> | |
90 | void async_resolve(implementation_type& impl, | |
91 | const query_type& query, Handler& handler) | |
92 | { | |
93 | // Allocate and construct an operation to wrap the handler. | |
b32b8144 | 94 | typedef resolve_query_op<Protocol, Handler> op; |
7c673cae | 95 | typename op::ptr p = { boost::asio::detail::addressof(handler), |
b32b8144 FG |
96 | op::ptr::allocate(handler), 0 }; |
97 | p.p = new (p.v) op(impl, query, io_context_impl_, handler); | |
7c673cae | 98 | |
b32b8144 FG |
99 | BOOST_ASIO_HANDLER_CREATION((io_context_impl_.context(), |
100 | *p.p, "resolver", &impl, 0, "async_resolve")); | |
7c673cae FG |
101 | |
102 | start_resolve_op(p.p); | |
103 | p.v = p.p = 0; | |
104 | } | |
105 | ||
106 | // Resolve an endpoint to a list of entries. | |
b32b8144 | 107 | results_type resolve(implementation_type&, |
7c673cae FG |
108 | const endpoint_type& endpoint, boost::system::error_code& ec) |
109 | { | |
110 | char host_name[NI_MAXHOST]; | |
111 | char service_name[NI_MAXSERV]; | |
112 | socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(), | |
113 | host_name, NI_MAXHOST, service_name, NI_MAXSERV, | |
114 | endpoint.protocol().type(), ec); | |
115 | ||
b32b8144 | 116 | return ec ? results_type() : results_type::create( |
7c673cae FG |
117 | endpoint, host_name, service_name); |
118 | } | |
119 | ||
120 | // Asynchronously resolve an endpoint to a list of entries. | |
121 | template <typename Handler> | |
122 | void async_resolve(implementation_type& impl, | |
123 | const endpoint_type& endpoint, Handler& handler) | |
124 | { | |
125 | // Allocate and construct an operation to wrap the handler. | |
126 | typedef resolve_endpoint_op<Protocol, Handler> op; | |
127 | typename op::ptr p = { boost::asio::detail::addressof(handler), | |
b32b8144 FG |
128 | op::ptr::allocate(handler), 0 }; |
129 | p.p = new (p.v) op(impl, endpoint, io_context_impl_, handler); | |
7c673cae | 130 | |
b32b8144 FG |
131 | BOOST_ASIO_HANDLER_CREATION((io_context_impl_.context(), |
132 | *p.p, "resolver", &impl, 0, "async_resolve")); | |
7c673cae FG |
133 | |
134 | start_resolve_op(p.p); | |
135 | p.v = p.p = 0; | |
136 | } | |
137 | }; | |
138 | ||
139 | } // namespace detail | |
140 | } // namespace asio | |
141 | } // namespace boost | |
142 | ||
143 | #include <boost/asio/detail/pop_options.hpp> | |
144 | ||
145 | #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
146 | ||
147 | #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP |