1 // 2 // detail/resolver_service_base.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_SERVICE_BASE_HPP 12 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_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/detail/mutex.hpp> 22 #include <boost/asio/detail/noncopyable.hpp> 23 #include <boost/asio/detail/operation.hpp> 24 #include <boost/asio/detail/socket_ops.hpp> 25 #include <boost/asio/detail/socket_types.hpp> 26 #include <boost/asio/detail/scoped_ptr.hpp> 27 #include <boost/asio/detail/thread.hpp> 28 29 #include <boost/asio/detail/push_options.hpp> 30 31 namespace boost { 32 namespace asio { 33 namespace detail { 34 35 class resolver_service_base 36 { 37 public: 38 // The implementation type of the resolver. A cancellation token is used to 39 // indicate to the background thread that the operation has been cancelled. 40 typedef socket_ops::shared_cancel_token_type implementation_type; 41 42 // Constructor. 43 BOOST_ASIO_DECL resolver_service_base(boost::asio::io_service& io_service); 44 45 // Destructor. 46 BOOST_ASIO_DECL ~resolver_service_base(); 47 48 // Destroy all user-defined handler objects owned by the service. 49 BOOST_ASIO_DECL void shutdown_service(); 50 51 // Perform any fork-related housekeeping. 52 BOOST_ASIO_DECL void fork_service( 53 boost::asio::io_service::fork_event fork_ev); 54 55 // Construct a new resolver implementation. 56 BOOST_ASIO_DECL void construct(implementation_type& impl); 57 58 // Destroy a resolver implementation. 59 BOOST_ASIO_DECL void destroy(implementation_type&); 60 61 // Cancel pending asynchronous operations. 62 BOOST_ASIO_DECL void cancel(implementation_type& impl); 63 64 protected: 65 // Helper function to start an asynchronous resolve operation. 66 BOOST_ASIO_DECL void start_resolve_op(operation* op); 67 68 // Helper class to perform exception-safe cleanup of addrinfo objects. 69 class auto_addrinfo 70 : private boost::asio::detail::noncopyable 71 { 72 public: auto_addrinfo(boost::asio::detail::addrinfo_type * ai)73 explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai) 74 : ai_(ai) 75 { 76 } 77 ~auto_addrinfo()78 ~auto_addrinfo() 79 { 80 if (ai_) 81 socket_ops::freeaddrinfo(ai_); 82 } 83 operator boost::asio::detail::addrinfo_type*()84 operator boost::asio::detail::addrinfo_type*() 85 { 86 return ai_; 87 } 88 89 private: 90 boost::asio::detail::addrinfo_type* ai_; 91 }; 92 93 // Helper class to run the work io_service in a thread. 94 class work_io_service_runner; 95 96 // Start the work thread if it's not already running. 97 BOOST_ASIO_DECL void start_work_thread(); 98 99 // The io_service implementation used to post completions. 100 io_service_impl& io_service_impl_; 101 102 private: 103 // Mutex to protect access to internal data. 104 boost::asio::detail::mutex mutex_; 105 106 // Private io_service used for performing asynchronous host resolution. 107 boost::asio::detail::scoped_ptr<boost::asio::io_service> work_io_service_; 108 109 // The work io_service implementation used to post completions. 110 io_service_impl& work_io_service_impl_; 111 112 // Work for the private io_service to perform. 113 boost::asio::detail::scoped_ptr<boost::asio::io_service::work> work_; 114 115 // Thread used for running the work io_service's run loop. 116 boost::asio::detail::scoped_ptr<boost::asio::detail::thread> work_thread_; 117 }; 118 119 } // namespace detail 120 } // namespace asio 121 } // namespace boost 122 123 #include <boost/asio/detail/pop_options.hpp> 124 125 #if defined(BOOST_ASIO_HEADER_ONLY) 126 # include <boost/asio/detail/impl/resolver_service_base.ipp> 127 #endif // defined(BOOST_ASIO_HEADER_ONLY) 128 129 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP 130