PageRenderTime 36ms CodeModel.GetById 24ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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