resolve_query_op.hpp 4.4 KB

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