win_iocp_file_service.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // detail/win_iocp_file_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_DETAIL_WIN_IOCP_FILE_SERVICE_HPP
  11. #define ASIO_DETAIL_WIN_IOCP_FILE_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_IOCP) && defined(ASIO_HAS_FILE)
  17. #include <string>
  18. #include "asio/detail/cstdint.hpp"
  19. #include "asio/detail/win_iocp_handle_service.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/execution_context.hpp"
  22. #include "asio/file_base.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. // Extend win_iocp_handle_service to provide file support.
  27. class win_iocp_file_service :
  28. public execution_context_service_base<win_iocp_file_service>
  29. {
  30. public:
  31. // The native type of a file.
  32. typedef win_iocp_handle_service::native_handle_type native_handle_type;
  33. // The implementation type of the file.
  34. class implementation_type : win_iocp_handle_service::implementation_type
  35. {
  36. private:
  37. // Only this service will have access to the internal values.
  38. friend class win_iocp_file_service;
  39. uint64_t offset_;
  40. bool is_stream_;
  41. };
  42. // Constructor.
  43. ASIO_DECL win_iocp_file_service(execution_context& context);
  44. // Destroy all user-defined handler objects owned by the service.
  45. ASIO_DECL void shutdown();
  46. // Construct a new file implementation.
  47. void construct(implementation_type& impl)
  48. {
  49. handle_service_.construct(impl);
  50. impl.offset_ = 0;
  51. impl.is_stream_ = false;
  52. }
  53. // Move-construct a new file implementation.
  54. void move_construct(implementation_type& impl,
  55. implementation_type& other_impl)
  56. {
  57. handle_service_.move_construct(impl, other_impl);
  58. impl.offset_ = other_impl.offset_;
  59. impl.is_stream_ = other_impl.is_stream_;
  60. other_impl.offset_ = 0;
  61. }
  62. // Move-assign from another file implementation.
  63. void move_assign(implementation_type& impl,
  64. win_iocp_file_service& other_service,
  65. implementation_type& other_impl)
  66. {
  67. handle_service_.move_assign(impl,
  68. other_service.handle_service_, other_impl);
  69. impl.offset_ = other_impl.offset_;
  70. impl.is_stream_ = other_impl.is_stream_;
  71. other_impl.offset_ = 0;
  72. }
  73. // Destroy a file implementation.
  74. void destroy(implementation_type& impl)
  75. {
  76. handle_service_.destroy(impl);
  77. }
  78. // Set whether the implementation is stream-oriented.
  79. void set_is_stream(implementation_type& impl, bool is_stream)
  80. {
  81. impl.is_stream_ = is_stream;
  82. }
  83. // Open the file using the specified path name.
  84. ASIO_DECL asio::error_code open(implementation_type& impl,
  85. const char* path, file_base::flags open_flags,
  86. asio::error_code& ec);
  87. // Assign a native handle to a file implementation.
  88. asio::error_code assign(implementation_type& impl,
  89. const native_handle_type& native_handle,
  90. asio::error_code& ec)
  91. {
  92. return handle_service_.assign(impl, native_handle, ec);
  93. }
  94. // Determine whether the file is open.
  95. bool is_open(const implementation_type& impl) const
  96. {
  97. return handle_service_.is_open(impl);
  98. }
  99. // Destroy a file implementation.
  100. asio::error_code close(implementation_type& impl,
  101. asio::error_code& ec)
  102. {
  103. return handle_service_.close(impl, ec);
  104. }
  105. // Get the native file representation.
  106. native_handle_type native_handle(const implementation_type& impl) const
  107. {
  108. return handle_service_.native_handle(impl);
  109. }
  110. // Release ownership of a file.
  111. native_handle_type release(implementation_type& impl,
  112. asio::error_code& ec)
  113. {
  114. return handle_service_.release(impl, ec);
  115. }
  116. // Cancel all operations associated with the file.
  117. asio::error_code cancel(implementation_type& impl,
  118. asio::error_code& ec)
  119. {
  120. return handle_service_.cancel(impl, ec);
  121. }
  122. // Get the size of the file.
  123. ASIO_DECL uint64_t size(const implementation_type& impl,
  124. asio::error_code& ec) const;
  125. // Alter the size of the file.
  126. ASIO_DECL asio::error_code resize(implementation_type& impl,
  127. uint64_t n, asio::error_code& ec);
  128. // Synchronise the file to disk.
  129. ASIO_DECL asio::error_code sync_all(implementation_type& impl,
  130. asio::error_code& ec);
  131. // Synchronise the file data to disk.
  132. ASIO_DECL asio::error_code sync_data(implementation_type& impl,
  133. asio::error_code& ec);
  134. // Seek to a position in the file.
  135. ASIO_DECL uint64_t seek(implementation_type& impl, int64_t offset,
  136. file_base::seek_basis whence, asio::error_code& ec);
  137. // Write the given data. Returns the number of bytes written.
  138. template <typename ConstBufferSequence>
  139. size_t write_some(implementation_type& impl,
  140. const ConstBufferSequence& buffers, asio::error_code& ec)
  141. {
  142. uint64_t offset = impl.offset_;
  143. impl.offset_ += asio::buffer_size(buffers);
  144. return handle_service_.write_some_at(impl, offset, buffers, ec);
  145. }
  146. // Start an asynchronous write. The data being written must be valid for the
  147. // lifetime of the asynchronous operation.
  148. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  149. void async_write_some(implementation_type& impl,
  150. const ConstBufferSequence& buffers,
  151. Handler& handler, const IoExecutor& io_ex)
  152. {
  153. uint64_t offset = impl.offset_;
  154. impl.offset_ += asio::buffer_size(buffers);
  155. handle_service_.async_write_some_at(impl, offset, buffers, handler, io_ex);
  156. }
  157. // Write the given data at the specified location. Returns the number of
  158. // bytes written.
  159. template <typename ConstBufferSequence>
  160. size_t write_some_at(implementation_type& impl, uint64_t offset,
  161. const ConstBufferSequence& buffers, asio::error_code& ec)
  162. {
  163. return handle_service_.write_some_at(impl, offset, buffers, ec);
  164. }
  165. // Start an asynchronous write at the specified location. The data being
  166. // written must be valid for the lifetime of the asynchronous operation.
  167. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  168. void async_write_some_at(implementation_type& impl,
  169. uint64_t offset, const ConstBufferSequence& buffers,
  170. Handler& handler, const IoExecutor& io_ex)
  171. {
  172. handle_service_.async_write_some_at(impl, offset, buffers, handler, io_ex);
  173. }
  174. // Read some data. Returns the number of bytes read.
  175. template <typename MutableBufferSequence>
  176. size_t read_some(implementation_type& impl,
  177. const MutableBufferSequence& buffers, asio::error_code& ec)
  178. {
  179. uint64_t offset = impl.offset_;
  180. impl.offset_ += asio::buffer_size(buffers);
  181. return handle_service_.read_some_at(impl, offset, buffers, ec);
  182. }
  183. // Start an asynchronous read. The buffer for the data being read must be
  184. // valid for the lifetime of the asynchronous operation.
  185. template <typename MutableBufferSequence,
  186. typename Handler, typename IoExecutor>
  187. void async_read_some(implementation_type& impl,
  188. const MutableBufferSequence& buffers,
  189. Handler& handler, const IoExecutor& io_ex)
  190. {
  191. uint64_t offset = impl.offset_;
  192. impl.offset_ += asio::buffer_size(buffers);
  193. handle_service_.async_read_some_at(impl, offset, buffers, handler, io_ex);
  194. }
  195. // Read some data. Returns the number of bytes read.
  196. template <typename MutableBufferSequence>
  197. size_t read_some_at(implementation_type& impl, uint64_t offset,
  198. const MutableBufferSequence& buffers, asio::error_code& ec)
  199. {
  200. return handle_service_.read_some_at(impl, offset, buffers, ec);
  201. }
  202. // Start an asynchronous read. The buffer for the data being read must be
  203. // valid for the lifetime of the asynchronous operation.
  204. template <typename MutableBufferSequence,
  205. typename Handler, typename IoExecutor>
  206. void async_read_some_at(implementation_type& impl,
  207. uint64_t offset, const MutableBufferSequence& buffers,
  208. Handler& handler, const IoExecutor& io_ex)
  209. {
  210. handle_service_.async_read_some_at(impl, offset, buffers, handler, io_ex);
  211. }
  212. private:
  213. // The implementation used for initiating asynchronous operations.
  214. win_iocp_handle_service handle_service_;
  215. // Emulation of Windows IO_STATUS_BLOCK structure.
  216. struct io_status_block
  217. {
  218. union u
  219. {
  220. LONG Status;
  221. void* Pointer;
  222. };
  223. ULONG_PTR Information;
  224. };
  225. // Emulation of flag passed to NtFlushBuffersFileEx.
  226. enum { flush_flags_file_data_sync_only = 4 };
  227. // The type of a NtFlushBuffersFileEx function pointer.
  228. typedef LONG (NTAPI *nt_flush_buffers_file_ex_fn)(
  229. HANDLE, ULONG, void*, ULONG, io_status_block*);
  230. // The NTFlushBuffersFileEx function pointer.
  231. nt_flush_buffers_file_ex_fn nt_flush_buffers_file_ex_;
  232. };
  233. } // namespace detail
  234. } // namespace asio
  235. #include "asio/detail/pop_options.hpp"
  236. #if defined(ASIO_HEADER_ONLY)
  237. # include "asio/detail/impl/win_iocp_file_service.ipp"
  238. #endif // defined(ASIO_HEADER_ONLY)
  239. #endif // defined(ASIO_HAS_IOCP) && defined(ASIO_HAS_FILE)
  240. #endif // ASIO_DETAIL_WIN_IOCP_FILE_SERVICE_HPP