Clementine
basic_resolver.hpp
1 //
2 // ip/basic_resolver.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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_BASIC_RESOLVER_HPP
12 #define ASIO_IP_BASIC_RESOLVER_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 #include <string>
20 #include "asio/any_io_executor.hpp"
21 #include "asio/async_result.hpp"
22 #include "asio/detail/handler_type_requirements.hpp"
23 #include "asio/detail/io_object_impl.hpp"
24 #include "asio/detail/non_const_lvalue.hpp"
25 #include "asio/detail/string_view.hpp"
26 #include "asio/detail/throw_error.hpp"
27 #include "asio/error.hpp"
28 #include "asio/execution_context.hpp"
29 #include "asio/ip/basic_resolver_iterator.hpp"
30 #include "asio/ip/basic_resolver_query.hpp"
31 #include "asio/ip/basic_resolver_results.hpp"
32 #include "asio/ip/resolver_base.hpp"
33 #if defined(ASIO_WINDOWS_RUNTIME)
34 # include "asio/detail/winrt_resolver_service.hpp"
35 #else
36 # include "asio/detail/resolver_service.hpp"
37 #endif
38 
39 #if defined(ASIO_HAS_MOVE)
40 # include <utility>
41 #endif // defined(ASIO_HAS_MOVE)
42 
43 #include "asio/detail/push_options.hpp"
44 
45 namespace asio {
46 namespace ip {
47 
48 #if !defined(ASIO_IP_BASIC_RESOLVER_FWD_DECL)
49 #define ASIO_IP_BASIC_RESOLVER_FWD_DECL
50 
51 // Forward declaration with defaulted arguments.
52 template <typename InternetProtocol, typename Executor = any_io_executor>
54 
55 #endif // !defined(ASIO_IP_BASIC_RESOLVER_FWD_DECL)
56 
58 
66 template <typename InternetProtocol, typename Executor>
67 class basic_resolver
68  : public resolver_base
69 {
70 public:
72  typedef Executor executor_type;
73 
75  template <typename Executor1>
77  {
80  };
81 
83  typedef InternetProtocol protocol_type;
84 
86  typedef typename InternetProtocol::endpoint endpoint_type;
87 
88 #if !defined(ASIO_NO_DEPRECATED)
91 
94 #endif // !defined(ASIO_NO_DEPRECATED)
95 
98 
100 
107  explicit basic_resolver(const executor_type& ex)
108  : impl_(ex)
109  {
110  }
111 
113 
120  template <typename ExecutionContext>
121  explicit basic_resolver(ExecutionContext& context,
122  typename enable_if<
123  is_convertible<ExecutionContext&, execution_context&>::value
124  >::type* = 0)
125  : impl_(context)
126  {
127  }
128 
129 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
130 
141  : impl_(std::move(other.impl_))
142  {
143  }
144 
145  // All resolvers have access to each other's implementations.
146  template <typename InternetProtocol1, typename Executor1>
147  friend class basic_resolver;
148 
150 
159  template <typename Executor1>
161  typename enable_if<
162  is_convertible<Executor1, Executor>::value
163  >::type* = 0)
164  : impl_(std::move(other.impl_))
165  {
166  }
167 
169 
180  basic_resolver& operator=(basic_resolver&& other)
181  {
182  impl_ = std::move(other.impl_);
183  return *this;
184  }
185 
187 
198  template <typename Executor1>
199  typename enable_if<
200  is_convertible<Executor1, Executor>::value,
203  {
204  basic_resolver tmp(std::move(other));
205  impl_ = std::move(tmp.impl_);
206  return *this;
207  }
208 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
209 
211 
217  {
218  }
219 
221  executor_type get_executor() ASIO_NOEXCEPT
222  {
223  return impl_.get_executor();
224  }
225 
227 
232  void cancel()
233  {
234  return impl_.get_service().cancel(impl_.get_implementation());
235  }
236 
237 #if !defined(ASIO_NO_DEPRECATED)
238 
251  results_type resolve(const query& q)
252  {
253  asio::error_code ec;
254  results_type r = impl_.get_service().resolve(
255  impl_.get_implementation(), q, ec);
256  asio::detail::throw_error(ec, "resolve");
257  return r;
258  }
259 
262 
273  results_type resolve(const query& q, asio::error_code& ec)
274  {
275  return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
276  }
277 #endif // !defined(ASIO_NO_DEPRECATED)
278 
280 
312  results_type resolve(ASIO_STRING_VIEW_PARAM host,
313  ASIO_STRING_VIEW_PARAM service)
314  {
315  return resolve(host, service, resolver_base::flags());
316  }
317 
319 
351  results_type resolve(ASIO_STRING_VIEW_PARAM host,
352  ASIO_STRING_VIEW_PARAM service, asio::error_code& ec)
353  {
354  return resolve(host, service, resolver_base::flags(), ec);
355  }
356 
358 
395  results_type resolve(ASIO_STRING_VIEW_PARAM host,
396  ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
397  {
398  asio::error_code ec;
399  basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
400  static_cast<std::string>(service), resolve_flags);
401  results_type r = impl_.get_service().resolve(
402  impl_.get_implementation(), q, ec);
403  asio::detail::throw_error(ec, "resolve");
404  return r;
405  }
406 
408 
445  results_type resolve(ASIO_STRING_VIEW_PARAM host,
446  ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags,
447  asio::error_code& ec)
448  {
449  basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
450  static_cast<std::string>(service), resolve_flags);
451  return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
452  }
453 
455 
490  results_type resolve(const protocol_type& protocol,
491  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service)
492  {
493  return resolve(protocol, host, service, resolver_base::flags());
494  }
495 
497 
532  results_type resolve(const protocol_type& protocol,
533  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service,
534  asio::error_code& ec)
535  {
536  return resolve(protocol, host, service, resolver_base::flags(), ec);
537  }
538 
540 
580  results_type resolve(const protocol_type& protocol,
581  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service,
582  resolver_base::flags resolve_flags)
583  {
584  asio::error_code ec;
586  protocol, static_cast<std::string>(host),
587  static_cast<std::string>(service), resolve_flags);
588  results_type r = impl_.get_service().resolve(
589  impl_.get_implementation(), q, ec);
590  asio::detail::throw_error(ec, "resolve");
591  return r;
592  }
593 
595 
635  results_type resolve(const protocol_type& protocol,
636  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service,
637  resolver_base::flags resolve_flags, asio::error_code& ec)
638  {
640  protocol, static_cast<std::string>(host),
641  static_cast<std::string>(service), resolve_flags);
642  return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
643  }
644 
645 #if !defined(ASIO_NO_DEPRECATED)
646 
669  template <
670  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
671  results_type)) ResolveHandler
672  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
673  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
674  void (asio::error_code, results_type))
675  async_resolve(const query& q,
676  ASIO_MOVE_ARG(ResolveHandler) handler
677  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
678  {
679  return asio::async_initiate<ResolveHandler,
680  void (asio::error_code, results_type)>(
681  initiate_async_resolve(this), handler, q);
682  }
683 #endif // !defined(ASIO_NO_DEPRECATED)
684 
686 
727  template <
728  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
729  results_type)) ResolveHandler
730  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
731  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
732  void (asio::error_code, results_type))
733  async_resolve(ASIO_STRING_VIEW_PARAM host,
734  ASIO_STRING_VIEW_PARAM service,
735  ASIO_MOVE_ARG(ResolveHandler) handler
736  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
737  {
738  return async_resolve(host, service, resolver_base::flags(),
739  ASIO_MOVE_CAST(ResolveHandler)(handler));
740  }
741 
743 
789  template <
790  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
791  results_type)) ResolveHandler
792  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
793  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
794  void (asio::error_code, results_type))
795  async_resolve(ASIO_STRING_VIEW_PARAM host,
796  ASIO_STRING_VIEW_PARAM service,
797  resolver_base::flags resolve_flags,
798  ASIO_MOVE_ARG(ResolveHandler) handler
799  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
800  {
801  basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
802  static_cast<std::string>(service), resolve_flags);
803 
804  return asio::async_initiate<ResolveHandler,
805  void (asio::error_code, results_type)>(
806  initiate_async_resolve(this), handler, q);
807  }
808 
810 
854  template <
855  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
856  results_type)) ResolveHandler
857  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
858  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
859  void (asio::error_code, results_type))
860  async_resolve(const protocol_type& protocol,
861  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service,
862  ASIO_MOVE_ARG(ResolveHandler) handler
863  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
864  {
865  return async_resolve(protocol, host, service, resolver_base::flags(),
866  ASIO_MOVE_CAST(ResolveHandler)(handler));
867  }
868 
870 
919  template <
920  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
921  results_type)) ResolveHandler
922  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
923  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
924  void (asio::error_code, results_type))
925  async_resolve(const protocol_type& protocol,
926  ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service,
927  resolver_base::flags resolve_flags,
928  ASIO_MOVE_ARG(ResolveHandler) handler
929  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
930  {
932  protocol, static_cast<std::string>(host),
933  static_cast<std::string>(service), resolve_flags);
934 
935  return asio::async_initiate<ResolveHandler,
936  void (asio::error_code, results_type)>(
937  initiate_async_resolve(this), handler, q);
938  }
939 
941 
954  results_type resolve(const endpoint_type& e)
955  {
956  asio::error_code ec;
957  results_type i = impl_.get_service().resolve(
958  impl_.get_implementation(), e, ec);
959  asio::detail::throw_error(ec, "resolve");
960  return i;
961  }
962 
964 
977  results_type resolve(const endpoint_type& e, asio::error_code& ec)
978  {
979  return impl_.get_service().resolve(impl_.get_implementation(), e, ec);
980  }
981 
984 
1006  template <
1007  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
1008  results_type)) ResolveHandler
1009  ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1010  ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler,
1011  void (asio::error_code, results_type))
1012  async_resolve(const endpoint_type& e,
1013  ASIO_MOVE_ARG(ResolveHandler) handler
1014  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1015  {
1016  return asio::async_initiate<ResolveHandler,
1017  void (asio::error_code, results_type)>(
1018  initiate_async_resolve(this), handler, e);
1019  }
1020 
1021 private:
1022  // Disallow copying and assignment.
1023  basic_resolver(const basic_resolver&) ASIO_DELETED;
1024  basic_resolver& operator=(const basic_resolver&) ASIO_DELETED;
1025 
1026  class initiate_async_resolve
1027  {
1028  public:
1029  typedef Executor executor_type;
1030 
1031  explicit initiate_async_resolve(basic_resolver* self)
1032  : self_(self)
1033  {
1034  }
1035 
1036  executor_type get_executor() const ASIO_NOEXCEPT
1037  {
1038  return self_->get_executor();
1039  }
1040 
1041  template <typename ResolveHandler, typename Query>
1042  void operator()(ASIO_MOVE_ARG(ResolveHandler) handler,
1043  const Query& q) const
1044  {
1045  // If you get an error on the following line it means that your handler
1046  // does not meet the documented type requirements for a ResolveHandler.
1047  ASIO_RESOLVE_HANDLER_CHECK(
1048  ResolveHandler, handler, results_type) type_check;
1049 
1051  self_->impl_.get_service().async_resolve(
1052  self_->impl_.get_implementation(), q,
1053  handler2.value, self_->impl_.get_executor());
1054  }
1055 
1056  private:
1057  basic_resolver* self_;
1058  };
1059 
1060 # if defined(ASIO_WINDOWS_RUNTIME)
1062  asio::detail::winrt_resolver_service<InternetProtocol>,
1063  Executor> impl_;
1064 # else
1067  Executor> impl_;
1068 # endif
1069 };
1070 
1071 } // namespace ip
1072 } // namespace asio
1073 
1074 #include "asio/detail/pop_options.hpp"
1075 
1076 #endif // ASIO_IP_BASIC_RESOLVER_HPP
basic_resolver< InternetProtocol, Executor1 > other
The resolver type when rebound to the specified executor.
Definition: basic_resolver.hpp:79
An query to be passed to a resolver.
Definition: basic_resolver_query.hpp:38
Rebinds the resolver type to another executor.
Definition: basic_resolver.hpp:76
~basic_resolver()
Destroys the resolver.
Definition: basic_resolver.hpp:216
results_type resolve(ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, asio::error_code &ec)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:351
basic_resolver(const executor_type &ex)
Construct with executor.
Definition: basic_resolver.hpp:107
results_type resolve(const protocol_type &protocol, ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:580
basic_resolver(ExecutionContext &context, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Construct with execution context.
Definition: basic_resolver.hpp:121
basic_resolver_results< InternetProtocol > results_type
The results type.
Definition: basic_resolver.hpp:97
results_type resolve(ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags, asio::error_code &ec)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:445
results_type resolve(const query &q, asio::error_code &ec)
(Deprecated: Use overload with separate host and service parameters.) Perform forward resolution of a...
Definition: basic_resolver.hpp:273
results_type resolve(ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:395
basic_resolver_iterator< InternetProtocol > iterator
(Deprecated.) The iterator type.
Definition: basic_resolver.hpp:93
Definition: resolver_service.hpp:36
results_type resolve(const protocol_type &protocol, ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags, asio::error_code &ec)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:635
ASIO_INITFN_AUTO_RESULT_TYPE(ResolveHandler, void(asio::error_code, results_type)) async_resolve(const query &q
(Deprecated: Use overload with separate host and service parameters.) Asynchronously perform forward ...
Definition: type_traits.hpp:97
Definition: non_const_lvalue.hpp:27
results_type resolve(const protocol_type &protocol, ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:490
A range of entries produced by a resolver.
Definition: basic_resolver_results.hpp:50
results_type resolve(const endpoint_type &e)
Perform reverse resolution of an endpoint to a list of entries.
Definition: basic_resolver.hpp:954
results_type resolve(const endpoint_type &e, asio::error_code &ec)
Perform reverse resolution of an endpoint to a list of entries.
Definition: basic_resolver.hpp:977
InternetProtocol::endpoint endpoint_type
The endpoint type.
Definition: basic_resolver.hpp:86
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: io_object_impl.hpp:33
basic_resolver_query< InternetProtocol > query
(Deprecated.) The query type.
Definition: basic_resolver.hpp:90
results_type resolve(const query &q)
(Deprecated: Use overload with separate host and service parameters.) Perform forward resolution of a...
Definition: basic_resolver.hpp:251
The resolver_base class is used as a base for the basic_resolver class templates to provide a common ...
Definition: resolver_base.hpp:28
Executor executor_type
The type of the executor associated with the object.
Definition: basic_resolver.hpp:72
Provides endpoint resolution functionality.
Definition: basic_resolver.hpp:53
An iterator over the entries produced by a resolver.
Definition: basic_resolver_iterator.hpp:51
results_type resolve(const protocol_type &protocol, ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service, asio::error_code &ec)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:532
void cancel()
Cancel any asynchronous operations that are waiting on the resolver.
Definition: basic_resolver.hpp:232
InternetProtocol protocol_type
The protocol type.
Definition: basic_resolver.hpp:83
executor_type get_executor() ASIO_NOEXCEPT
Get the executor associated with the object.
Definition: basic_resolver.hpp:221
Definition: any_io_executor.hpp:28
results_type resolve(ASIO_STRING_VIEW_PARAM host, ASIO_STRING_VIEW_PARAM service)
Perform forward resolution of a query to a list of entries.
Definition: basic_resolver.hpp:312