resolver_service_base.hpp 3.8 KB

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