resolver_service_base.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // detail/resolver_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. // Cancel pending asynchronous operations.
  61. BOOST_ASIO_DECL void cancel(implementation_type& impl);
  62. protected:
  63. // Helper function to start an asynchronous resolve operation.
  64. BOOST_ASIO_DECL void start_resolve_op(resolve_op* op);
  65. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  66. // Helper class to perform exception-safe cleanup of addrinfo objects.
  67. class auto_addrinfo
  68. : private boost::asio::detail::noncopyable
  69. {
  70. public:
  71. explicit auto_addrinfo(boost::asio::detail::addrinfo_type* ai)
  72. : ai_(ai)
  73. {
  74. }
  75. ~auto_addrinfo()
  76. {
  77. if (ai_)
  78. socket_ops::freeaddrinfo(ai_);
  79. }
  80. operator boost::asio::detail::addrinfo_type*()
  81. {
  82. return ai_;
  83. }
  84. private:
  85. boost::asio::detail::addrinfo_type* ai_;
  86. };
  87. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  88. // Helper class to run the work scheduler in a thread.
  89. class work_scheduler_runner;
  90. // Start the work scheduler if it's not already running.
  91. BOOST_ASIO_DECL void start_work_thread();
  92. // The scheduler implementation used to post completions.
  93. #if defined(BOOST_ASIO_HAS_IOCP)
  94. typedef class win_iocp_io_context scheduler_impl;
  95. #else
  96. typedef class scheduler scheduler_impl;
  97. #endif
  98. scheduler_impl& scheduler_;
  99. private:
  100. // Mutex to protect access to internal data.
  101. boost::asio::detail::mutex mutex_;
  102. // Private scheduler used for performing asynchronous host resolution.
  103. boost::asio::detail::scoped_ptr<scheduler_impl> work_scheduler_;
  104. // Thread used for running the work io_context's run loop.
  105. boost::asio::detail::scoped_ptr<boost::asio::detail::thread> work_thread_;
  106. };
  107. } // namespace detail
  108. } // namespace asio
  109. } // namespace boost
  110. #include <boost/asio/detail/pop_options.hpp>
  111. #if defined(BOOST_ASIO_HEADER_ONLY)
  112. # include <boost/asio/detail/impl/resolver_service_base.ipp>
  113. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  114. #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_BASE_HPP