resolver_service_base.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // detail/resolver_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP
  11. #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/error.hpp>
  17. #include <boost/asio/execution_context.hpp>
  18. #include <boost/asio/detail/mutex.hpp>
  19. #include <boost/asio/detail/noncopyable.hpp>
  20. #include <boost/asio/detail/resolve_op.hpp>
  21. #include <boost/asio/detail/socket_ops.hpp>
  22. #include <boost/asio/detail/socket_types.hpp>
  23. #include <boost/asio/detail/scoped_ptr.hpp>
  24. #include <boost/asio/detail/thread.hpp>
  25. #if defined(BOOST_ASIO_HAS_IOCP)
  26. # include <boost/asio/detail/win_iocp_io_context.hpp>
  27. #else // defined(BOOST_ASIO_HAS_IOCP)
  28. # include <boost/asio/detail/scheduler.hpp>
  29. #endif // defined(BOOST_ASIO_HAS_IOCP)
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace detail {
  34. class resolver_service_base
  35. {
  36. public:
  37. // The implementation type of the resolver. A cancellation token is used to
  38. // indicate to the background thread that the operation has been cancelled.
  39. typedef socket_ops::shared_cancel_token_type implementation_type;
  40. // Constructor.
  41. BOOST_ASIO_DECL resolver_service_base(execution_context& context);
  42. // Destructor.
  43. BOOST_ASIO_DECL ~resolver_service_base();
  44. // Destroy all user-defined handler objects owned by the service.
  45. BOOST_ASIO_DECL void base_shutdown();
  46. // Perform any fork-related housekeeping.
  47. BOOST_ASIO_DECL void base_notify_fork(
  48. execution_context::fork_event fork_ev);
  49. // Construct a new resolver implementation.
  50. BOOST_ASIO_DECL void construct(implementation_type& impl);
  51. // Destroy a resolver implementation.
  52. BOOST_ASIO_DECL void destroy(implementation_type&);
  53. // Move-construct a new resolver implementation.
  54. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  55. implementation_type& other_impl);
  56. // Move-assign from another resolver implementation.
  57. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  58. resolver_service_base& other_service,
  59. implementation_type& other_impl);
  60. // Move-construct a new timer implementation.
  61. void converting_move_construct(implementation_type& impl,
  62. resolver_service_base&, implementation_type& other_impl)
  63. {
  64. move_construct(impl, other_impl);
  65. }
  66. // Move-assign from another timer implementation.
  67. void converting_move_assign(implementation_type& impl,
  68. resolver_service_base& other_service,
  69. implementation_type& other_impl)
  70. {
  71. move_assign(impl, other_service, other_impl);
  72. }
  73. // Cancel pending asynchronous operations.
  74. BOOST_ASIO_DECL void cancel(implementation_type& impl);
  75. protected:
  76. // Helper function to start an asynchronous resolve operation.
  77. 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.
  80. class auto_addrinfo
  81. : private boost::asio::detail::noncopyable
  82. {
  83. public:
  84. explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai)
  85. : ai_(ai)
  86. {
  87. }
  88. ~auto_addrinfo()
  89. {
  90. if (ai_)
  91. socket_ops::freeaddrinfo(ai_);
  92. }
  93. operator boost::asio::detail::addrinfo_type*()
  94. {
  95. return ai_;
  96. }
  97. private:
  98. boost::asio::detail::addrinfo_type* ai_;
  99. };
  100. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  101. // Helper class to run the work scheduler in a thread.
  102. class work_scheduler_runner;
  103. // Start the work scheduler if it's not already running.
  104. BOOST_ASIO_DECL void start_work_thread();
  105. // The scheduler implementation used to post completions.
  106. #if defined(BOOST_ASIO_HAS_IOCP)
  107. typedef class win_iocp_io_context scheduler_impl;
  108. #else
  109. typedef class scheduler scheduler_impl;
  110. #endif
  111. scheduler_impl& scheduler_;
  112. private:
  113. // Mutex to protect access to internal data.
  114. boost::asio::detail::mutex mutex_;
  115. // Private scheduler used for performing asynchronous host resolution.
  116. boost::asio::detail::scoped_ptr<scheduler_impl> work_scheduler_;
  117. // Thread used for running the work io_context's run loop.
  118. boost::asio::detail::scoped_ptr<boost::asio::detail::thread> work_thread_;
  119. };
  120. } // namespace detail
  121. } // namespace asio
  122. } // namespace boost
  123. #include <boost/asio/detail/pop_options.hpp>
  124. #if defined(BOOST_ASIO_HEADER_ONLY)
  125. # include <boost/asio/detail/impl/resolver_service_base.ipp>
  126. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  127. #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP