1//
2// detail/resolve_endpoint_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 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/asio/error.hpp>
20#include <boost/asio/io_service.hpp>
21#include <boost/asio/ip/basic_resolver_iterator.hpp>
22#include <boost/asio/detail/addressof.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 const boost::system::error_code& /*ec*/,
57 std::size_t /*bytes_transferred*/)
58 {
59 // Take ownership of the operation object.
60 resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base));
61 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
62
63 if (owner && owner != &o->io_service_impl_)
64 {
65 // The operation is being run on the worker io_service. Time to perform
66 // the resolver operation.
67
68 // Perform the blocking endpoint resolution operation.
69 char host_name[NI_MAXHOST];
70 char service_name[NI_MAXSERV];
71 socket_ops::background_getnameinfo(cancel_token: o->cancel_token_, addr: o->endpoint_.data(),
72 addrlen: o->endpoint_.size(), host: host_name, NI_MAXHOST, serv: service_name, NI_MAXSERV,
73 sock_type: o->endpoint_.protocol().type(), ec&: o->ec_);
74 o->iter_ = iterator_type::create(o->endpoint_, host_name, service_name);
75
76 // Pass operation back to main io_service for completion.
77 o->io_service_impl_.post_deferred_completion(o);
78 p.v = p.p = 0;
79 }
80 else
81 {
82 // The operation has been returned to the main io_service. The completion
83 // handler is ready to be delivered.
84
85 BOOST_ASIO_HANDLER_COMPLETION((o));
86
87 // Make a copy of the handler so that the memory can be deallocated
88 // before the upcall is made. Even if we're not about to make an upcall,
89 // a sub-object of the handler may be the true owner of the memory
90 // associated with the handler. Consequently, a local copy of the handler
91 // is required to ensure that any owning sub-object remains valid until
92 // after we have deallocated the memory here.
93 detail::binder2<Handler, boost::system::error_code, iterator_type>
94 handler(o->handler_, o->ec_, o->iter_);
95 p.h = boost::asio::detail::addressof(handler.handler_);
96 p.reset();
97
98 if (owner)
99 {
100 fenced_block b(fenced_block::half);
101 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
102 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
103 BOOST_ASIO_HANDLER_INVOCATION_END;
104 }
105 }
106 }
107
108private:
109 socket_ops::weak_cancel_token_type cancel_token_;
110 endpoint_type endpoint_;
111 io_service_impl& io_service_impl_;
112 Handler handler_;
113 boost::system::error_code ec_;
114 iterator_type iter_;
115};
116
117} // namespace detail
118} // namespace asio
119} // namespace boost
120
121#include <boost/asio/detail/pop_options.hpp>
122
123#endif // BOOST_ASIO_DETAIL_RESOLVER_ENDPOINT_OP_HPP
124

source code of boost/boost/asio/detail/resolve_endpoint_op.hpp