resolver_service_base.hpp 4.4 KB

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