random_access_handle_service.hpp 6.6 KB

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