]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // posix/stream_descriptor_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP | |
12 | #define BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_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 | ||
b32b8144 FG |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
21 | ||
7c673cae FG |
22 | #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \ |
23 | || defined(GENERATING_DOCUMENTATION) | |
24 | ||
25 | #include <cstddef> | |
26 | #include <boost/asio/async_result.hpp> | |
27 | #include <boost/asio/error.hpp> | |
b32b8144 | 28 | #include <boost/asio/io_context.hpp> |
7c673cae FG |
29 | #include <boost/asio/detail/reactive_descriptor_service.hpp> |
30 | ||
31 | #include <boost/asio/detail/push_options.hpp> | |
32 | ||
33 | namespace boost { | |
34 | namespace asio { | |
35 | namespace posix { | |
36 | ||
37 | /// Default service implementation for a stream descriptor. | |
38 | class stream_descriptor_service | |
39 | #if defined(GENERATING_DOCUMENTATION) | |
b32b8144 | 40 | : public boost::asio::io_context::service |
7c673cae FG |
41 | #else |
42 | : public boost::asio::detail::service_base<stream_descriptor_service> | |
43 | #endif | |
44 | { | |
45 | public: | |
46 | #if defined(GENERATING_DOCUMENTATION) | |
47 | /// The unique service identifier. | |
b32b8144 | 48 | static boost::asio::io_context::id id; |
7c673cae FG |
49 | #endif |
50 | ||
51 | private: | |
52 | // The type of the platform-specific implementation. | |
53 | typedef detail::reactive_descriptor_service service_impl_type; | |
54 | ||
55 | public: | |
56 | /// The type of a stream descriptor implementation. | |
57 | #if defined(GENERATING_DOCUMENTATION) | |
58 | typedef implementation_defined implementation_type; | |
59 | #else | |
60 | typedef service_impl_type::implementation_type implementation_type; | |
61 | #endif | |
62 | ||
7c673cae FG |
63 | /// The native descriptor type. |
64 | #if defined(GENERATING_DOCUMENTATION) | |
65 | typedef implementation_defined native_handle_type; | |
66 | #else | |
67 | typedef service_impl_type::native_handle_type native_handle_type; | |
68 | #endif | |
69 | ||
b32b8144 FG |
70 | /// Construct a new stream descriptor service for the specified io_context. |
71 | explicit stream_descriptor_service(boost::asio::io_context& io_context) | |
72 | : boost::asio::detail::service_base<stream_descriptor_service>(io_context), | |
73 | service_impl_(io_context) | |
7c673cae FG |
74 | { |
75 | } | |
76 | ||
77 | /// Construct a new stream descriptor implementation. | |
78 | void construct(implementation_type& impl) | |
79 | { | |
80 | service_impl_.construct(impl); | |
81 | } | |
82 | ||
83 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
84 | /// Move-construct a new stream descriptor implementation. | |
85 | void move_construct(implementation_type& impl, | |
86 | implementation_type& other_impl) | |
87 | { | |
88 | service_impl_.move_construct(impl, other_impl); | |
89 | } | |
90 | ||
91 | /// Move-assign from another stream descriptor implementation. | |
92 | void move_assign(implementation_type& impl, | |
93 | stream_descriptor_service& other_service, | |
94 | implementation_type& other_impl) | |
95 | { | |
96 | service_impl_.move_assign(impl, other_service.service_impl_, other_impl); | |
97 | } | |
98 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
99 | ||
100 | /// Destroy a stream descriptor implementation. | |
101 | void destroy(implementation_type& impl) | |
102 | { | |
103 | service_impl_.destroy(impl); | |
104 | } | |
105 | ||
106 | /// Assign an existing native descriptor to a stream descriptor. | |
b32b8144 | 107 | BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl, |
7c673cae FG |
108 | const native_handle_type& native_descriptor, |
109 | boost::system::error_code& ec) | |
110 | { | |
b32b8144 FG |
111 | service_impl_.assign(impl, native_descriptor, ec); |
112 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
113 | } |
114 | ||
115 | /// Determine whether the descriptor is open. | |
116 | bool is_open(const implementation_type& impl) const | |
117 | { | |
118 | return service_impl_.is_open(impl); | |
119 | } | |
120 | ||
121 | /// Close a stream descriptor implementation. | |
b32b8144 | 122 | BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl, |
7c673cae FG |
123 | boost::system::error_code& ec) |
124 | { | |
b32b8144 FG |
125 | service_impl_.close(impl, ec); |
126 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
127 | } |
128 | ||
129 | /// Get the native descriptor implementation. | |
130 | native_handle_type native_handle(implementation_type& impl) | |
131 | { | |
132 | return service_impl_.native_handle(impl); | |
133 | } | |
134 | ||
135 | /// Release ownership of the native descriptor implementation. | |
136 | native_handle_type release(implementation_type& impl) | |
137 | { | |
138 | return service_impl_.release(impl); | |
139 | } | |
140 | ||
141 | /// Cancel all asynchronous operations associated with the descriptor. | |
b32b8144 | 142 | BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl, |
7c673cae FG |
143 | boost::system::error_code& ec) |
144 | { | |
b32b8144 FG |
145 | service_impl_.cancel(impl, ec); |
146 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
147 | } |
148 | ||
149 | /// Perform an IO control command on the descriptor. | |
150 | template <typename IoControlCommand> | |
b32b8144 | 151 | BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl, |
7c673cae FG |
152 | IoControlCommand& command, boost::system::error_code& ec) |
153 | { | |
b32b8144 FG |
154 | service_impl_.io_control(impl, command, ec); |
155 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
156 | } |
157 | ||
158 | /// Gets the non-blocking mode of the descriptor. | |
159 | bool non_blocking(const implementation_type& impl) const | |
160 | { | |
161 | return service_impl_.non_blocking(impl); | |
162 | } | |
163 | ||
164 | /// Sets the non-blocking mode of the descriptor. | |
b32b8144 | 165 | BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl, |
7c673cae FG |
166 | bool mode, boost::system::error_code& ec) |
167 | { | |
b32b8144 FG |
168 | service_impl_.non_blocking(impl, mode, ec); |
169 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
170 | } |
171 | ||
172 | /// Gets the non-blocking mode of the native descriptor implementation. | |
173 | bool native_non_blocking(const implementation_type& impl) const | |
174 | { | |
175 | return service_impl_.native_non_blocking(impl); | |
176 | } | |
177 | ||
178 | /// Sets the non-blocking mode of the native descriptor implementation. | |
b32b8144 | 179 | BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl, |
7c673cae FG |
180 | bool mode, boost::system::error_code& ec) |
181 | { | |
b32b8144 FG |
182 | service_impl_.native_non_blocking(impl, mode, ec); |
183 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
184 | } | |
185 | ||
186 | /// Wait for the descriptor to become ready to read, ready to write, or to | |
187 | /// have pending error conditions. | |
188 | BOOST_ASIO_SYNC_OP_VOID wait(implementation_type& impl, | |
189 | descriptor_base::wait_type w, boost::system::error_code& ec) | |
190 | { | |
191 | service_impl_.wait(impl, w, ec); | |
192 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
193 | } | |
194 | ||
195 | /// Asynchronously wait for the descriptor to become ready to read, ready to | |
196 | /// write, or to have pending error conditions. | |
197 | template <typename WaitHandler> | |
198 | BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler, | |
199 | void (boost::system::error_code)) | |
200 | async_wait(implementation_type& impl, descriptor_base::wait_type w, | |
201 | BOOST_ASIO_MOVE_ARG(WaitHandler) handler) | |
202 | { | |
203 | async_completion<WaitHandler, | |
204 | void (boost::system::error_code)> init(handler); | |
205 | ||
206 | service_impl_.async_wait(impl, w, init.completion_handler); | |
207 | ||
208 | return init.result.get(); | |
7c673cae FG |
209 | } |
210 | ||
211 | /// Write the given data to the stream. | |
212 | template <typename ConstBufferSequence> | |
213 | std::size_t write_some(implementation_type& impl, | |
214 | const ConstBufferSequence& buffers, boost::system::error_code& ec) | |
215 | { | |
216 | return service_impl_.write_some(impl, buffers, ec); | |
217 | } | |
218 | ||
219 | /// Start an asynchronous write. | |
220 | template <typename ConstBufferSequence, typename WriteHandler> | |
221 | BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, | |
222 | void (boost::system::error_code, std::size_t)) | |
223 | async_write_some(implementation_type& impl, | |
224 | const ConstBufferSequence& buffers, | |
225 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler) | |
226 | { | |
b32b8144 FG |
227 | boost::asio::async_completion<WriteHandler, |
228 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 229 | |
b32b8144 | 230 | service_impl_.async_write_some(impl, buffers, init.completion_handler); |
7c673cae FG |
231 | |
232 | return init.result.get(); | |
233 | } | |
234 | ||
235 | /// Read some data from the stream. | |
236 | template <typename MutableBufferSequence> | |
237 | std::size_t read_some(implementation_type& impl, | |
238 | const MutableBufferSequence& buffers, boost::system::error_code& ec) | |
239 | { | |
240 | return service_impl_.read_some(impl, buffers, ec); | |
241 | } | |
242 | ||
243 | /// Start an asynchronous read. | |
244 | template <typename MutableBufferSequence, typename ReadHandler> | |
245 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
246 | void (boost::system::error_code, std::size_t)) | |
247 | async_read_some(implementation_type& impl, | |
248 | const MutableBufferSequence& buffers, | |
249 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler) | |
250 | { | |
b32b8144 FG |
251 | boost::asio::async_completion<ReadHandler, |
252 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 253 | |
b32b8144 | 254 | service_impl_.async_read_some(impl, buffers, init.completion_handler); |
7c673cae FG |
255 | |
256 | return init.result.get(); | |
257 | } | |
258 | ||
259 | private: | |
260 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 261 | void shutdown() |
7c673cae | 262 | { |
b32b8144 | 263 | service_impl_.shutdown(); |
7c673cae FG |
264 | } |
265 | ||
266 | // The platform-specific implementation. | |
267 | service_impl_type service_impl_; | |
268 | }; | |
269 | ||
270 | } // namespace posix | |
271 | } // namespace asio | |
272 | } // namespace boost | |
273 | ||
274 | #include <boost/asio/detail/pop_options.hpp> | |
275 | ||
276 | #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) | |
277 | // || defined(GENERATING_DOCUMENTATION) | |
278 | ||
b32b8144 FG |
279 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
280 | ||
7c673cae | 281 | #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP |