stream_handle_service.hpp 6.2 KB

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