2 // windows/stream_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_STREAM_HANDLE_SERVICE_HPP
12 #define BOOST_ASIO_WINDOWS_STREAM_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_STREAM_HANDLE) \
23 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/async_result.hpp>
27 #include <boost/asio/detail/win_iocp_handle_service.hpp>
28 #include <boost/asio/error.hpp>
29 #include <boost/asio/io_context.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 /// Default service implementation for a stream handle.
38 class stream_handle_service
39 #if defined(GENERATING_DOCUMENTATION)
40 : public boost::asio::io_context::service
42 : public boost::asio::detail::service_base<stream_handle_service>
46 #if defined(GENERATING_DOCUMENTATION)
47 /// The unique service identifier.
48 static boost::asio::io_context::id id;
52 // The type of the platform-specific implementation.
53 typedef detail::win_iocp_handle_service service_impl_type;
56 /// The type of a stream handle implementation.
57 #if defined(GENERATING_DOCUMENTATION)
58 typedef implementation_defined implementation_type;
60 typedef service_impl_type::implementation_type implementation_type;
63 /// The native handle type.
64 #if defined(GENERATING_DOCUMENTATION)
65 typedef implementation_defined native_handle_type;
67 typedef service_impl_type::native_handle_type native_handle_type;
70 /// Construct a new stream handle service for the specified io_context.
71 explicit stream_handle_service(boost::asio::io_context& io_context)
72 : boost::asio::detail::service_base<stream_handle_service>(io_context),
73 service_impl_(io_context)
77 /// Construct a new stream handle implementation.
78 void construct(implementation_type& impl)
80 service_impl_.construct(impl);
83 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
84 /// Move-construct a new stream handle implementation.
85 void move_construct(implementation_type& impl,
86 implementation_type& other_impl)
88 service_impl_.move_construct(impl, other_impl);
91 /// Move-assign from another stream handle implementation.
92 void move_assign(implementation_type& impl,
93 stream_handle_service& other_service,
94 implementation_type& other_impl)
96 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
98 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
100 /// Destroy a stream handle implementation.
101 void destroy(implementation_type& impl)
103 service_impl_.destroy(impl);
106 /// Assign an existing native handle to a stream handle.
107 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
108 const native_handle_type& handle, boost::system::error_code& ec)
110 service_impl_.assign(impl, handle, ec);
111 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
114 /// Determine whether the handle is open.
115 bool is_open(const implementation_type& impl) const
117 return service_impl_.is_open(impl);
120 /// Close a stream handle implementation.
121 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
122 boost::system::error_code& ec)
124 service_impl_.close(impl, ec);
125 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
128 /// Get the native handle implementation.
129 native_handle_type native_handle(implementation_type& impl)
131 return service_impl_.native_handle(impl);
134 /// Cancel all asynchronous operations associated with the handle.
135 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
136 boost::system::error_code& ec)
138 service_impl_.cancel(impl, ec);
139 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
142 /// Write the given data to the stream.
143 template <typename ConstBufferSequence>
144 std::size_t write_some(implementation_type& impl,
145 const ConstBufferSequence& buffers, boost::system::error_code& ec)
147 return service_impl_.write_some(impl, buffers, ec);
150 /// Start an asynchronous write.
151 template <typename ConstBufferSequence, typename WriteHandler>
152 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
153 void (boost::system::error_code, std::size_t))
154 async_write_some(implementation_type& impl,
155 const ConstBufferSequence& buffers,
156 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
158 boost::asio::async_completion<WriteHandler,
159 void (boost::system::error_code, std::size_t)> init(handler);
161 service_impl_.async_write_some(impl, buffers, init.completion_handler);
163 return init.result.get();
166 /// Read some data from the stream.
167 template <typename MutableBufferSequence>
168 std::size_t read_some(implementation_type& impl,
169 const MutableBufferSequence& buffers, boost::system::error_code& ec)
171 return service_impl_.read_some(impl, buffers, ec);
174 /// Start an asynchronous read.
175 template <typename MutableBufferSequence, typename ReadHandler>
176 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
177 void (boost::system::error_code, std::size_t))
178 async_read_some(implementation_type& impl,
179 const MutableBufferSequence& buffers,
180 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
182 boost::asio::async_completion<ReadHandler,
183 void (boost::system::error_code, std::size_t)> init(handler);
185 service_impl_.async_read_some(impl, buffers, init.completion_handler);
187 return init.result.get();
191 // Destroy all user-defined handler objects owned by the service.
194 service_impl_.shutdown();
197 // The platform-specific implementation.
198 service_impl_type service_impl_;
201 } // namespace windows
205 #include <boost/asio/detail/pop_options.hpp>
207 #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
208 // || defined(GENERATING_DOCUMENTATION)
210 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
212 #endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP