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