Chris@16: // Chris@16: // detail/resolve_endpoint_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_RESOLVER_ENDPOINT_OP_HPP Chris@16: #define BOOST_ASIO_DETAIL_RESOLVER_ENDPOINT_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: 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_endpoint_op : public operation Chris@16: { Chris@16: public: Chris@16: BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_endpoint_op); Chris@16: Chris@16: typedef typename Protocol::endpoint endpoint_type; Chris@16: typedef boost::asio::ip::basic_resolver_iterator iterator_type; Chris@16: Chris@16: resolve_endpoint_op(socket_ops::weak_cancel_token_type cancel_token, Chris@16: const endpoint_type& endpoint, io_service_impl& ios, Handler& handler) Chris@16: : operation(&resolve_endpoint_op::do_complete), Chris@16: cancel_token_(cancel_token), Chris@16: endpoint_(endpoint), Chris@16: io_service_impl_(ios), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) Chris@16: { 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_endpoint_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 endpoint resolution operation. Chris@16: char host_name[NI_MAXHOST]; Chris@16: char service_name[NI_MAXSERV]; Chris@16: socket_ops::background_getnameinfo(o->cancel_token_, o->endpoint_.data(), Chris@16: o->endpoint_.size(), host_name, NI_MAXHOST, service_name, NI_MAXSERV, Chris@16: o->endpoint_.protocol().type(), o->ec_); Chris@16: o->iter_ = iterator_type::create(o->endpoint_, host_name, service_name); 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_, o->iter_); Chris@16: p.h = boost::asio::detail::addressof(handler.handler_); 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: endpoint_type endpoint_; Chris@16: io_service_impl& io_service_impl_; Chris@16: Handler handler_; Chris@16: boost::system::error_code ec_; Chris@16: iterator_type iter_; 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_RESOLVER_ENDPOINT_OP_HPP