random_access_handle_service.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // windows/random_access_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_RANDOM_ACCESS_HANDLE_SERVICE_HPP
  11. #define BOOST_ASIO_WINDOWS_RANDOM_ACCESS_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_RANDOM_ACCESS_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <cstddef>
  20. #include <boost/asio/async_result.hpp>
  21. #include <boost/asio/detail/cstdint.hpp>
  22. #include <boost/asio/detail/win_iocp_handle_service.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/io_context.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace windows {
  29. /// Default service implementation for a random-access handle.
  30. class random_access_handle_service
  31. #if defined(GENERATING_DOCUMENTATION)
  32. : public boost::asio::io_context::service
  33. #else
  34. : public boost::asio::detail::service_base<random_access_handle_service>
  35. #endif
  36. {
  37. public:
  38. #if defined(GENERATING_DOCUMENTATION)
  39. /// The unique service identifier.
  40. static boost::asio::io_context::id id;
  41. #endif
  42. private:
  43. // The type of the platform-specific implementation.
  44. typedef detail::win_iocp_handle_service service_impl_type;
  45. public:
  46. /// The type of a random-access handle implementation.
  47. #if defined(GENERATING_DOCUMENTATION)
  48. typedef implementation_defined implementation_type;
  49. #else
  50. typedef service_impl_type::implementation_type implementation_type;
  51. #endif
  52. /// The native handle type.
  53. #if defined(GENERATING_DOCUMENTATION)
  54. typedef implementation_defined native_handle_type;
  55. #else
  56. typedef service_impl_type::native_handle_type native_handle_type;
  57. #endif
  58. /// Construct a new random-access handle service for the specified io_context.
  59. explicit random_access_handle_service(boost::asio::io_context& io_context)
  60. : boost::asio::detail::service_base<
  61. random_access_handle_service>(io_context),
  62. service_impl_(io_context)
  63. {
  64. }
  65. /// Construct a new random-access handle implementation.
  66. void construct(implementation_type& impl)
  67. {
  68. service_impl_.construct(impl);
  69. }
  70. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  71. /// Move-construct a new random-access handle implementation.
  72. void move_construct(implementation_type& impl,
  73. implementation_type& other_impl)
  74. {
  75. service_impl_.move_construct(impl, other_impl);
  76. }
  77. /// Move-assign from another random-access handle implementation.
  78. void move_assign(implementation_type& impl,
  79. random_access_handle_service& other_service,
  80. implementation_type& other_impl)
  81. {
  82. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  83. }
  84. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  85. /// Destroy a random-access handle implementation.
  86. void destroy(implementation_type& impl)
  87. {
  88. service_impl_.destroy(impl);
  89. }
  90. /// Assign an existing native handle to a random-access handle.
  91. BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
  92. const native_handle_type& handle, boost::system::error_code& ec)
  93. {
  94. service_impl_.assign(impl, handle, ec);
  95. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  96. }
  97. /// Determine whether the handle is open.
  98. bool is_open(const implementation_type& impl) const
  99. {
  100. return service_impl_.is_open(impl);
  101. }
  102. /// Close a random-access handle implementation.
  103. BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
  104. boost::system::error_code& ec)
  105. {
  106. service_impl_.close(impl, ec);
  107. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  108. }
  109. /// Get the native handle implementation.
  110. native_handle_type native_handle(implementation_type& impl)
  111. {
  112. return service_impl_.native_handle(impl);
  113. }
  114. /// Cancel all asynchronous operations associated with the handle.
  115. BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
  116. boost::system::error_code& ec)
  117. {
  118. service_impl_.cancel(impl, ec);
  119. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  120. }
  121. /// Write the given data at the specified offset.
  122. template <typename ConstBufferSequence>
  123. std::size_t write_some_at(implementation_type& impl, uint64_t offset,
  124. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  125. {
  126. return service_impl_.write_some_at(impl, offset, buffers, ec);
  127. }
  128. /// Start an asynchronous write at the specified offset.
  129. template <typename ConstBufferSequence, typename WriteHandler>
  130. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  131. void (boost::system::error_code, std::size_t))
  132. async_write_some_at(implementation_type& impl,
  133. uint64_t offset, const ConstBufferSequence& buffers,
  134. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  135. {
  136. boost::asio::async_completion<WriteHandler,
  137. void (boost::system::error_code, std::size_t)> init(handler);
  138. service_impl_.async_write_some_at(impl,
  139. offset, buffers, init.completion_handler);
  140. return init.result.get();
  141. }
  142. /// Read some data from the specified offset.
  143. template <typename MutableBufferSequence>
  144. std::size_t read_some_at(implementation_type& impl, uint64_t offset,
  145. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  146. {
  147. return service_impl_.read_some_at(impl, offset, buffers, ec);
  148. }
  149. /// Start an asynchronous read at the specified offset.
  150. template <typename MutableBufferSequence, typename ReadHandler>
  151. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  152. void (boost::system::error_code, std::size_t))
  153. async_read_some_at(implementation_type& impl,
  154. uint64_t offset, const MutableBufferSequence& buffers,
  155. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  156. {
  157. boost::asio::async_completion<ReadHandler,
  158. void (boost::system::error_code, std::size_t)> init(handler);
  159. service_impl_.async_read_some_at(impl,
  160. offset, buffers, init.completion_handler);
  161. return init.result.get();
  162. }
  163. private:
  164. // Destroy all user-defined handler objects owned by the service.
  165. void shutdown()
  166. {
  167. service_impl_.shutdown();
  168. }
  169. // The platform-specific implementation.
  170. service_impl_type service_impl_;
  171. };
  172. } // namespace windows
  173. } // namespace asio
  174. } // namespace boost
  175. #include <boost/asio/detail/pop_options.hpp>
  176. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  177. // || defined(GENERATING_DOCUMENTATION)
  178. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  179. #endif // BOOST_ASIO_WINDOWS_RANDOM_ACCESS_HANDLE_SERVICE_HPP