2 // detail/resolver_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP
12 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/error.hpp>
20 #include <boost/asio/executor_work_guard.hpp>
21 #include <boost/asio/io_context.hpp>
22 #include <boost/asio/detail/mutex.hpp>
23 #include <boost/asio/detail/noncopyable.hpp>
24 #include <boost/asio/detail/resolve_op.hpp>
25 #include <boost/asio/detail/socket_ops.hpp>
26 #include <boost/asio/detail/socket_types.hpp>
27 #include <boost/asio/detail/scoped_ptr.hpp>
28 #include <boost/asio/detail/thread.hpp>
30 #include <boost/asio/detail/push_options.hpp>
36 class resolver_service_base
39 // The implementation type of the resolver. A cancellation token is used to
40 // indicate to the background thread that the operation has been cancelled.
41 typedef socket_ops::shared_cancel_token_type implementation_type;
44 BOOST_ASIO_DECL resolver_service_base(boost::asio::io_context& io_context);
47 BOOST_ASIO_DECL ~resolver_service_base();
49 // Destroy all user-defined handler objects owned by the service.
50 BOOST_ASIO_DECL void base_shutdown();
52 // Perform any fork-related housekeeping.
53 BOOST_ASIO_DECL void base_notify_fork(
54 boost::asio::io_context::fork_event fork_ev);
56 // Construct a new resolver implementation.
57 BOOST_ASIO_DECL void construct(implementation_type& impl);
59 // Destroy a resolver implementation.
60 BOOST_ASIO_DECL void destroy(implementation_type&);
62 // Move-construct a new resolver implementation.
63 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
64 implementation_type& other_impl);
66 // Move-assign from another resolver implementation.
67 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
68 resolver_service_base& other_service,
69 implementation_type& other_impl);
71 // Cancel pending asynchronous operations.
72 BOOST_ASIO_DECL void cancel(implementation_type& impl);
75 // Helper function to start an asynchronous resolve operation.
76 BOOST_ASIO_DECL void start_resolve_op(resolve_op* op);
78 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
79 // Helper class to perform exception-safe cleanup of addrinfo objects.
81 : private boost::asio::detail::noncopyable
84 explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai)
92 socket_ops::freeaddrinfo(ai_);
95 operator boost::asio::detail::addrinfo_type*()
101 boost::asio::detail::addrinfo_type* ai_;
103 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
105 // Helper class to run the work io_context in a thread.
106 class work_io_context_runner;
108 // Start the work thread if it's not already running.
109 BOOST_ASIO_DECL void start_work_thread();
111 // The io_context implementation used to post completions.
112 io_context_impl& io_context_impl_;
115 // Mutex to protect access to internal data.
116 boost::asio::detail::mutex mutex_;
118 // Private io_context used for performing asynchronous host resolution.
119 boost::asio::detail::scoped_ptr<boost::asio::io_context> work_io_context_;
121 // The work io_context implementation used to post completions.
122 io_context_impl& work_io_context_impl_;
124 // Work for the private io_context to perform.
125 boost::asio::executor_work_guard<
126 boost::asio::io_context::executor_type> work_;
128 // Thread used for running the work io_context's run loop.
129 boost::asio::detail::scoped_ptr<boost::asio::detail::thread> work_thread_;
132 } // namespace detail
136 #include <boost/asio/detail/pop_options.hpp>
138 #if defined(BOOST_ASIO_HEADER_ONLY)
139 # include <boost/asio/detail/impl/resolver_service_base.ipp>
140 #endif // defined(BOOST_ASIO_HEADER_ONLY)
142 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP