reactive_descriptor_service.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // detail/reactive_descriptor_service.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_REACTIVE_DESCRIPTOR_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_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. #if !defined(BOOST_ASIO_WINDOWS) \
  17. && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
  18. && !defined(__CYGWIN__)
  19. #include <boost/asio/buffer.hpp>
  20. #include <boost/asio/io_service.hpp>
  21. #include <boost/asio/detail/addressof.hpp>
  22. #include <boost/asio/detail/bind_handler.hpp>
  23. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  24. #include <boost/asio/detail/descriptor_ops.hpp>
  25. #include <boost/asio/detail/descriptor_read_op.hpp>
  26. #include <boost/asio/detail/descriptor_write_op.hpp>
  27. #include <boost/asio/detail/fenced_block.hpp>
  28. #include <boost/asio/detail/noncopyable.hpp>
  29. #include <boost/asio/detail/reactive_null_buffers_op.hpp>
  30. #include <boost/asio/detail/reactor.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail {
  35. class reactive_descriptor_service
  36. {
  37. public:
  38. // The native type of a descriptor.
  39. typedef int native_handle_type;
  40. // The implementation type of the descriptor.
  41. class implementation_type
  42. : private boost::asio::detail::noncopyable
  43. {
  44. public:
  45. // Default constructor.
  46. implementation_type()
  47. : descriptor_(-1),
  48. state_(0)
  49. {
  50. }
  51. private:
  52. // Only this service will have access to the internal values.
  53. friend class reactive_descriptor_service;
  54. // The native descriptor representation.
  55. int descriptor_;
  56. // The current state of the descriptor.
  57. descriptor_ops::state_type state_;
  58. // Per-descriptor data used by the reactor.
  59. reactor::per_descriptor_data reactor_data_;
  60. };
  61. // Constructor.
  62. BOOST_ASIO_DECL reactive_descriptor_service(
  63. boost::asio::io_service& io_service);
  64. // Destroy all user-defined handler objects owned by the service.
  65. BOOST_ASIO_DECL void shutdown_service();
  66. // Construct a new descriptor implementation.
  67. BOOST_ASIO_DECL void construct(implementation_type& impl);
  68. // Move-construct a new descriptor implementation.
  69. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  70. implementation_type& other_impl);
  71. // Move-assign from another descriptor implementation.
  72. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  73. reactive_descriptor_service& other_service,
  74. implementation_type& other_impl);
  75. // Destroy a descriptor implementation.
  76. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  77. // Assign a native descriptor to a descriptor implementation.
  78. BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
  79. const native_handle_type& native_descriptor,
  80. boost::system::error_code& ec);
  81. // Determine whether the descriptor is open.
  82. bool is_open(const implementation_type& impl) const
  83. {
  84. return impl.descriptor_ != -1;
  85. }
  86. // Destroy a descriptor implementation.
  87. BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
  88. boost::system::error_code& ec);
  89. // Get the native descriptor representation.
  90. native_handle_type native_handle(const implementation_type& impl) const
  91. {
  92. return impl.descriptor_;
  93. }
  94. // Release ownership of the native descriptor representation.
  95. BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
  96. // Cancel all operations associated with the descriptor.
  97. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  98. boost::system::error_code& ec);
  99. // Perform an IO control command on the descriptor.
  100. template <typename IO_Control_Command>
  101. boost::system::error_code io_control(implementation_type& impl,
  102. IO_Control_Command& command, boost::system::error_code& ec)
  103. {
  104. descriptor_ops::ioctl(impl.descriptor_, impl.state_,
  105. command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
  106. return ec;
  107. }
  108. // Gets the non-blocking mode of the descriptor.
  109. bool non_blocking(const implementation_type& impl) const
  110. {
  111. return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
  112. }
  113. // Sets the non-blocking mode of the descriptor.
  114. boost::system::error_code non_blocking(implementation_type& impl,
  115. bool mode, boost::system::error_code& ec)
  116. {
  117. descriptor_ops::set_user_non_blocking(
  118. impl.descriptor_, impl.state_, mode, ec);
  119. return ec;
  120. }
  121. // Gets the non-blocking mode of the native descriptor implementation.
  122. bool native_non_blocking(const implementation_type& impl) const
  123. {
  124. return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
  125. }
  126. // Sets the non-blocking mode of the native descriptor implementation.
  127. boost::system::error_code native_non_blocking(implementation_type& impl,
  128. bool mode, boost::system::error_code& ec)
  129. {
  130. descriptor_ops::set_internal_non_blocking(
  131. impl.descriptor_, impl.state_, mode, ec);
  132. return ec;
  133. }
  134. // Write some data to the descriptor.
  135. template <typename ConstBufferSequence>
  136. size_t write_some(implementation_type& impl,
  137. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  138. {
  139. buffer_sequence_adapter<boost::asio::const_buffer,
  140. ConstBufferSequence> bufs(buffers);
  141. return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
  142. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  143. }
  144. // Wait until data can be written without blocking.
  145. size_t write_some(implementation_type& impl,
  146. const null_buffers&, boost::system::error_code& ec)
  147. {
  148. // Wait for descriptor to become ready.
  149. descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
  150. return 0;
  151. }
  152. // Start an asynchronous write. The data being sent must be valid for the
  153. // lifetime of the asynchronous operation.
  154. template <typename ConstBufferSequence, typename Handler>
  155. void async_write_some(implementation_type& impl,
  156. const ConstBufferSequence& buffers, Handler& handler)
  157. {
  158. bool is_continuation =
  159. boost_asio_handler_cont_helpers::is_continuation(handler);
  160. // Allocate and construct an operation to wrap the handler.
  161. typedef descriptor_write_op<ConstBufferSequence, Handler> op;
  162. typename op::ptr p = { boost::asio::detail::addressof(handler),
  163. boost_asio_handler_alloc_helpers::allocate(
  164. sizeof(op), handler), 0 };
  165. p.p = new (p.v) op(impl.descriptor_, buffers, handler);
  166. BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
  167. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  168. buffer_sequence_adapter<boost::asio::const_buffer,
  169. ConstBufferSequence>::all_empty(buffers));
  170. p.v = p.p = 0;
  171. }
  172. // Start an asynchronous wait until data can be written without blocking.
  173. template <typename Handler>
  174. void async_write_some(implementation_type& impl,
  175. const null_buffers&, Handler& handler)
  176. {
  177. bool is_continuation =
  178. boost_asio_handler_cont_helpers::is_continuation(handler);
  179. // Allocate and construct an operation to wrap the handler.
  180. typedef reactive_null_buffers_op<Handler> op;
  181. typename op::ptr p = { boost::asio::detail::addressof(handler),
  182. boost_asio_handler_alloc_helpers::allocate(
  183. sizeof(op), handler), 0 };
  184. p.p = new (p.v) op(handler);
  185. BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
  186. &impl, "async_write_some(null_buffers)"));
  187. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  188. p.v = p.p = 0;
  189. }
  190. // Read some data from the stream. Returns the number of bytes read.
  191. template <typename MutableBufferSequence>
  192. size_t read_some(implementation_type& impl,
  193. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  194. {
  195. buffer_sequence_adapter<boost::asio::mutable_buffer,
  196. MutableBufferSequence> bufs(buffers);
  197. return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
  198. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  199. }
  200. // Wait until data can be read without blocking.
  201. size_t read_some(implementation_type& impl,
  202. const null_buffers&, boost::system::error_code& ec)
  203. {
  204. // Wait for descriptor to become ready.
  205. descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
  206. return 0;
  207. }
  208. // Start an asynchronous read. The buffer for the data being read must be
  209. // valid for the lifetime of the asynchronous operation.
  210. template <typename MutableBufferSequence, typename Handler>
  211. void async_read_some(implementation_type& impl,
  212. const MutableBufferSequence& buffers, Handler& handler)
  213. {
  214. bool is_continuation =
  215. boost_asio_handler_cont_helpers::is_continuation(handler);
  216. // Allocate and construct an operation to wrap the handler.
  217. typedef descriptor_read_op<MutableBufferSequence, Handler> op;
  218. typename op::ptr p = { boost::asio::detail::addressof(handler),
  219. boost_asio_handler_alloc_helpers::allocate(
  220. sizeof(op), handler), 0 };
  221. p.p = new (p.v) op(impl.descriptor_, buffers, handler);
  222. BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
  223. start_op(impl, reactor::read_op, p.p, is_continuation, true,
  224. buffer_sequence_adapter<boost::asio::mutable_buffer,
  225. MutableBufferSequence>::all_empty(buffers));
  226. p.v = p.p = 0;
  227. }
  228. // Wait until data can be read without blocking.
  229. template <typename Handler>
  230. void async_read_some(implementation_type& impl,
  231. const null_buffers&, Handler& handler)
  232. {
  233. bool is_continuation =
  234. boost_asio_handler_cont_helpers::is_continuation(handler);
  235. // Allocate and construct an operation to wrap the handler.
  236. typedef reactive_null_buffers_op<Handler> op;
  237. typename op::ptr p = { boost::asio::detail::addressof(handler),
  238. boost_asio_handler_alloc_helpers::allocate(
  239. sizeof(op), handler), 0 };
  240. p.p = new (p.v) op(handler);
  241. BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
  242. &impl, "async_read_some(null_buffers)"));
  243. start_op(impl, reactor::read_op, p.p, is_continuation, false, false);
  244. p.v = p.p = 0;
  245. }
  246. private:
  247. // Start the asynchronous operation.
  248. BOOST_ASIO_DECL void start_op(implementation_type& impl, int op_type,
  249. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
  250. // The selector that performs event demultiplexing for the service.
  251. reactor& reactor_;
  252. };
  253. } // namespace detail
  254. } // namespace asio
  255. } // namespace boost
  256. #include <boost/asio/detail/pop_options.hpp>
  257. #if defined(BOOST_ASIO_HEADER_ONLY)
  258. # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
  259. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  260. #endif // !defined(BOOST_ASIO_WINDOWS)
  261. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  262. // && !defined(__CYGWIN__)
  263. #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP