Clementine
resolver_service_base.hpp
1 //
2 // detail/resolver_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include "asio/error.hpp"
20 #include "asio/execution_context.hpp"
21 #include "asio/detail/mutex.hpp"
22 #include "asio/detail/noncopyable.hpp"
23 #include "asio/detail/resolve_op.hpp"
24 #include "asio/detail/socket_ops.hpp"
25 #include "asio/detail/socket_types.hpp"
26 #include "asio/detail/scoped_ptr.hpp"
27 #include "asio/detail/thread.hpp"
28 
29 #if defined(ASIO_HAS_IOCP)
30 # include "asio/detail/win_iocp_io_context.hpp"
31 #else // defined(ASIO_HAS_IOCP)
32 # include "asio/detail/scheduler.hpp"
33 #endif // defined(ASIO_HAS_IOCP)
34 
35 #include "asio/detail/push_options.hpp"
36 
37 namespace asio {
38 namespace detail {
39 
41 {
42 public:
43  // The implementation type of the resolver. A cancellation token is used to
44  // indicate to the background thread that the operation has been cancelled.
45  typedef socket_ops::shared_cancel_token_type implementation_type;
46 
47  // Constructor.
48  ASIO_DECL resolver_service_base(execution_context& context);
49 
50  // Destructor.
51  ASIO_DECL ~resolver_service_base();
52 
53  // Destroy all user-defined handler objects owned by the service.
54  ASIO_DECL void base_shutdown();
55 
56  // Perform any fork-related housekeeping.
57  ASIO_DECL void base_notify_fork(
59 
60  // Construct a new resolver implementation.
61  ASIO_DECL void construct(implementation_type& impl);
62 
63  // Destroy a resolver implementation.
64  ASIO_DECL void destroy(implementation_type&);
65 
66  // Move-construct a new resolver implementation.
67  ASIO_DECL void move_construct(implementation_type& impl,
68  implementation_type& other_impl);
69 
70  // Move-assign from another resolver implementation.
71  ASIO_DECL void move_assign(implementation_type& impl,
72  resolver_service_base& other_service,
73  implementation_type& other_impl);
74 
75  // Move-construct a new timer implementation.
76  void converting_move_construct(implementation_type& impl,
77  resolver_service_base&, implementation_type& other_impl)
78  {
79  move_construct(impl, other_impl);
80  }
81 
82  // Move-assign from another timer implementation.
83  void converting_move_assign(implementation_type& impl,
84  resolver_service_base& other_service,
85  implementation_type& other_impl)
86  {
87  move_assign(impl, other_service, other_impl);
88  }
89 
90  // Cancel pending asynchronous operations.
91  ASIO_DECL void cancel(implementation_type& impl);
92 
93 protected:
94  // Helper function to start an asynchronous resolve operation.
95  ASIO_DECL void start_resolve_op(resolve_op* op);
96 
97 #if !defined(ASIO_WINDOWS_RUNTIME)
98  // Helper class to perform exception-safe cleanup of addrinfo objects.
100  : private asio::detail::noncopyable
101  {
102  public:
103  explicit auto_addrinfo(asio::detail::addrinfo_type* ai)
104  : ai_(ai)
105  {
106  }
107 
108  ~auto_addrinfo()
109  {
110  if (ai_)
111  socket_ops::freeaddrinfo(ai_);
112  }
113 
114  operator asio::detail::addrinfo_type*()
115  {
116  return ai_;
117  }
118 
119  private:
120  asio::detail::addrinfo_type* ai_;
121  };
122 #endif // !defined(ASIO_WINDOWS_RUNTIME)
123 
124  // Helper class to run the work scheduler in a thread.
125  class work_scheduler_runner;
126 
127  // Start the work scheduler if it's not already running.
128  ASIO_DECL void start_work_thread();
129 
130  // The scheduler implementation used to post completions.
131 #if defined(ASIO_HAS_IOCP)
132  typedef class win_iocp_io_context scheduler_impl;
133 #else
134  typedef class scheduler scheduler_impl;
135 #endif
136  scheduler_impl& scheduler_;
137 
138 private:
139  // Mutex to protect access to internal data.
140  asio::detail::mutex mutex_;
141 
142  // Private scheduler used for performing asynchronous host resolution.
144 
145  // Thread used for running the work io_context's run loop.
147 };
148 
149 } // namespace detail
150 } // namespace asio
151 
152 #include "asio/detail/pop_options.hpp"
153 
154 #if defined(ASIO_HEADER_ONLY)
155 # include "asio/detail/impl/resolver_service_base.ipp"
156 #endif // defined(ASIO_HEADER_ONLY)
157 
158 #endif // ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP
Definition: null_mutex.hpp:30
Definition: resolve_op.hpp:27
Definition: noncopyable.hpp:25
A context for function object execution.
Definition: execution_context.hpp:105
Definition: chrono.h:284
Definition: resolver_service_base.ipp:26
fork_event
Fork-related event notifications.
Definition: execution_context.hpp:142
Definition: resolver_service_base.hpp:40
Definition: resolver_service_base.hpp:99
Definition: any_io_executor.hpp:28
Definition: scheduler.hpp:38