resolve_endpoint_op.hpp 4.2 KB

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