stream_handle_service.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // windows/stream_handle_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_WINDOWS_STREAM_HANDLE_SERVICE_HPP
  11. #define ASIO_WINDOWS_STREAM_HANDLE_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_WINDOWS_STREAM_HANDLE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include "asio/async_result.hpp"
  20. #include "asio/detail/win_iocp_handle_service.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/io_service.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace windows {
  26. /// Default service implementation for a stream handle.
  27. class stream_handle_service
  28. #if defined(GENERATING_DOCUMENTATION)
  29. : public asio::io_service::service
  30. #else
  31. : public asio::detail::service_base<stream_handle_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::win_iocp_handle_service service_impl_type;
  42. public:
  43. /// The type of a stream handle 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 handle 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 handle 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 handle service for the specified io_service.
  62. explicit stream_handle_service(asio::io_service& io_service)
  63. : asio::detail::service_base<stream_handle_service>(io_service),
  64. service_impl_(io_service)
  65. {
  66. }
  67. /// Construct a new stream handle 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 handle 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 handle implementation.
  80. void move_assign(implementation_type& impl,
  81. stream_handle_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 handle implementation.
  88. void destroy(implementation_type& impl)
  89. {
  90. service_impl_.destroy(impl);
  91. }
  92. /// Assign an existing native handle to a stream handle.
  93. asio::error_code assign(implementation_type& impl,
  94. const native_handle_type& handle, asio::error_code& ec)
  95. {
  96. return service_impl_.assign(impl, handle, ec);
  97. }
  98. /// Determine whether the handle is open.
  99. bool is_open(const implementation_type& impl) const
  100. {
  101. return service_impl_.is_open(impl);
  102. }
  103. /// Close a stream handle implementation.
  104. asio::error_code close(implementation_type& impl,
  105. asio::error_code& ec)
  106. {
  107. return service_impl_.close(impl, ec);
  108. }
  109. /// (Deprecated: Use native_handle().) Get the native handle implementation.
  110. native_type native(implementation_type& impl)
  111. {
  112. return service_impl_.native_handle(impl);
  113. }
  114. /// Get the native handle implementation.
  115. native_handle_type native_handle(implementation_type& impl)
  116. {
  117. return service_impl_.native_handle(impl);
  118. }
  119. /// Cancel all asynchronous operations associated with the handle.
  120. asio::error_code cancel(implementation_type& impl,
  121. asio::error_code& ec)
  122. {
  123. return service_impl_.cancel(impl, ec);
  124. }
  125. /// Write the given data to the stream.
  126. template <typename ConstBufferSequence>
  127. std::size_t write_some(implementation_type& impl,
  128. const ConstBufferSequence& buffers, asio::error_code& ec)
  129. {
  130. return service_impl_.write_some(impl, buffers, ec);
  131. }
  132. /// Start an asynchronous write.
  133. template <typename ConstBufferSequence, typename WriteHandler>
  134. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  135. void (asio::error_code, std::size_t))
  136. async_write_some(implementation_type& impl,
  137. const ConstBufferSequence& buffers,
  138. ASIO_MOVE_ARG(WriteHandler) handler)
  139. {
  140. asio::detail::async_result_init<
  141. WriteHandler, void (asio::error_code, std::size_t)> init(
  142. ASIO_MOVE_CAST(WriteHandler)(handler));
  143. service_impl_.async_write_some(impl, buffers, init.handler);
  144. return init.result.get();
  145. }
  146. /// Read some data from the stream.
  147. template <typename MutableBufferSequence>
  148. std::size_t read_some(implementation_type& impl,
  149. const MutableBufferSequence& buffers, asio::error_code& ec)
  150. {
  151. return service_impl_.read_some(impl, buffers, ec);
  152. }
  153. /// Start an asynchronous read.
  154. template <typename MutableBufferSequence, typename ReadHandler>
  155. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  156. void (asio::error_code, std::size_t))
  157. async_read_some(implementation_type& impl,
  158. const MutableBufferSequence& buffers,
  159. ASIO_MOVE_ARG(ReadHandler) handler)
  160. {
  161. asio::detail::async_result_init<
  162. ReadHandler, void (asio::error_code, std::size_t)> init(
  163. ASIO_MOVE_CAST(ReadHandler)(handler));
  164. service_impl_.async_read_some(impl, buffers, init.handler);
  165. return init.result.get();
  166. }
  167. private:
  168. // Destroy all user-defined handler objects owned by the service.
  169. void shutdown_service()
  170. {
  171. service_impl_.shutdown_service();
  172. }
  173. // The platform-specific implementation.
  174. service_impl_type service_impl_;
  175. };
  176. } // namespace windows
  177. } // namespace asio
  178. #include "asio/detail/pop_options.hpp"
  179. #endif // defined(ASIO_HAS_WINDOWS_STREAM_HANDLE)
  180. // || defined(GENERATING_DOCUMENTATION)
  181. #endif // ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP