2 // windows/random_access_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WINDOWS_RANDOM_ACCESS_HANDLE_SERVICE_HPP
12 #define BOOST_ASIO_WINDOWS_RANDOM_ACCESS_HANDLE_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
23 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/async_result.hpp>
27 #include <boost/asio/detail/cstdint.hpp>
28 #include <boost/asio/detail/win_iocp_handle_service.hpp>
29 #include <boost/asio/error.hpp>
30 #include <boost/asio/io_context.hpp>
32 #include <boost/asio/detail/push_options.hpp>
38 /// Default service implementation for a random-access handle.
39 class random_access_handle_service
40 #if defined(GENERATING_DOCUMENTATION)
41 : public boost::asio::io_context::service
43 : public boost::asio::detail::service_base<random_access_handle_service>
47 #if defined(GENERATING_DOCUMENTATION)
48 /// The unique service identifier.
49 static boost::asio::io_context::id id;
53 // The type of the platform-specific implementation.
54 typedef detail::win_iocp_handle_service service_impl_type;
57 /// The type of a random-access handle implementation.
58 #if defined(GENERATING_DOCUMENTATION)
59 typedef implementation_defined implementation_type;
61 typedef service_impl_type::implementation_type implementation_type;
64 /// The native handle type.
65 #if defined(GENERATING_DOCUMENTATION)
66 typedef implementation_defined native_handle_type;
68 typedef service_impl_type::native_handle_type native_handle_type;
71 /// Construct a new random-access handle service for the specified io_context.
72 explicit random_access_handle_service(boost::asio::io_context& io_context)
73 : boost::asio::detail::service_base<
74 random_access_handle_service>(io_context),
75 service_impl_(io_context)
79 /// Construct a new random-access handle implementation.
80 void construct(implementation_type& impl)
82 service_impl_.construct(impl);
85 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
86 /// Move-construct a new random-access handle implementation.
87 void move_construct(implementation_type& impl,
88 implementation_type& other_impl)
90 service_impl_.move_construct(impl, other_impl);
93 /// Move-assign from another random-access handle implementation.
94 void move_assign(implementation_type& impl,
95 random_access_handle_service& other_service,
96 implementation_type& other_impl)
98 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
100 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
102 /// Destroy a random-access handle implementation.
103 void destroy(implementation_type& impl)
105 service_impl_.destroy(impl);
108 /// Assign an existing native handle to a random-access handle.
109 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
110 const native_handle_type& handle, boost::system::error_code& ec)
112 service_impl_.assign(impl, handle, ec);
113 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
116 /// Determine whether the handle is open.
117 bool is_open(const implementation_type& impl) const
119 return service_impl_.is_open(impl);
122 /// Close a random-access handle implementation.
123 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
124 boost::system::error_code& ec)
126 service_impl_.close(impl, ec);
127 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
130 /// Get the native handle implementation.
131 native_handle_type native_handle(implementation_type& impl)
133 return service_impl_.native_handle(impl);
136 /// Cancel all asynchronous operations associated with the handle.
137 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
138 boost::system::error_code& ec)
140 service_impl_.cancel(impl, ec);
141 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
144 /// Write the given data at the specified offset.
145 template <typename ConstBufferSequence>
146 std::size_t write_some_at(implementation_type& impl, uint64_t offset,
147 const ConstBufferSequence& buffers, boost::system::error_code& ec)
149 return service_impl_.write_some_at(impl, offset, buffers, ec);
152 /// Start an asynchronous write at the specified offset.
153 template <typename ConstBufferSequence, typename WriteHandler>
154 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
155 void (boost::system::error_code, std::size_t))
156 async_write_some_at(implementation_type& impl,
157 uint64_t offset, const ConstBufferSequence& buffers,
158 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
160 boost::asio::async_completion<WriteHandler,
161 void (boost::system::error_code, std::size_t)> init(handler);
163 service_impl_.async_write_some_at(impl,
164 offset, buffers, init.completion_handler);
166 return init.result.get();
169 /// Read some data from the specified offset.
170 template <typename MutableBufferSequence>
171 std::size_t read_some_at(implementation_type& impl, uint64_t offset,
172 const MutableBufferSequence& buffers, boost::system::error_code& ec)
174 return service_impl_.read_some_at(impl, offset, buffers, ec);
177 /// Start an asynchronous read at the specified offset.
178 template <typename MutableBufferSequence, typename ReadHandler>
179 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
180 void (boost::system::error_code, std::size_t))
181 async_read_some_at(implementation_type& impl,
182 uint64_t offset, const MutableBufferSequence& buffers,
183 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
185 boost::asio::async_completion<ReadHandler,
186 void (boost::system::error_code, std::size_t)> init(handler);
188 service_impl_.async_read_some_at(impl,
189 offset, buffers, init.completion_handler);
191 return init.result.get();
195 // Destroy all user-defined handler objects owned by the service.
198 service_impl_.shutdown();
201 // The platform-specific implementation.
202 service_impl_type service_impl_;
205 } // namespace windows
209 #include <boost/asio/detail/pop_options.hpp>
211 #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
212 // || defined(GENERATING_DOCUMENTATION)
214 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
216 #endif // BOOST_ASIO_WINDOWS_RANDOM_ACCESS_HANDLE_SERVICE_HPP