reactive_descriptor_service.hpp 10 KB

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