stream_descriptor_service.hpp 8.2 KB

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