resolver_service.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // detail/resolver_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <boost/asio/ip/basic_resolver_query.hpp>
  18. #include <boost/asio/ip/basic_resolver_results.hpp>
  19. #include <boost/asio/detail/concurrency_hint.hpp>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/resolve_endpoint_op.hpp>
  22. #include <boost/asio/detail/resolve_query_op.hpp>
  23. #include <boost/asio/detail/resolver_service_base.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename Protocol>
  29. class resolver_service :
  30. public execution_context_service_base<resolver_service<Protocol> >,
  31. public resolver_service_base
  32. {
  33. public:
  34. // The implementation type of the resolver. A cancellation token is used to
  35. // indicate to the background thread that the operation has been cancelled.
  36. typedef socket_ops::shared_cancel_token_type implementation_type;
  37. // The endpoint type.
  38. typedef typename Protocol::endpoint endpoint_type;
  39. // The query type.
  40. typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
  41. // The results type.
  42. typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
  43. // Constructor.
  44. resolver_service(execution_context& context)
  45. : execution_context_service_base<resolver_service<Protocol> >(context),
  46. resolver_service_base(context)
  47. {
  48. }
  49. // Destroy all user-defined handler objects owned by the service.
  50. void shutdown()
  51. {
  52. this->base_shutdown();
  53. }
  54. // Perform any fork-related housekeeping.
  55. void notify_fork(execution_context::fork_event fork_ev)
  56. {
  57. this->base_notify_fork(fork_ev);
  58. }
  59. // Resolve a query to a list of entries.
  60. results_type resolve(implementation_type&, const query_type& qry,
  61. boost::system::error_code& ec)
  62. {
  63. boost::asio::detail::addrinfo_type* address_info = 0;
  64. socket_ops::getaddrinfo(qry.host_name().c_str(),
  65. qry.service_name().c_str(), qry.hints(), &address_info, ec);
  66. auto_addrinfo auto_address_info(address_info);
  67. return ec ? results_type() : results_type::create(
  68. address_info, qry.host_name(), qry.service_name());
  69. }
  70. // Asynchronously resolve a query to a list of entries.
  71. template <typename Handler, typename IoExecutor>
  72. void async_resolve(implementation_type& impl, const query_type& qry,
  73. Handler& handler, const IoExecutor& io_ex)
  74. {
  75. // Allocate and construct an operation to wrap the handler.
  76. typedef resolve_query_op<Protocol, Handler, IoExecutor> op;
  77. typename op::ptr p = { boost::asio::detail::addressof(handler),
  78. op::ptr::allocate(handler), 0 };
  79. p.p = new (p.v) op(impl, qry, scheduler_, handler, io_ex);
  80. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  81. *p.p, "resolver", &impl, 0, "async_resolve"));
  82. start_resolve_op(p.p);
  83. p.v = p.p = 0;
  84. }
  85. // Resolve an endpoint to a list of entries.
  86. results_type resolve(implementation_type&,
  87. const endpoint_type& endpoint, boost::system::error_code& ec)
  88. {
  89. char host_name[NI_MAXHOST];
  90. char service_name[NI_MAXSERV];
  91. socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
  92. host_name, NI_MAXHOST, service_name, NI_MAXSERV,
  93. endpoint.protocol().type(), ec);
  94. return ec ? results_type() : results_type::create(
  95. endpoint, host_name, service_name);
  96. }
  97. // Asynchronously resolve an endpoint to a list of entries.
  98. template <typename Handler, typename IoExecutor>
  99. void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
  100. Handler& handler, const IoExecutor& io_ex)
  101. {
  102. // Allocate and construct an operation to wrap the handler.
  103. typedef resolve_endpoint_op<Protocol, Handler, IoExecutor> op;
  104. typename op::ptr p = { boost::asio::detail::addressof(handler),
  105. op::ptr::allocate(handler), 0 };
  106. p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
  107. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  108. *p.p, "resolver", &impl, 0, "async_resolve"));
  109. start_resolve_op(p.p);
  110. p.v = p.p = 0;
  111. }
  112. };
  113. } // namespace detail
  114. } // namespace asio
  115. } // namespace boost
  116. #include <boost/asio/detail/pop_options.hpp>
  117. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  118. #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP