1 | // |
2 | // windows/stream_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 | |
11 | #ifndef BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP |
12 | #define BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | |
20 | #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \ |
21 | || defined(GENERATING_DOCUMENTATION) |
22 | |
23 | #include <cstddef> |
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> |
28 | |
29 | #include <boost/asio/detail/push_options.hpp> |
30 | |
31 | namespace boost { |
32 | namespace asio { |
33 | namespace windows { |
34 | |
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 |
39 | #else |
40 | : public boost::asio::detail::service_base<stream_handle_service> |
41 | #endif |
42 | { |
43 | public: |
44 | #if defined(GENERATING_DOCUMENTATION) |
45 | /// The unique service identifier. |
46 | static boost::asio::io_service::id id; |
47 | #endif |
48 | |
49 | private: |
50 | // The type of the platform-specific implementation. |
51 | typedef detail::win_iocp_handle_service service_impl_type; |
52 | |
53 | public: |
54 | /// The type of a stream handle implementation. |
55 | #if defined(GENERATING_DOCUMENTATION) |
56 | typedef implementation_defined implementation_type; |
57 | #else |
58 | typedef service_impl_type::implementation_type implementation_type; |
59 | #endif |
60 | |
61 | /// (Deprecated: Use native_handle_type.) The native handle type. |
62 | #if defined(GENERATING_DOCUMENTATION) |
63 | typedef implementation_defined native_type; |
64 | #else |
65 | typedef service_impl_type::native_handle_type native_type; |
66 | #endif |
67 | |
68 | /// The native handle type. |
69 | #if defined(GENERATING_DOCUMENTATION) |
70 | typedef implementation_defined native_handle_type; |
71 | #else |
72 | typedef service_impl_type::native_handle_type native_handle_type; |
73 | #endif |
74 | |
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) |
79 | { |
80 | } |
81 | |
82 | /// Construct a new stream handle implementation. |
83 | void construct(implementation_type& impl) |
84 | { |
85 | service_impl_.construct(impl); |
86 | } |
87 | |
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) |
92 | { |
93 | service_impl_.move_construct(impl, other_impl); |
94 | } |
95 | |
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) |
100 | { |
101 | service_impl_.move_assign(impl, other_service.service_impl_, other_impl); |
102 | } |
103 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) |
104 | |
105 | /// Destroy a stream handle implementation. |
106 | void destroy(implementation_type& impl) |
107 | { |
108 | service_impl_.destroy(impl); |
109 | } |
110 | |
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) |
114 | { |
115 | return service_impl_.assign(impl, handle, ec); |
116 | } |
117 | |
118 | /// Determine whether the handle is open. |
119 | bool is_open(const implementation_type& impl) const |
120 | { |
121 | return service_impl_.is_open(impl); |
122 | } |
123 | |
124 | /// Close a stream handle implementation. |
125 | boost::system::error_code close(implementation_type& impl, |
126 | boost::system::error_code& ec) |
127 | { |
128 | return service_impl_.close(impl, ec); |
129 | } |
130 | |
131 | /// (Deprecated: Use native_handle().) Get the native handle implementation. |
132 | native_type native(implementation_type& impl) |
133 | { |
134 | return service_impl_.native_handle(impl); |
135 | } |
136 | |
137 | /// Get the native handle implementation. |
138 | native_handle_type native_handle(implementation_type& impl) |
139 | { |
140 | return service_impl_.native_handle(impl); |
141 | } |
142 | |
143 | /// Cancel all asynchronous operations associated with the handle. |
144 | boost::system::error_code cancel(implementation_type& impl, |
145 | boost::system::error_code& ec) |
146 | { |
147 | return service_impl_.cancel(impl, ec); |
148 | } |
149 | |
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) |
154 | { |
155 | return service_impl_.write_some(impl, buffers, ec); |
156 | } |
157 | |
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) |
165 | { |
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)); |
169 | |
170 | service_impl_.async_write_some(impl, buffers, init.handler); |
171 | |
172 | return init.result.get(); |
173 | } |
174 | |
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) |
179 | { |
180 | return service_impl_.read_some(impl, buffers, ec); |
181 | } |
182 | |
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) |
190 | { |
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)); |
194 | |
195 | service_impl_.async_read_some(impl, buffers, init.handler); |
196 | |
197 | return init.result.get(); |
198 | } |
199 | |
200 | private: |
201 | // Destroy all user-defined handler objects owned by the service. |
202 | void shutdown_service() |
203 | { |
204 | service_impl_.shutdown_service(); |
205 | } |
206 | |
207 | // The platform-specific implementation. |
208 | service_impl_type service_impl_; |
209 | }; |
210 | |
211 | } // namespace windows |
212 | } // namespace asio |
213 | } // namespace boost |
214 | |
215 | #include <boost/asio/detail/pop_options.hpp> |
216 | |
217 | #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) |
218 | // || defined(GENERATING_DOCUMENTATION) |
219 | |
220 | #endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_SERVICE_HPP |
221 | |