resolver_service.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // detail/resolver_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_DETAIL_RESOLVER_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if !defined(ASIO_WINDOWS_RUNTIME)
  17. #include "asio/ip/basic_resolver_query.hpp"
  18. #include "asio/ip/basic_resolver_results.hpp"
  19. #include "asio/detail/concurrency_hint.hpp"
  20. #include "asio/detail/memory.hpp"
  21. #include "asio/detail/resolve_endpoint_op.hpp"
  22. #include "asio/detail/resolve_query_op.hpp"
  23. #include "asio/detail/resolver_service_base.hpp"
  24. #include "asio/detail/push_options.hpp"
  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 asio::ip::basic_resolver_query<Protocol> query_type;
  40. // The results type.
  41. typedef 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. this->base_shutdown();
  52. }
  53. // Perform any fork-related housekeeping.
  54. void notify_fork(execution_context::fork_event fork_ev)
  55. {
  56. this->base_notify_fork(fork_ev);
  57. }
  58. // Resolve a query to a list of entries.
  59. results_type resolve(implementation_type&, const query_type& qry,
  60. asio::error_code& ec)
  61. {
  62. asio::detail::addrinfo_type* address_info = 0;
  63. socket_ops::getaddrinfo(qry.host_name().c_str(),
  64. qry.service_name().c_str(), qry.hints(), &address_info, ec);
  65. auto_addrinfo auto_address_info(address_info);
  66. ASIO_ERROR_LOCATION(ec);
  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 = { asio::detail::addressof(handler),
  78. op::ptr::allocate(handler), 0 };
  79. p.p = new (p.v) op(impl, qry, scheduler_, handler, io_ex);
  80. 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, asio::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. ASIO_ERROR_LOCATION(ec);
  95. return ec ? results_type() : results_type::create(
  96. endpoint, host_name, service_name);
  97. }
  98. // Asynchronously resolve an endpoint to a list of entries.
  99. template <typename Handler, typename IoExecutor>
  100. void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
  101. Handler& handler, const IoExecutor& io_ex)
  102. {
  103. // Allocate and construct an operation to wrap the handler.
  104. typedef resolve_endpoint_op<Protocol, Handler, IoExecutor> op;
  105. typename op::ptr p = { asio::detail::addressof(handler),
  106. op::ptr::allocate(handler), 0 };
  107. p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
  108. ASIO_HANDLER_CREATION((scheduler_.context(),
  109. *p.p, "resolver", &impl, 0, "async_resolve"));
  110. start_resolve_op(p.p);
  111. p.v = p.p = 0;
  112. }
  113. };
  114. } // namespace detail
  115. } // namespace asio
  116. #include "asio/detail/pop_options.hpp"
  117. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  118. #endif // ASIO_DETAIL_RESOLVER_SERVICE_HPP