Chris@16
|
1 //
|
Chris@16
|
2 // detail/resolver_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/asio/ip/basic_resolver_iterator.hpp>
|
Chris@16
|
23 #include <boost/asio/ip/basic_resolver_query.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/resolve_endpoint_op.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/resolve_op.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/resolver_service_base.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace asio {
|
Chris@16
|
33 namespace detail {
|
Chris@16
|
34
|
Chris@16
|
35 template <typename Protocol>
|
Chris@16
|
36 class resolver_service : public resolver_service_base
|
Chris@16
|
37 {
|
Chris@16
|
38 public:
|
Chris@16
|
39 // The implementation type of the resolver. A cancellation token is used to
|
Chris@16
|
40 // indicate to the background thread that the operation has been cancelled.
|
Chris@16
|
41 typedef socket_ops::shared_cancel_token_type implementation_type;
|
Chris@16
|
42
|
Chris@16
|
43 // The endpoint type.
|
Chris@16
|
44 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
45
|
Chris@16
|
46 // The query type.
|
Chris@16
|
47 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
|
Chris@16
|
48
|
Chris@16
|
49 // The iterator type.
|
Chris@16
|
50 typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
|
Chris@16
|
51
|
Chris@16
|
52 // Constructor.
|
Chris@16
|
53 resolver_service(boost::asio::io_service& io_service)
|
Chris@16
|
54 : resolver_service_base(io_service)
|
Chris@16
|
55 {
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 // Resolve a query to a list of entries.
|
Chris@16
|
59 iterator_type resolve(implementation_type&, const query_type& query,
|
Chris@16
|
60 boost::system::error_code& ec)
|
Chris@16
|
61 {
|
Chris@16
|
62 boost::asio::detail::addrinfo_type* address_info = 0;
|
Chris@16
|
63
|
Chris@16
|
64 socket_ops::getaddrinfo(query.host_name().c_str(),
|
Chris@16
|
65 query.service_name().c_str(), query.hints(), &address_info, ec);
|
Chris@16
|
66 auto_addrinfo auto_address_info(address_info);
|
Chris@16
|
67
|
Chris@16
|
68 return ec ? iterator_type() : iterator_type::create(
|
Chris@16
|
69 address_info, query.host_name(), query.service_name());
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 // Asynchronously resolve a query to a list of entries.
|
Chris@16
|
73 template <typename Handler>
|
Chris@16
|
74 void async_resolve(implementation_type& impl,
|
Chris@16
|
75 const query_type& query, Handler& handler)
|
Chris@16
|
76 {
|
Chris@16
|
77 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
78 typedef resolve_op<Protocol, Handler> op;
|
Chris@16
|
79 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
80 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
81 sizeof(op), handler), 0 };
|
Chris@16
|
82 p.p = new (p.v) op(impl, query, io_service_impl_, handler);
|
Chris@16
|
83
|
Chris@16
|
84 BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
|
Chris@16
|
85
|
Chris@16
|
86 start_resolve_op(p.p);
|
Chris@16
|
87 p.v = p.p = 0;
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 // Resolve an endpoint to a list of entries.
|
Chris@16
|
91 iterator_type resolve(implementation_type&,
|
Chris@16
|
92 const endpoint_type& endpoint, boost::system::error_code& ec)
|
Chris@16
|
93 {
|
Chris@16
|
94 char host_name[NI_MAXHOST];
|
Chris@16
|
95 char service_name[NI_MAXSERV];
|
Chris@16
|
96 socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
|
Chris@16
|
97 host_name, NI_MAXHOST, service_name, NI_MAXSERV,
|
Chris@16
|
98 endpoint.protocol().type(), ec);
|
Chris@16
|
99
|
Chris@16
|
100 return ec ? iterator_type() : iterator_type::create(
|
Chris@16
|
101 endpoint, host_name, service_name);
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 // Asynchronously resolve an endpoint to a list of entries.
|
Chris@16
|
105 template <typename Handler>
|
Chris@16
|
106 void async_resolve(implementation_type& impl,
|
Chris@16
|
107 const endpoint_type& endpoint, Handler& handler)
|
Chris@16
|
108 {
|
Chris@16
|
109 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
110 typedef resolve_endpoint_op<Protocol, Handler> op;
|
Chris@16
|
111 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
112 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
113 sizeof(op), handler), 0 };
|
Chris@16
|
114 p.p = new (p.v) op(impl, endpoint, io_service_impl_, handler);
|
Chris@16
|
115
|
Chris@16
|
116 BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
|
Chris@16
|
117
|
Chris@16
|
118 start_resolve_op(p.p);
|
Chris@16
|
119 p.v = p.p = 0;
|
Chris@16
|
120 }
|
Chris@16
|
121 };
|
Chris@16
|
122
|
Chris@16
|
123 } // namespace detail
|
Chris@16
|
124 } // namespace asio
|
Chris@16
|
125 } // namespace boost
|
Chris@16
|
126
|
Chris@16
|
127 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
128
|
Chris@16
|
129 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
|
Chris@16
|
130
|
Chris@16
|
131 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
|