reactive_descriptor_service.hpp 8.0 KB

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