resolve_query_op.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // detail/resolve_query_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_RESOLVE_QUERY_OP_HPP
  11. #define BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_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. #include <boost/asio/error.hpp>
  17. #include <boost/asio/ip/basic_resolver_query.hpp>
  18. #include <boost/asio/ip/basic_resolver_results.hpp>
  19. #include <boost/asio/detail/bind_handler.hpp>
  20. #include <boost/asio/detail/fenced_block.hpp>
  21. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  22. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  23. #include <boost/asio/detail/memory.hpp>
  24. #include <boost/asio/detail/resolve_op.hpp>
  25. #include <boost/asio/detail/socket_ops.hpp>
  26. #if defined(BOOST_ASIO_HAS_IOCP)
  27. # include <boost/asio/detail/win_iocp_io_context.hpp>
  28. #else // defined(BOOST_ASIO_HAS_IOCP)
  29. # include <boost/asio/detail/scheduler.hpp>
  30. #endif // defined(BOOST_ASIO_HAS_IOCP)
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail {
  35. template <typename Protocol, typename Handler, typename IoExecutor>
  36. class resolve_query_op : public resolve_op
  37. {
  38. public:
  39. BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_query_op);
  40. typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
  41. typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
  42. #if defined(BOOST_ASIO_HAS_IOCP)
  43. typedef class win_iocp_io_context scheduler_impl;
  44. #else
  45. typedef class scheduler scheduler_impl;
  46. #endif
  47. resolve_query_op(socket_ops::weak_cancel_token_type cancel_token,
  48. const query_type& query, scheduler_impl& sched,
  49. Handler& handler, const IoExecutor& io_ex)
  50. : resolve_op(&resolve_query_op::do_complete),
  51. cancel_token_(cancel_token),
  52. query_(query),
  53. scheduler_(sched),
  54. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  55. io_executor_(io_ex),
  56. addrinfo_(0)
  57. {
  58. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  59. }
  60. ~resolve_query_op()
  61. {
  62. if (addrinfo_)
  63. socket_ops::freeaddrinfo(addrinfo_);
  64. }
  65. static void do_complete(void* owner, operation* base,
  66. const boost::system::error_code& /*ec*/,
  67. std::size_t /*bytes_transferred*/)
  68. {
  69. // Take ownership of the operation object.
  70. resolve_query_op* o(static_cast<resolve_query_op*>(base));
  71. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  72. if (owner && owner != &o->scheduler_)
  73. {
  74. // The operation is being run on the worker io_context. Time to perform
  75. // the resolver operation.
  76. // Perform the blocking host resolution operation.
  77. socket_ops::background_getaddrinfo(o->cancel_token_,
  78. o->query_.host_name().c_str(), o->query_.service_name().c_str(),
  79. o->query_.hints(), &o->addrinfo_, o->ec_);
  80. // Pass operation back to main io_context for completion.
  81. o->scheduler_.post_deferred_completion(o);
  82. p.v = p.p = 0;
  83. }
  84. else
  85. {
  86. // The operation has been returned to the main io_context. The completion
  87. // handler is ready to be delivered.
  88. // Take ownership of the operation's outstanding work.
  89. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  90. BOOST_ASIO_HANDLER_COMPLETION((*o));
  91. // Make a copy of the handler so that the memory can be deallocated
  92. // before the upcall is made. Even if we're not about to make an upcall,
  93. // a sub-object of the handler may be the true owner of the memory
  94. // associated with the handler. Consequently, a local copy of the handler
  95. // is required to ensure that any owning sub-object remains valid until
  96. // after we have deallocated the memory here.
  97. detail::binder2<Handler, boost::system::error_code, results_type>
  98. handler(o->handler_, o->ec_, results_type());
  99. p.h = boost::asio::detail::addressof(handler.handler_);
  100. if (o->addrinfo_)
  101. {
  102. handler.arg2_ = results_type::create(o->addrinfo_,
  103. o->query_.host_name(), o->query_.service_name());
  104. }
  105. p.reset();
  106. if (owner)
  107. {
  108. fenced_block b(fenced_block::half);
  109. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  110. w.complete(handler, handler.handler_);
  111. BOOST_ASIO_HANDLER_INVOCATION_END;
  112. }
  113. }
  114. }
  115. private:
  116. socket_ops::weak_cancel_token_type cancel_token_;
  117. query_type query_;
  118. scheduler_impl& scheduler_;
  119. Handler handler_;
  120. IoExecutor io_executor_;
  121. boost::asio::detail::addrinfo_type* addrinfo_;
  122. };
  123. } // namespace detail
  124. } // namespace asio
  125. } // namespace boost
  126. #include <boost/asio/detail/pop_options.hpp>
  127. #endif // BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_HPP