stream_descriptor_service.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // posix/stream_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_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
  11. #define BOOST_ASIO_POSIX_STREAM_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_HAS_POSIX_STREAM_DESCRIPTOR) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/io_service.hpp>
  22. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace posix {
  27. /// Default service implementation for a stream descriptor.
  28. class stream_descriptor_service
  29. #if defined(GENERATING_DOCUMENTATION)
  30. : public boost::asio::io_service::service
  31. #else
  32. : public boost::asio::detail::service_base<stream_descriptor_service>
  33. #endif
  34. {
  35. public:
  36. #if defined(GENERATING_DOCUMENTATION)
  37. /// The unique service identifier.
  38. static boost::asio::io_service::id id;
  39. #endif
  40. private:
  41. // The type of the platform-specific implementation.
  42. typedef detail::reactive_descriptor_service service_impl_type;
  43. public:
  44. /// The type of a stream descriptor implementation.
  45. #if defined(GENERATING_DOCUMENTATION)
  46. typedef implementation_defined implementation_type;
  47. #else
  48. typedef service_impl_type::implementation_type implementation_type;
  49. #endif
  50. /// (Deprecated: Use native_handle_type.) The native descriptor type.
  51. #if defined(GENERATING_DOCUMENTATION)
  52. typedef implementation_defined native_type;
  53. #else
  54. typedef service_impl_type::native_handle_type native_type;
  55. #endif
  56. /// The native descriptor type.
  57. #if defined(GENERATING_DOCUMENTATION)
  58. typedef implementation_defined native_handle_type;
  59. #else
  60. typedef service_impl_type::native_handle_type native_handle_type;
  61. #endif
  62. /// Construct a new stream descriptor service for the specified io_service.
  63. explicit stream_descriptor_service(boost::asio::io_service& io_service)
  64. : boost::asio::detail::service_base<stream_descriptor_service>(io_service),
  65. service_impl_(io_service)
  66. {
  67. }
  68. /// Construct a new stream descriptor implementation.
  69. void construct(implementation_type& impl)
  70. {
  71. service_impl_.construct(impl);
  72. }
  73. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  74. /// Move-construct a new stream descriptor implementation.
  75. void move_construct(implementation_type& impl,
  76. implementation_type& other_impl)
  77. {
  78. service_impl_.move_construct(impl, other_impl);
  79. }
  80. /// Move-assign from another stream descriptor implementation.
  81. void move_assign(implementation_type& impl,
  82. stream_descriptor_service& other_service,
  83. implementation_type& other_impl)
  84. {
  85. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  86. }
  87. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  88. /// Destroy a stream descriptor implementation.
  89. void destroy(implementation_type& impl)
  90. {
  91. service_impl_.destroy(impl);
  92. }
  93. /// Assign an existing native descriptor to a stream descriptor.
  94. boost::system::error_code assign(implementation_type& impl,
  95. const native_handle_type& native_descriptor,
  96. boost::system::error_code& ec)
  97. {
  98. return service_impl_.assign(impl, native_descriptor, ec);
  99. }
  100. /// Determine whether the descriptor is open.
  101. bool is_open(const implementation_type& impl) const
  102. {
  103. return service_impl_.is_open(impl);
  104. }
  105. /// Close a stream descriptor implementation.
  106. boost::system::error_code close(implementation_type& impl,
  107. boost::system::error_code& ec)
  108. {
  109. return service_impl_.close(impl, ec);
  110. }
  111. /// (Deprecated: Use native_handle().) Get the native descriptor
  112. /// implementation.
  113. native_type native(implementation_type& impl)
  114. {
  115. return service_impl_.native_handle(impl);
  116. }
  117. /// Get the native descriptor implementation.
  118. native_handle_type native_handle(implementation_type& impl)
  119. {
  120. return service_impl_.native_handle(impl);
  121. }
  122. /// Release ownership of the native descriptor implementation.
  123. native_handle_type release(implementation_type& impl)
  124. {
  125. return service_impl_.release(impl);
  126. }
  127. /// Cancel all asynchronous operations associated with the descriptor.
  128. boost::system::error_code cancel(implementation_type& impl,
  129. boost::system::error_code& ec)
  130. {
  131. return service_impl_.cancel(impl, ec);
  132. }
  133. /// Perform an IO control command on the descriptor.
  134. template <typename IoControlCommand>
  135. boost::system::error_code io_control(implementation_type& impl,
  136. IoControlCommand& command, boost::system::error_code& ec)
  137. {
  138. return service_impl_.io_control(impl, command, ec);
  139. }
  140. /// Gets the non-blocking mode of the descriptor.
  141. bool non_blocking(const implementation_type& impl) const
  142. {
  143. return service_impl_.non_blocking(impl);
  144. }
  145. /// Sets the non-blocking mode of the descriptor.
  146. boost::system::error_code non_blocking(implementation_type& impl,
  147. bool mode, boost::system::error_code& ec)
  148. {
  149. return service_impl_.non_blocking(impl, mode, ec);
  150. }
  151. /// Gets the non-blocking mode of the native descriptor implementation.
  152. bool native_non_blocking(const implementation_type& impl) const
  153. {
  154. return service_impl_.native_non_blocking(impl);
  155. }
  156. /// Sets the non-blocking mode of the native descriptor implementation.
  157. boost::system::error_code native_non_blocking(implementation_type& impl,
  158. bool mode, boost::system::error_code& ec)
  159. {
  160. return service_impl_.native_non_blocking(impl, mode, ec);
  161. }
  162. /// Write the given data to the stream.
  163. template <typename ConstBufferSequence>
  164. std::size_t write_some(implementation_type& impl,
  165. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  166. {
  167. return service_impl_.write_some(impl, buffers, ec);
  168. }
  169. /// Start an asynchronous write.
  170. template <typename ConstBufferSequence, typename WriteHandler>
  171. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  172. void (boost::system::error_code, std::size_t))
  173. async_write_some(implementation_type& impl,
  174. const ConstBufferSequence& buffers,
  175. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  176. {
  177. boost::asio::detail::async_result_init<
  178. WriteHandler, void (boost::system::error_code, std::size_t)> init(
  179. BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  180. service_impl_.async_write_some(impl, buffers, init.handler);
  181. return init.result.get();
  182. }
  183. /// Read some data from the stream.
  184. template <typename MutableBufferSequence>
  185. std::size_t read_some(implementation_type& impl,
  186. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  187. {
  188. return service_impl_.read_some(impl, buffers, ec);
  189. }
  190. /// Start an asynchronous read.
  191. template <typename MutableBufferSequence, typename ReadHandler>
  192. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  193. void (boost::system::error_code, std::size_t))
  194. async_read_some(implementation_type& impl,
  195. const MutableBufferSequence& buffers,
  196. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  197. {
  198. boost::asio::detail::async_result_init<
  199. ReadHandler, void (boost::system::error_code, std::size_t)> init(
  200. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  201. service_impl_.async_read_some(impl, buffers, init.handler);
  202. return init.result.get();
  203. }
  204. private:
  205. // Destroy all user-defined handler objects owned by the service.
  206. void shutdown_service()
  207. {
  208. service_impl_.shutdown_service();
  209. }
  210. // The platform-specific implementation.
  211. service_impl_type service_impl_;
  212. };
  213. } // namespace posix
  214. } // namespace asio
  215. } // namespace boost
  216. #include <boost/asio/detail/pop_options.hpp>
  217. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  218. // || defined(GENERATING_DOCUMENTATION)
  219. #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP