stream_descriptor_service.hpp 7.6 KB

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