resolver_service.hpp 4.6 KB

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