1 // 2 // detail/select_reactor.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2015 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 11 #ifndef BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP 12 #define BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 20 #if defined(BOOST_ASIO_HAS_IOCP) \ 21 || (!defined(BOOST_ASIO_HAS_DEV_POLL) \ 22 && !defined(BOOST_ASIO_HAS_EPOLL) \ 23 && !defined(BOOST_ASIO_HAS_KQUEUE) \ 24 && !defined(BOOST_ASIO_WINDOWS_RUNTIME)) 25 26 #include <cstddef> 27 #include <boost/asio/detail/fd_set_adapter.hpp> 28 #include <boost/asio/detail/limits.hpp> 29 #include <boost/asio/detail/mutex.hpp> 30 #include <boost/asio/detail/op_queue.hpp> 31 #include <boost/asio/detail/reactor_op.hpp> 32 #include <boost/asio/detail/reactor_op_queue.hpp> 33 #include <boost/asio/detail/select_interrupter.hpp> 34 #include <boost/asio/detail/socket_types.hpp> 35 #include <boost/asio/detail/timer_queue_base.hpp> 36 #include <boost/asio/detail/timer_queue_set.hpp> 37 #include <boost/asio/detail/wait_op.hpp> 38 #include <boost/asio/io_service.hpp> 39 40 #if defined(BOOST_ASIO_HAS_IOCP) 41 # include <boost/asio/detail/thread.hpp> 42 #endif // defined(BOOST_ASIO_HAS_IOCP) 43 44 #include <boost/asio/detail/push_options.hpp> 45 46 namespace boost { 47 namespace asio { 48 namespace detail { 49 50 class select_reactor 51 : public boost::asio::detail::service_base<select_reactor> 52 { 53 public: 54 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) 55 enum op_types { read_op = 0, write_op = 1, except_op = 2, 56 max_select_ops = 3, connect_op = 3, max_ops = 4 }; 57 #else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) 58 enum op_types { read_op = 0, write_op = 1, except_op = 2, 59 max_select_ops = 3, connect_op = 1, max_ops = 3 }; 60 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__) 61 62 // Per-descriptor data. 63 struct per_descriptor_data 64 { 65 }; 66 67 // Constructor. 68 BOOST_ASIO_DECL select_reactor(boost::asio::io_service& io_service); 69 70 // Destructor. 71 BOOST_ASIO_DECL ~select_reactor(); 72 73 // Destroy all user-defined handler objects owned by the service. 74 BOOST_ASIO_DECL void shutdown_service(); 75 76 // Recreate internal descriptors following a fork. 77 BOOST_ASIO_DECL void fork_service( 78 boost::asio::io_service::fork_event fork_ev); 79 80 // Initialise the task, but only if the reactor is not in its own thread. 81 BOOST_ASIO_DECL void init_task(); 82 83 // Register a socket with the reactor. Returns 0 on success, system error 84 // code on failure. 85 BOOST_ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&); 86 87 // Register a descriptor with an associated single operation. Returns 0 on 88 // success, system error code on failure. 89 BOOST_ASIO_DECL int register_internal_descriptor( 90 int op_type, socket_type descriptor, 91 per_descriptor_data& descriptor_data, reactor_op* op); 92 93 // Post a reactor operation for immediate completion. post_immediate_completion(reactor_op * op,bool is_continuation)94 void post_immediate_completion(reactor_op* op, bool is_continuation) 95 { 96 io_service_.post_immediate_completion(op, is_continuation); 97 } 98 99 // Start a new operation. The reactor operation will be performed when the 100 // given descriptor is flagged as ready, or an error has occurred. 101 BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor, 102 per_descriptor_data&, reactor_op* op, bool is_continuation, bool); 103 104 // Cancel all operations associated with the given descriptor. The 105 // handlers associated with the descriptor will be invoked with the 106 // operation_aborted error. 107 BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&); 108 109 // Cancel any operations that are running against the descriptor and remove 110 // its registration from the reactor. 111 BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor, 112 per_descriptor_data&, bool closing); 113 114 // Remote the descriptor's registration from the reactor. 115 BOOST_ASIO_DECL void deregister_internal_descriptor( 116 socket_type descriptor, per_descriptor_data& descriptor_data); 117 118 // Move descriptor registration from one descriptor_data object to another. 119 BOOST_ASIO_DECL void move_descriptor(socket_type descriptor, 120 per_descriptor_data& target_descriptor_data, 121 per_descriptor_data& source_descriptor_data); 122 123 // Add a new timer queue to the reactor. 124 template <typename Time_Traits> 125 void add_timer_queue(timer_queue<Time_Traits>& queue); 126 127 // Remove a timer queue from the reactor. 128 template <typename Time_Traits> 129 void remove_timer_queue(timer_queue<Time_Traits>& queue); 130 131 // Schedule a new operation in the given timer queue to expire at the 132 // specified absolute time. 133 template <typename Time_Traits> 134 void schedule_timer(timer_queue<Time_Traits>& queue, 135 const typename Time_Traits::time_type& time, 136 typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op); 137 138 // Cancel the timer operations associated with the given token. Returns the 139 // number of operations that have been posted or dispatched. 140 template <typename Time_Traits> 141 std::size_t cancel_timer(timer_queue<Time_Traits>& queue, 142 typename timer_queue<Time_Traits>::per_timer_data& timer, 143 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()); 144 145 // Run select once until interrupted or events are ready to be dispatched. 146 BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops); 147 148 // Interrupt the select loop. 149 BOOST_ASIO_DECL void interrupt(); 150 151 private: 152 #if defined(BOOST_ASIO_HAS_IOCP) 153 // Run the select loop in the thread. 154 BOOST_ASIO_DECL void run_thread(); 155 156 // Entry point for the select loop thread. 157 BOOST_ASIO_DECL static void call_run_thread(select_reactor* reactor); 158 #endif // defined(BOOST_ASIO_HAS_IOCP) 159 160 // Helper function to add a new timer queue. 161 BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); 162 163 // Helper function to remove a timer queue. 164 BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); 165 166 // Get the timeout value for the select call. 167 BOOST_ASIO_DECL timeval* get_timeout(timeval& tv); 168 169 // Cancel all operations associated with the given descriptor. This function 170 // does not acquire the select_reactor's mutex. 171 BOOST_ASIO_DECL void cancel_ops_unlocked(socket_type descriptor, 172 const boost::system::error_code& ec); 173 174 // The io_service implementation used to post completions. 175 io_service_impl& io_service_; 176 177 // Mutex to protect access to internal data. 178 boost::asio::detail::mutex mutex_; 179 180 // The interrupter is used to break a blocking select call. 181 select_interrupter interrupter_; 182 183 // The queues of read, write and except operations. 184 reactor_op_queue<socket_type> op_queue_[max_ops]; 185 186 // The file descriptor sets to be passed to the select system call. 187 fd_set_adapter fd_sets_[max_select_ops]; 188 189 // The timer queues. 190 timer_queue_set timer_queues_; 191 192 #if defined(BOOST_ASIO_HAS_IOCP) 193 // Does the reactor loop thread need to stop. 194 bool stop_thread_; 195 196 // The thread that is running the reactor loop. 197 boost::asio::detail::thread* thread_; 198 #endif // defined(BOOST_ASIO_HAS_IOCP) 199 200 // Whether the service has been shut down. 201 bool shutdown_; 202 }; 203 204 } // namespace detail 205 } // namespace asio 206 } // namespace boost 207 208 #include <boost/asio/detail/pop_options.hpp> 209 210 #include <boost/asio/detail/impl/select_reactor.hpp> 211 #if defined(BOOST_ASIO_HEADER_ONLY) 212 # include <boost/asio/detail/impl/select_reactor.ipp> 213 #endif // defined(BOOST_ASIO_HEADER_ONLY) 214 215 #endif // defined(BOOST_ASIO_HAS_IOCP) 216 // || (!defined(BOOST_ASIO_HAS_DEV_POLL) 217 // && !defined(BOOST_ASIO_HAS_EPOLL) 218 // && !defined(BOOST_ASIO_HAS_KQUEUE) 219 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)) 220 221 #endif // BOOST_ASIO_DETAIL_SELECT_REACTOR_HPP 222