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

http://hadesmem.googlecode.com/ · C++ Header · 122 lines · 81 code · 19 blank · 22 comment · 5 complexity · b61d880496059b24353f48b7c67777ae MD5 · raw file

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