stream_descriptor_service.hpp 8.6 KB

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