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