stream_handle_service.hpp 6.6 KB

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