resolve_endpoint_op.hpp 4.2 KB

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