Chris@16: // Chris@16: // detail/resolve_op.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_RESOLVE_OP_HPP Chris@16: #define BOOST_ASIO_DETAIL_RESOLVE_OP_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: class resolve_op : public operation Chris@16: { Chris@16: public: Chris@16: BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_op); Chris@16: Chris@16: typedef boost::asio::ip::basic_resolver_query query_type; Chris@16: typedef boost::asio::ip::basic_resolver_iterator iterator_type; Chris@16: Chris@16: resolve_op(socket_ops::weak_cancel_token_type cancel_token, Chris@16: const query_type& query, io_service_impl& ios, Handler& handler) Chris@16: : operation(&resolve_op::do_complete), Chris@16: cancel_token_(cancel_token), Chris@16: query_(query), Chris@16: io_service_impl_(ios), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), Chris@16: addrinfo_(0) Chris@16: { Chris@16: } Chris@16: Chris@16: ~resolve_op() Chris@16: { Chris@16: if (addrinfo_) Chris@16: socket_ops::freeaddrinfo(addrinfo_); Chris@16: } Chris@16: Chris@16: static void do_complete(io_service_impl* owner, operation* base, Chris@16: const boost::system::error_code& /*ec*/, Chris@16: std::size_t /*bytes_transferred*/) Chris@16: { Chris@16: // Take ownership of the operation object. Chris@16: resolve_op* o(static_cast(base)); Chris@16: ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; Chris@16: Chris@16: if (owner && owner != &o->io_service_impl_) Chris@16: { Chris@16: // The operation is being run on the worker io_service. Time to perform Chris@16: // the resolver operation. Chris@16: Chris@16: // Perform the blocking host resolution operation. Chris@16: socket_ops::background_getaddrinfo(o->cancel_token_, Chris@16: o->query_.host_name().c_str(), o->query_.service_name().c_str(), Chris@16: o->query_.hints(), &o->addrinfo_, o->ec_); Chris@16: Chris@16: // Pass operation back to main io_service for completion. Chris@16: o->io_service_impl_.post_deferred_completion(o); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: else Chris@16: { Chris@16: // The operation has been returned to the main io_service. The completion Chris@16: // handler is ready to be delivered. Chris@16: Chris@16: BOOST_ASIO_HANDLER_COMPLETION((o)); Chris@16: Chris@16: // Make a copy of the handler so that the memory can be deallocated Chris@16: // before the upcall is made. Even if we're not about to make an upcall, Chris@16: // a sub-object of the handler may be the true owner of the memory Chris@16: // associated with the handler. Consequently, a local copy of the handler Chris@16: // is required to ensure that any owning sub-object remains valid until Chris@16: // after we have deallocated the memory here. Chris@16: detail::binder2 Chris@16: handler(o->handler_, o->ec_, iterator_type()); Chris@16: p.h = boost::asio::detail::addressof(handler.handler_); Chris@16: if (o->addrinfo_) Chris@16: { Chris@16: handler.arg2_ = iterator_type::create(o->addrinfo_, Chris@16: o->query_.host_name(), o->query_.service_name()); Chris@16: } Chris@16: p.reset(); Chris@16: Chris@16: if (owner) Chris@16: { Chris@16: fenced_block b(fenced_block::half); Chris@16: BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "...")); Chris@16: boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_); Chris@16: BOOST_ASIO_HANDLER_INVOCATION_END; Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: socket_ops::weak_cancel_token_type cancel_token_; Chris@16: query_type query_; Chris@16: io_service_impl& io_service_impl_; Chris@16: Handler handler_; Chris@16: boost::system::error_code ec_; Chris@16: boost::asio::detail::addrinfo_type* addrinfo_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_RESOLVE_OP_HPP