resolve_endpoint_op.hpp 4.7 KB

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