select_reactor.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // detail/select_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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_SELECT_REACTOR_HPP
  11. #define ASIO_DETAIL_SELECT_REACTOR_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. #if defined(ASIO_HAS_IOCP) \
  17. || (!defined(ASIO_HAS_DEV_POLL) \
  18. && !defined(ASIO_HAS_EPOLL) \
  19. && !defined(ASIO_HAS_KQUEUE))
  20. #include <cstddef>
  21. #include "asio/detail/mutex.hpp"
  22. #include "asio/detail/op_queue.hpp"
  23. #include "asio/detail/reactor_op.hpp"
  24. #include "asio/detail/reactor_op_queue.hpp"
  25. #include "asio/detail/select_interrupter.hpp"
  26. #include "asio/detail/select_reactor_fwd.hpp"
  27. #include "asio/detail/socket_types.hpp"
  28. #include "asio/detail/timer_op.hpp"
  29. #include "asio/detail/timer_queue_base.hpp"
  30. #include "asio/detail/timer_queue_fwd.hpp"
  31. #include "asio/detail/timer_queue_set.hpp"
  32. #include "asio/io_service.hpp"
  33. #if defined(ASIO_HAS_IOCP)
  34. # include "asio/detail/thread.hpp"
  35. #endif // defined(ASIO_HAS_IOCP)
  36. #include "asio/detail/push_options.hpp"
  37. namespace asio {
  38. namespace detail {
  39. class select_reactor
  40. : public asio::detail::service_base<select_reactor>
  41. {
  42. public:
  43. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  44. enum { read_op = 0, write_op = 1, except_op = 2,
  45. max_select_ops = 3, connect_op = 3, max_ops = 4 };
  46. #else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  47. enum { read_op = 0, write_op = 1, except_op = 2,
  48. max_select_ops = 3, connect_op = 1, max_ops = 3 };
  49. #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  50. // Per-descriptor data.
  51. struct per_descriptor_data
  52. {
  53. };
  54. // Constructor.
  55. ASIO_DECL select_reactor(asio::io_service& io_service);
  56. // Destructor.
  57. ASIO_DECL ~select_reactor();
  58. // Destroy all user-defined handler objects owned by the service.
  59. ASIO_DECL void shutdown_service();
  60. // Initialise the task, but only if the reactor is not in its own thread.
  61. ASIO_DECL void init_task();
  62. // Register a socket with the reactor. Returns 0 on success, system error
  63. // code on failure.
  64. ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&);
  65. // Post a reactor operation for immediate completion.
  66. void post_immediate_completion(reactor_op* op)
  67. {
  68. io_service_.post_immediate_completion(op);
  69. }
  70. // Start a new operation. The reactor operation will be performed when the
  71. // given descriptor is flagged as ready, or an error has occurred.
  72. ASIO_DECL void start_op(int op_type, socket_type descriptor,
  73. per_descriptor_data&, reactor_op* op, bool);
  74. // Cancel all operations associated with the given descriptor. The
  75. // handlers associated with the descriptor will be invoked with the
  76. // operation_aborted error.
  77. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&);
  78. // Cancel any operations that are running against the descriptor and remove
  79. // its registration from the reactor.
  80. ASIO_DECL void close_descriptor(socket_type descriptor,
  81. per_descriptor_data&);
  82. // Add a new timer queue to the reactor.
  83. template <typename Time_Traits>
  84. void add_timer_queue(timer_queue<Time_Traits>& queue);
  85. // Remove a timer queue from the reactor.
  86. template <typename Time_Traits>
  87. void remove_timer_queue(timer_queue<Time_Traits>& queue);
  88. // Schedule a new operation in the given timer queue to expire at the
  89. // specified absolute time.
  90. template <typename Time_Traits>
  91. void schedule_timer(timer_queue<Time_Traits>& queue,
  92. const typename Time_Traits::time_type& time,
  93. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
  94. // Cancel the timer operations associated with the given token. Returns the
  95. // number of operations that have been posted or dispatched.
  96. template <typename Time_Traits>
  97. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  98. typename timer_queue<Time_Traits>::per_timer_data& timer);
  99. // Run select once until interrupted or events are ready to be dispatched.
  100. ASIO_DECL void run(bool block, op_queue<operation>& ops);
  101. // Interrupt the select loop.
  102. ASIO_DECL void interrupt();
  103. private:
  104. #if defined(ASIO_HAS_IOCP)
  105. // Run the select loop in the thread.
  106. ASIO_DECL void run_thread();
  107. // Entry point for the select loop thread.
  108. ASIO_DECL static void call_run_thread(select_reactor* reactor);
  109. #endif // defined(ASIO_HAS_IOCP)
  110. // Helper function to add a new timer queue.
  111. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  112. // Helper function to remove a timer queue.
  113. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  114. // Get the timeout value for the select call.
  115. ASIO_DECL timeval* get_timeout(timeval& tv);
  116. // Cancel all operations associated with the given descriptor. This function
  117. // does not acquire the select_reactor's mutex.
  118. ASIO_DECL void cancel_ops_unlocked(socket_type descriptor,
  119. const asio::error_code& ec);
  120. // The io_service implementation used to post completions.
  121. io_service_impl& io_service_;
  122. // Mutex to protect access to internal data.
  123. asio::detail::mutex mutex_;
  124. // The interrupter is used to break a blocking select call.
  125. select_interrupter interrupter_;
  126. // The queues of read, write and except operations.
  127. reactor_op_queue<socket_type> op_queue_[max_ops];
  128. // The timer queues.
  129. timer_queue_set timer_queues_;
  130. #if defined(ASIO_HAS_IOCP)
  131. // Does the reactor loop thread need to stop.
  132. bool stop_thread_;
  133. // The thread that is running the reactor loop.
  134. asio::detail::thread* thread_;
  135. #endif // defined(ASIO_HAS_IOCP)
  136. // Whether the service has been shut down.
  137. bool shutdown_;
  138. };
  139. } // namespace detail
  140. } // namespace asio
  141. #include "asio/detail/pop_options.hpp"
  142. #include "asio/detail/impl/select_reactor.hpp"
  143. #if defined(ASIO_HEADER_ONLY)
  144. # include "asio/detail/impl/select_reactor.ipp"
  145. #endif // defined(ASIO_HEADER_ONLY)
  146. #endif // defined(ASIO_HAS_IOCP)
  147. // || (!defined(ASIO_HAS_DEV_POLL)
  148. // && !defined(ASIO_HAS_EPOLL)
  149. // && !defined(ASIO_HAS_KQUEUE))
  150. #endif // ASIO_DETAIL_SELECT_REACTOR_HPP