Clementine
resolver_service.hpp
1 //
2 // detail/resolver_service.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_DETAIL_RESOLVER_SERVICE_HPP
12 #define 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 "asio/detail/config.hpp"
19 
20 #if !defined(ASIO_WINDOWS_RUNTIME)
21 
22 #include "asio/ip/basic_resolver_query.hpp"
23 #include "asio/ip/basic_resolver_results.hpp"
24 #include "asio/detail/concurrency_hint.hpp"
25 #include "asio/detail/memory.hpp"
26 #include "asio/detail/resolve_endpoint_op.hpp"
27 #include "asio/detail/resolve_query_op.hpp"
28 #include "asio/detail/resolver_service_base.hpp"
29 
30 #include "asio/detail/push_options.hpp"
31 
32 namespace asio {
33 namespace detail {
34 
35 template <typename Protocol>
37  public execution_context_service_base<resolver_service<Protocol> >,
39 {
40 public:
41  // The implementation type of the resolver. A cancellation token is used to
42  // indicate to the background thread that the operation has been cancelled.
43  typedef socket_ops::shared_cancel_token_type implementation_type;
44 
45  // The endpoint type.
46  typedef typename Protocol::endpoint endpoint_type;
47 
48  // The query type.
50 
51  // The results type.
53 
54  // Constructor.
57  resolver_service_base(context)
58  {
59  }
60 
61  // Destroy all user-defined handler objects owned by the service.
62  void shutdown()
63  {
64  this->base_shutdown();
65  }
66 
67  // Perform any fork-related housekeeping.
69  {
70  this->base_notify_fork(fork_ev);
71  }
72 
73  // Resolve a query to a list of entries.
74  results_type resolve(implementation_type&, const query_type& qry,
75  asio::error_code& ec)
76  {
77  asio::detail::addrinfo_type* address_info = 0;
78 
79  socket_ops::getaddrinfo(qry.host_name().c_str(),
80  qry.service_name().c_str(), qry.hints(), &address_info, ec);
81  auto_addrinfo auto_address_info(address_info);
82 
83  return ec ? results_type() : results_type::create(
84  address_info, qry.host_name(), qry.service_name());
85  }
86 
87  // Asynchronously resolve a query to a list of entries.
88  template <typename Handler, typename IoExecutor>
89  void async_resolve(implementation_type& impl, const query_type& qry,
90  Handler& handler, const IoExecutor& io_ex)
91  {
92  // Allocate and construct an operation to wrap the handler.
94  typename op::ptr p = { asio::detail::addressof(handler),
95  op::ptr::allocate(handler), 0 };
96  p.p = new (p.v) op(impl, qry, scheduler_, handler, io_ex);
97 
98  ASIO_HANDLER_CREATION((scheduler_.context(),
99  *p.p, "resolver", &impl, 0, "async_resolve"));
100 
101  start_resolve_op(p.p);
102  p.v = p.p = 0;
103  }
104 
105  // Resolve an endpoint to a list of entries.
106  results_type resolve(implementation_type&,
107  const endpoint_type& endpoint, asio::error_code& ec)
108  {
109  char host_name[NI_MAXHOST];
110  char service_name[NI_MAXSERV];
111  socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
112  host_name, NI_MAXHOST, service_name, NI_MAXSERV,
113  endpoint.protocol().type(), ec);
114 
115  return ec ? results_type() : results_type::create(
116  endpoint, host_name, service_name);
117  }
118 
119  // Asynchronously resolve an endpoint to a list of entries.
120  template <typename Handler, typename IoExecutor>
121  void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
122  Handler& handler, const IoExecutor& io_ex)
123  {
124  // Allocate and construct an operation to wrap the handler.
126  typename op::ptr p = { asio::detail::addressof(handler),
127  op::ptr::allocate(handler), 0 };
128  p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
129 
130  ASIO_HANDLER_CREATION((scheduler_.context(),
131  *p.p, "resolver", &impl, 0, "async_resolve"));
132 
133  start_resolve_op(p.p);
134  p.v = p.p = 0;
135  }
136 };
137 
138 } // namespace detail
139 } // namespace asio
140 
141 #include "asio/detail/pop_options.hpp"
142 
143 #endif // !defined(ASIO_WINDOWS_RUNTIME)
144 
145 #endif // ASIO_DETAIL_RESOLVER_SERVICE_HPP
void notify_fork(execution_context::fork_event fork_ev)
Handle notification of a fork-related event to perform any necessary housekeeping.
Definition: resolver_service.hpp:68
const asio::detail::addrinfo_type & hints() const
Get the hints associated with the query.
Definition: basic_resolver_query.hpp:216
Definition: resolve_query_op.hpp:43
Definition: resolve_endpoint_op.hpp:42
Definition: execution_context.hpp:386
Definition: resolver_service.hpp:36
A context for function object execution.
Definition: execution_context.hpp:105
std::string service_name() const
Get the service name associated with the query.
Definition: basic_resolver_query.hpp:228
Definition: chrono.h:284
fork_event
Fork-related event notifications.
Definition: execution_context.hpp:142
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: resolver_service_base.hpp:40
Definition: resolver_service_base.hpp:99
void shutdown()
Destroy all user-defined handler objects owned by the service.
Definition: resolver_service.hpp:62
std::string host_name() const
Get the host name associated with the query.
Definition: basic_resolver_query.hpp:222
execution_context & context()
Get the context object that owns the service.
Definition: execution_context.hpp:100
Definition: any_io_executor.hpp:28