resolver_service_base.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // detail/resolver_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/executor_work_guard.hpp>
  18. #include <boost/asio/io_context.hpp>
  19. #include <boost/asio/detail/mutex.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/resolve_op.hpp>
  22. #include <boost/asio/detail/socket_ops.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/detail/scoped_ptr.hpp>
  25. #include <boost/asio/detail/thread.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. class resolver_service_base
  31. {
  32. public:
  33. // The implementation type of the resolver. A cancellation token is used to
  34. // indicate to the background thread that the operation has been cancelled.
  35. typedef socket_ops::shared_cancel_token_type implementation_type;
  36. // Constructor.
  37. BOOST_ASIO_DECL resolver_service_base(boost::asio::io_context& io_context);
  38. // Destructor.
  39. BOOST_ASIO_DECL ~resolver_service_base();
  40. // Destroy all user-defined handler objects owned by the service.
  41. BOOST_ASIO_DECL void base_shutdown();
  42. // Perform any fork-related housekeeping.
  43. BOOST_ASIO_DECL void base_notify_fork(
  44. boost::asio::io_context::fork_event fork_ev);
  45. // Construct a new resolver implementation.
  46. BOOST_ASIO_DECL void construct(implementation_type& impl);
  47. // Destroy a resolver implementation.
  48. BOOST_ASIO_DECL void destroy(implementation_type&);
  49. // Move-construct a new resolver implementation.
  50. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  51. implementation_type& other_impl);
  52. // Move-assign from another resolver implementation.
  53. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  54. resolver_service_base& other_service,
  55. implementation_type& other_impl);
  56. // Cancel pending asynchronous operations.
  57. BOOST_ASIO_DECL void cancel(implementation_type& impl);
  58. protected:
  59. // Helper function to start an asynchronous resolve operation.
  60. BOOST_ASIO_DECL void start_resolve_op(resolve_op* op);
  61. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  62. // Helper class to perform exception-safe cleanup of addrinfo objects.
  63. class auto_addrinfo
  64. : private boost::asio::detail::noncopyable
  65. {
  66. public:
  67. explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai)
  68. : ai_(ai)
  69. {
  70. }
  71. ~auto_addrinfo()
  72. {
  73. if (ai_)
  74. socket_ops::freeaddrinfo(ai_);
  75. }
  76. operator boost::asio::detail::addrinfo_type*()
  77. {
  78. return ai_;
  79. }
  80. private:
  81. boost::asio::detail::addrinfo_type* ai_;
  82. };
  83. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  84. // Helper class to run the work io_context in a thread.
  85. class work_io_context_runner;
  86. // Start the work thread if it's not already running.
  87. BOOST_ASIO_DECL void start_work_thread();
  88. // The io_context implementation used to post completions.
  89. io_context_impl& io_context_impl_;
  90. private:
  91. // Mutex to protect access to internal data.
  92. boost::asio::detail::mutex mutex_;
  93. // Private io_context used for performing asynchronous host resolution.
  94. boost::asio::detail::scoped_ptr<boost::asio::io_context> work_io_context_;
  95. // The work io_context implementation used to post completions.
  96. io_context_impl& work_io_context_impl_;
  97. // Work for the private io_context to perform.
  98. boost::asio::executor_work_guard<
  99. boost::asio::io_context::executor_type> work_;
  100. // Thread used for running the work io_context's run loop.
  101. boost::asio::detail::scoped_ptr<boost::asio::detail::thread> work_thread_;
  102. };
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #if defined(BOOST_ASIO_HEADER_ONLY)
  108. # include <boost/asio/detail/impl/resolver_service_base.ipp>
  109. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  110. #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP