PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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