1 //
2 // ip/resolver_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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 ASIO_IP_RESOLVER_SERVICE_HPP
12 #define ASIO_IP_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 "asio/detail/config.hpp"
19 
20 #if defined(ASIO_ENABLE_OLD_SERVICES)
21 
22 #include "asio/async_result.hpp"
23 #include "asio/error_code.hpp"
24 #include "asio/io_context.hpp"
25 #include "asio/ip/basic_resolver_iterator.hpp"
26 #include "asio/ip/basic_resolver_query.hpp"
27 #include "asio/ip/basic_resolver_results.hpp"
28 
29 #if defined(ASIO_WINDOWS_RUNTIME)
30 # include "asio/detail/winrt_resolver_service.hpp"
31 #else
32 # include "asio/detail/resolver_service.hpp"
33 #endif
34 
35 #include "asio/detail/push_options.hpp"
36 
37 namespace asio {
38 namespace ip {
39 
40 /// Default service implementation for a resolver.
41 template <typename InternetProtocol>
42 class resolver_service
43 #if defined(GENERATING_DOCUMENTATION)
44   : public asio::io_context::service
45 #else
46   : public asio::detail::service_base<
47       resolver_service<InternetProtocol> >
48 #endif
49 {
50 public:
51 #if defined(GENERATING_DOCUMENTATION)
52   /// The unique service identifier.
53   static asio::io_context::id id;
54 #endif
55 
56   /// The protocol type.
57   typedef InternetProtocol protocol_type;
58 
59   /// The endpoint type.
60   typedef typename InternetProtocol::endpoint endpoint_type;
61 
62   /// The query type.
63   typedef basic_resolver_query<InternetProtocol> query_type;
64 
65   /// The iterator type.
66   typedef basic_resolver_iterator<InternetProtocol> iterator_type;
67 
68   /// The results type.
69   typedef basic_resolver_results<InternetProtocol> results_type;
70 
71 private:
72   // The type of the platform-specific implementation.
73 #if defined(ASIO_WINDOWS_RUNTIME)
74   typedef asio::detail::winrt_resolver_service<InternetProtocol>
75     service_impl_type;
76 #else
77   typedef asio::detail::resolver_service<InternetProtocol>
78     service_impl_type;
79 #endif
80 
81 public:
82   /// The type of a resolver implementation.
83 #if defined(GENERATING_DOCUMENTATION)
84   typedef implementation_defined implementation_type;
85 #else
86   typedef typename service_impl_type::implementation_type implementation_type;
87 #endif
88 
89   /// Construct a new resolver service for the specified io_context.
resolver_service(asio::io_context & io_context)90   explicit resolver_service(asio::io_context& io_context)
91     : asio::detail::service_base<
92         resolver_service<InternetProtocol> >(io_context),
93       service_impl_(io_context)
94   {
95   }
96 
97   /// Construct a new resolver implementation.
construct(implementation_type & impl)98   void construct(implementation_type& impl)
99   {
100     service_impl_.construct(impl);
101   }
102 
103 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
104   /// Move-construct a new resolver implementation.
move_construct(implementation_type & impl,implementation_type & other_impl)105   void move_construct(implementation_type& impl,
106       implementation_type& other_impl)
107   {
108     service_impl_.move_construct(impl, other_impl);
109   }
110 
111   /// Move-assign from another resolver implementation.
move_assign(implementation_type & impl,resolver_service & other_service,implementation_type & other_impl)112   void move_assign(implementation_type& impl,
113       resolver_service& other_service,
114       implementation_type& other_impl)
115   {
116     service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
117   }
118 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
119 
120   /// Destroy a resolver implementation.
destroy(implementation_type & impl)121   void destroy(implementation_type& impl)
122   {
123     service_impl_.destroy(impl);
124   }
125 
126   /// Cancel pending asynchronous operations.
cancel(implementation_type & impl)127   void cancel(implementation_type& impl)
128   {
129     service_impl_.cancel(impl);
130   }
131 
132   /// Resolve a query to a list of entries.
resolve(implementation_type & impl,const query_type & query,asio::error_code & ec)133   results_type resolve(implementation_type& impl, const query_type& query,
134       asio::error_code& ec)
135   {
136     return service_impl_.resolve(impl, query, ec);
137   }
138 
139   /// Asynchronously resolve a query to a list of entries.
140   template <typename ResolveHandler>
ASIO_INITFN_RESULT_TYPE(ResolveHandler,void (asio::error_code,results_type))141   ASIO_INITFN_RESULT_TYPE(ResolveHandler,
142       void (asio::error_code, results_type))
143   async_resolve(implementation_type& impl, const query_type& query,
144       ASIO_MOVE_ARG(ResolveHandler) handler)
145   {
146     asio::async_completion<ResolveHandler,
147       void (asio::error_code, results_type)> init(handler);
148 
149     service_impl_.async_resolve(impl, query, init.completion_handler);
150 
151     return init.result.get();
152   }
153 
154   /// Resolve an endpoint to a list of entries.
resolve(implementation_type & impl,const endpoint_type & endpoint,asio::error_code & ec)155   results_type resolve(implementation_type& impl,
156       const endpoint_type& endpoint, asio::error_code& ec)
157   {
158     return service_impl_.resolve(impl, endpoint, ec);
159   }
160 
161   /// Asynchronously resolve an endpoint to a list of entries.
162   template <typename ResolveHandler>
ASIO_INITFN_RESULT_TYPE(ResolveHandler,void (asio::error_code,results_type))163   ASIO_INITFN_RESULT_TYPE(ResolveHandler,
164       void (asio::error_code, results_type))
165   async_resolve(implementation_type& impl, const endpoint_type& endpoint,
166       ASIO_MOVE_ARG(ResolveHandler) handler)
167   {
168     asio::async_completion<ResolveHandler,
169       void (asio::error_code, results_type)> init(handler);
170 
171     service_impl_.async_resolve(impl, endpoint, init.completion_handler);
172 
173     return init.result.get();
174   }
175 
176 private:
177   // Destroy all user-defined handler objects owned by the service.
shutdown()178   void shutdown()
179   {
180     service_impl_.shutdown();
181   }
182 
183   // Perform any fork-related housekeeping.
notify_fork(asio::io_context::fork_event event)184   void notify_fork(asio::io_context::fork_event event)
185   {
186     service_impl_.notify_fork(event);
187   }
188 
189   // The platform-specific implementation.
190   service_impl_type service_impl_;
191 };
192 
193 } // namespace ip
194 } // namespace asio
195 
196 #include "asio/detail/pop_options.hpp"
197 
198 #endif // defined(ASIO_ENABLE_OLD_SERVICES)
199 
200 #endif // ASIO_IP_RESOLVER_SERVICE_HPP
201