resolver_service.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // detail/resolver_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_iterator.hpp>
  18. #include <boost/asio/ip/basic_resolver_query.hpp>
  19. #include <boost/asio/detail/addressof.hpp>
  20. #include <boost/asio/detail/resolve_endpoint_op.hpp>
  21. #include <boost/asio/detail/resolve_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 : public resolver_service_base
  29. {
  30. public:
  31. // The implementation type of the resolver. A cancellation token is used to
  32. // indicate to the background thread that the operation has been cancelled.
  33. typedef socket_ops::shared_cancel_token_type implementation_type;
  34. // The endpoint type.
  35. typedef typename Protocol::endpoint endpoint_type;
  36. // The query type.
  37. typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
  38. // The iterator type.
  39. typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
  40. // Constructor.
  41. resolver_service(boost::asio::io_service& io_service)
  42. : resolver_service_base(io_service)
  43. {
  44. }
  45. // Resolve a query to a list of entries.
  46. iterator_type resolve(implementation_type&, const query_type& query,
  47. boost::system::error_code& ec)
  48. {
  49. boost::asio::detail::addrinfo_type* address_info = 0;
  50. socket_ops::getaddrinfo(query.host_name().c_str(),
  51. query.service_name().c_str(), query.hints(), &address_info, ec);
  52. auto_addrinfo auto_address_info(address_info);
  53. return ec ? iterator_type() : iterator_type::create(
  54. address_info, query.host_name(), query.service_name());
  55. }
  56. // Asynchronously resolve a query to a list of entries.
  57. template <typename Handler>
  58. void async_resolve(implementation_type& impl,
  59. const query_type& query, Handler& handler)
  60. {
  61. // Allocate and construct an operation to wrap the handler.
  62. typedef resolve_op<Protocol, Handler> op;
  63. typename op::ptr p = { boost::asio::detail::addressof(handler),
  64. boost_asio_handler_alloc_helpers::allocate(
  65. sizeof(op), handler), 0 };
  66. p.p = new (p.v) op(impl, query, io_service_impl_, handler);
  67. BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
  68. start_resolve_op(p.p);
  69. p.v = p.p = 0;
  70. }
  71. // Resolve an endpoint to a list of entries.
  72. iterator_type resolve(implementation_type&,
  73. const endpoint_type& endpoint, boost::system::error_code& ec)
  74. {
  75. char host_name[NI_MAXHOST];
  76. char service_name[NI_MAXSERV];
  77. socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
  78. host_name, NI_MAXHOST, service_name, NI_MAXSERV,
  79. endpoint.protocol().type(), ec);
  80. return ec ? iterator_type() : iterator_type::create(
  81. endpoint, host_name, service_name);
  82. }
  83. // Asynchronously resolve an endpoint to a list of entries.
  84. template <typename Handler>
  85. void async_resolve(implementation_type& impl,
  86. const endpoint_type& endpoint, Handler& handler)
  87. {
  88. // Allocate and construct an operation to wrap the handler.
  89. typedef resolve_endpoint_op<Protocol, Handler> op;
  90. typename op::ptr p = { boost::asio::detail::addressof(handler),
  91. boost_asio_handler_alloc_helpers::allocate(
  92. sizeof(op), handler), 0 };
  93. p.p = new (p.v) op(impl, endpoint, io_service_impl_, handler);
  94. BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
  95. start_resolve_op(p.p);
  96. p.v = p.p = 0;
  97. }
  98. };
  99. } // namespace detail
  100. } // namespace asio
  101. } // namespace boost
  102. #include <boost/asio/detail/pop_options.hpp>
  103. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  104. #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP