2 // windows/stream_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_HAS_WINDOWS_STREAM_HANDLE) \
21 || defined(GENERATING_DOCUMENTATION)
24 #include <boost/asio/async_result.hpp>
25 #include <boost/asio/detail/win_iocp_handle_service.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/io_service.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 /// Default service implementation for a stream handle.
36 class stream_handle_service
37 #if defined(GENERATING_DOCUMENTATION)
38 : public boost::asio::io_service::service
40 : public boost::asio::detail::service_base<stream_handle_service>
44 #if defined(GENERATING_DOCUMENTATION)
45 /// The unique service identifier.
46 static boost::asio::io_service::id id;
50 // The type of the platform-specific implementation.
51 typedef detail::win_iocp_handle_service service_impl_type;
54 /// The type of a stream handle implementation.
55 #if defined(GENERATING_DOCUMENTATION)
56 typedef implementation_defined implementation_type;
58 typedef service_impl_type::implementation_type implementation_type;
61 /// (Deprecated: Use native_handle_type.) The native handle type.
62 #if defined(GENERATING_DOCUMENTATION)
63 typedef implementation_defined native_type;
65 typedef service_impl_type::native_handle_type native_type;
68 /// The native handle type.
69 #if defined(GENERATING_DOCUMENTATION)
70 typedef implementation_defined native_handle_type;
72 typedef service_impl_type::native_handle_type native_handle_type;
75 /// Construct a new stream handle service for the specified io_service.
76 explicit stream_handle_service(boost::asio::io_service& io_service)
77 : boost::asio::detail::service_base<stream_handle_service>(io_service),
78 service_impl_(io_service)
82 /// Construct a new stream handle implementation.
83 void construct(implementation_type& impl)
85 service_impl_.construct(impl);
88 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
89 /// Move-construct a new stream handle implementation.
90 void move_construct(implementation_type& impl,
91 implementation_type& other_impl)
93 service_impl_.move_construct(impl, other_impl);
96 /// Move-assign from another stream handle implementation.
97 void move_assign(implementation_type& impl,
98 stream_handle_service& other_service,
99 implementation_type& other_impl)
101 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
103 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
105 /// Destroy a stream handle implementation.
106 void destroy(implementation_type& impl)
108 service_impl_.destroy(impl);
111 /// Assign an existing native handle to a stream handle.
112 boost::system::error_code assign(implementation_type& impl,
113 const native_handle_type& handle, boost::system::error_code& ec)
115 return service_impl_.assign(impl, handle, ec);
118 /// Determine whether the handle is open.
119 bool is_open(const implementation_type& impl) const
121 return service_impl_.is_open(impl);
124 /// Close a stream handle implementation.
125 boost::system::error_code close(implementation_type& impl,
126 boost::system::error_code& ec)
128 return service_impl_.close(impl, ec);
131 /// (Deprecated: Use native_handle().) Get the native handle implementation.
132 native_type native(implementation_type& impl)
134 return service_impl_.native_handle(impl);
137 /// Get the native handle implementation.
138 native_handle_type native_handle(implementation_type& impl)
140 return service_impl_.native_handle(impl);
143 /// Cancel all asynchronous operations associated with the handle.
144 boost::system::error_code cancel(implementation_type& impl,
145 boost::system::error_code& ec)
147 return service_impl_.cancel(impl, ec);
150 /// Write the given data to the stream.
151 template <typename ConstBufferSequence>
152 std::size_t write_some(implementation_type& impl,
153 const ConstBufferSequence& buffers, boost::system::error_code& ec)
155 return service_impl_.write_some(impl, buffers, ec);
158 /// Start an asynchronous write.
159 template <typename ConstBufferSequence, typename WriteHandler>
160 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
161 void (boost::system::error_code, std::size_t))
162 async_write_some(implementation_type& impl,
163 const ConstBufferSequence& buffers,
164 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
166 boost::asio::detail::async_result_init<
167 WriteHandler, void (boost::system::error_code, std::size_t)> init(
168 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
170 service_impl_.async_write_some(impl, buffers, init.handler);
172 return init.result.get();
175 /// Read some data from the stream.
176 template <typename MutableBufferSequence>
177 std::size_t read_some(implementation_type& impl,
178 const MutableBufferSequence& buffers, boost::system::error_code& ec)
180 return service_impl_.read_some(impl, buffers, ec);
183 /// Start an asynchronous read.
184 template <typename MutableBufferSequence, typename ReadHandler>
185 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
186 void (boost::system::error_code, std::size_t))
187 async_read_some(implementation_type& impl,
188 const MutableBufferSequence& buffers,
189 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
191 boost::asio::detail::async_result_init<
192 ReadHandler, void (boost::system::error_code, std::size_t)> init(
193 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
195 service_impl_.async_read_some(impl, buffers, init.handler);
197 return init.result.get();
201 // Destroy all user-defined handler objects owned by the service.
202 void shutdown_service()
204 service_impl_.shutdown_service();
207 // The platform-specific implementation.
208 service_impl_type service_impl_;
211 } // namespace windows
215 #include <boost/asio/detail/pop_options.hpp>
217 #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
218 // || defined(GENERATING_DOCUMENTATION)
220 #endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP