/Src/Dependencies/Boost/boost/asio/detail/resolve_op.hpp

http://hadesmem.googlecode.com/ · C++ Header · 132 lines · 90 code · 20 blank · 22 comment · 7 complexity · 156ebb39585e7340941b9f10274af0d3 MD5 · raw file

  1. //
  2. // detail/resolve_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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_OP_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/utility/addressof.hpp>
  17. #include <boost/asio/error.hpp>
  18. #include <boost/asio/io_service.hpp>
  19. #include <boost/asio/ip/basic_resolver_iterator.hpp>
  20. #include <boost/asio/ip/basic_resolver_query.hpp>
  21. #include <boost/asio/detail/bind_handler.hpp>
  22. #include <boost/asio/detail/fenced_block.hpp>
  23. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  24. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  25. #include <boost/asio/detail/operation.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_op : public operation
  33. {
  34. public:
  35. BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_op);
  36. typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
  37. typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
  38. resolve_op(socket_ops::weak_cancel_token_type cancel_token,
  39. const query_type& query, io_service_impl& ios, Handler& handler)
  40. : operation(&resolve_op::do_complete),
  41. cancel_token_(cancel_token),
  42. query_(query),
  43. io_service_impl_(ios),
  44. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  45. addrinfo_(0)
  46. {
  47. }
  48. ~resolve_op()
  49. {
  50. if (addrinfo_)
  51. socket_ops::freeaddrinfo(addrinfo_);
  52. }
  53. static void do_complete(io_service_impl* owner, operation* base,
  54. boost::system::error_code /*ec*/, 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 = { boost::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. BOOST_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, boost::system::error_code, iterator_type>
  83. handler(o->handler_, o->ec_, iterator_type());
  84. p.h = boost::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. boost::asio::detail::fenced_block b;
  94. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  95. boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  96. BOOST_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. boost::system::error_code ec_;
  106. boost::asio::detail::addrinfo_type* addrinfo_;
  107. };
  108. } // namespace detail
  109. } // namespace asio
  110. } // namespace boost
  111. #include <boost/asio/detail/pop_options.hpp>
  112. #endif // BOOST_ASIO_DETAIL_RESOLVE_OP_HPP