resolve_op.hpp 4.0 KB

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