]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/reactive_descriptor_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / reactive_descriptor_service.hpp
1 //
2 // detail/reactive_descriptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_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
20 #if !defined(BOOST_ASIO_WINDOWS) \
21 && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
22 && !defined(__CYGWIN__)
23
24 #include <boost/asio/buffer.hpp>
25 #include <boost/asio/io_context.hpp>
26 #include <boost/asio/detail/bind_handler.hpp>
27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
28 #include <boost/asio/detail/descriptor_ops.hpp>
29 #include <boost/asio/detail/descriptor_read_op.hpp>
30 #include <boost/asio/detail/descriptor_write_op.hpp>
31 #include <boost/asio/detail/fenced_block.hpp>
32 #include <boost/asio/detail/memory.hpp>
33 #include <boost/asio/detail/noncopyable.hpp>
34 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
35 #include <boost/asio/detail/reactive_wait_op.hpp>
36 #include <boost/asio/detail/reactor.hpp>
37 #include <boost/asio/posix/descriptor_base.hpp>
38
39 #include <boost/asio/detail/push_options.hpp>
40
41 namespace boost {
42 namespace asio {
43 namespace detail {
44
45 class reactive_descriptor_service :
46 public service_base<reactive_descriptor_service>
47 {
48 public:
49 // The native type of a descriptor.
50 typedef int native_handle_type;
51
52 // The implementation type of the descriptor.
53 class implementation_type
54 : private boost::asio::detail::noncopyable
55 {
56 public:
57 // Default constructor.
58 implementation_type()
59 : descriptor_(-1),
60 state_(0)
61 {
62 }
63
64 private:
65 // Only this service will have access to the internal values.
66 friend class reactive_descriptor_service;
67
68 // The native descriptor representation.
69 int descriptor_;
70
71 // The current state of the descriptor.
72 descriptor_ops::state_type state_;
73
74 // Per-descriptor data used by the reactor.
75 reactor::per_descriptor_data reactor_data_;
76 };
77
78 // Constructor.
79 BOOST_ASIO_DECL reactive_descriptor_service(
80 boost::asio::io_context& io_context);
81
82 // Destroy all user-defined handler objects owned by the service.
83 BOOST_ASIO_DECL void shutdown();
84
85 // Construct a new descriptor implementation.
86 BOOST_ASIO_DECL void construct(implementation_type& impl);
87
88 // Move-construct a new descriptor implementation.
89 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
90 implementation_type& other_impl);
91
92 // Move-assign from another descriptor implementation.
93 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
94 reactive_descriptor_service& other_service,
95 implementation_type& other_impl);
96
97 // Destroy a descriptor implementation.
98 BOOST_ASIO_DECL void destroy(implementation_type& impl);
99
100 // Assign a native descriptor to a descriptor implementation.
101 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
102 const native_handle_type& native_descriptor,
103 boost::system::error_code& ec);
104
105 // Determine whether the descriptor is open.
106 bool is_open(const implementation_type& impl) const
107 {
108 return impl.descriptor_ != -1;
109 }
110
111 // Destroy a descriptor implementation.
112 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
113 boost::system::error_code& ec);
114
115 // Get the native descriptor representation.
116 native_handle_type native_handle(const implementation_type& impl) const
117 {
118 return impl.descriptor_;
119 }
120
121 // Release ownership of the native descriptor representation.
122 BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
123
124 // Cancel all operations associated with the descriptor.
125 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
126 boost::system::error_code& ec);
127
128 // Perform an IO control command on the descriptor.
129 template <typename IO_Control_Command>
130 boost::system::error_code io_control(implementation_type& impl,
131 IO_Control_Command& command, boost::system::error_code& ec)
132 {
133 descriptor_ops::ioctl(impl.descriptor_, impl.state_,
134 command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
135 return ec;
136 }
137
138 // Gets the non-blocking mode of the descriptor.
139 bool non_blocking(const implementation_type& impl) const
140 {
141 return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
142 }
143
144 // Sets the non-blocking mode of the descriptor.
145 boost::system::error_code non_blocking(implementation_type& impl,
146 bool mode, boost::system::error_code& ec)
147 {
148 descriptor_ops::set_user_non_blocking(
149 impl.descriptor_, impl.state_, mode, ec);
150 return ec;
151 }
152
153 // Gets the non-blocking mode of the native descriptor implementation.
154 bool native_non_blocking(const implementation_type& impl) const
155 {
156 return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
157 }
158
159 // Sets the non-blocking mode of the native descriptor implementation.
160 boost::system::error_code native_non_blocking(implementation_type& impl,
161 bool mode, boost::system::error_code& ec)
162 {
163 descriptor_ops::set_internal_non_blocking(
164 impl.descriptor_, impl.state_, mode, ec);
165 return ec;
166 }
167
168 // Wait for the descriptor to become ready to read, ready to write, or to have
169 // pending error conditions.
170 boost::system::error_code wait(implementation_type& impl,
171 posix::descriptor_base::wait_type w, boost::system::error_code& ec)
172 {
173 switch (w)
174 {
175 case posix::descriptor_base::wait_read:
176 descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
177 break;
178 case posix::descriptor_base::wait_write:
179 descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
180 break;
181 case posix::descriptor_base::wait_error:
182 descriptor_ops::poll_error(impl.descriptor_, impl.state_, ec);
183 break;
184 default:
185 ec = boost::asio::error::invalid_argument;
186 break;
187 }
188
189 return ec;
190 }
191
192 // Asynchronously wait for the descriptor to become ready to read, ready to
193 // write, or to have pending error conditions.
194 template <typename Handler>
195 void async_wait(implementation_type& impl,
196 posix::descriptor_base::wait_type w, Handler& handler)
197 {
198 bool is_continuation =
199 boost_asio_handler_cont_helpers::is_continuation(handler);
200
201 // Allocate and construct an operation to wrap the handler.
202 typedef reactive_wait_op<Handler> op;
203 typename op::ptr p = { boost::asio::detail::addressof(handler),
204 op::ptr::allocate(handler), 0 };
205 p.p = new (p.v) op(handler);
206
207 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
208 &impl, impl.descriptor_, "async_wait"));
209
210 int op_type;
211 switch (w)
212 {
213 case posix::descriptor_base::wait_read:
214 op_type = reactor::read_op;
215 break;
216 case posix::descriptor_base::wait_write:
217 op_type = reactor::write_op;
218 break;
219 case posix::descriptor_base::wait_error:
220 op_type = reactor::except_op;
221 break;
222 default:
223 p.p->ec_ = boost::asio::error::invalid_argument;
224 reactor_.post_immediate_completion(p.p, is_continuation);
225 p.v = p.p = 0;
226 return;
227 }
228
229 start_op(impl, op_type, p.p, is_continuation, false, false);
230 p.v = p.p = 0;
231 }
232
233 // Write some data to the descriptor.
234 template <typename ConstBufferSequence>
235 size_t write_some(implementation_type& impl,
236 const ConstBufferSequence& buffers, boost::system::error_code& ec)
237 {
238 buffer_sequence_adapter<boost::asio::const_buffer,
239 ConstBufferSequence> bufs(buffers);
240
241 return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
242 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
243 }
244
245 // Wait until data can be written without blocking.
246 size_t write_some(implementation_type& impl,
247 const null_buffers&, boost::system::error_code& ec)
248 {
249 // Wait for descriptor to become ready.
250 descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
251
252 return 0;
253 }
254
255 // Start an asynchronous write. The data being sent must be valid for the
256 // lifetime of the asynchronous operation.
257 template <typename ConstBufferSequence, typename Handler>
258 void async_write_some(implementation_type& impl,
259 const ConstBufferSequence& buffers, Handler& handler)
260 {
261 bool is_continuation =
262 boost_asio_handler_cont_helpers::is_continuation(handler);
263
264 // Allocate and construct an operation to wrap the handler.
265 typedef descriptor_write_op<ConstBufferSequence, Handler> op;
266 typename op::ptr p = { boost::asio::detail::addressof(handler),
267 op::ptr::allocate(handler), 0 };
268 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
269
270 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
271 &impl, impl.descriptor_, "async_write_some"));
272
273 start_op(impl, reactor::write_op, p.p, is_continuation, true,
274 buffer_sequence_adapter<boost::asio::const_buffer,
275 ConstBufferSequence>::all_empty(buffers));
276 p.v = p.p = 0;
277 }
278
279 // Start an asynchronous wait until data can be written without blocking.
280 template <typename Handler>
281 void async_write_some(implementation_type& impl,
282 const null_buffers&, Handler& handler)
283 {
284 bool is_continuation =
285 boost_asio_handler_cont_helpers::is_continuation(handler);
286
287 // Allocate and construct an operation to wrap the handler.
288 typedef reactive_null_buffers_op<Handler> op;
289 typename op::ptr p = { boost::asio::detail::addressof(handler),
290 op::ptr::allocate(handler), 0 };
291 p.p = new (p.v) op(handler);
292
293 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
294 &impl, impl.descriptor_, "async_write_some(null_buffers)"));
295
296 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
297 p.v = p.p = 0;
298 }
299
300 // Read some data from the stream. Returns the number of bytes read.
301 template <typename MutableBufferSequence>
302 size_t read_some(implementation_type& impl,
303 const MutableBufferSequence& buffers, boost::system::error_code& ec)
304 {
305 buffer_sequence_adapter<boost::asio::mutable_buffer,
306 MutableBufferSequence> bufs(buffers);
307
308 return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
309 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
310 }
311
312 // Wait until data can be read without blocking.
313 size_t read_some(implementation_type& impl,
314 const null_buffers&, boost::system::error_code& ec)
315 {
316 // Wait for descriptor to become ready.
317 descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
318
319 return 0;
320 }
321
322 // Start an asynchronous read. The buffer for the data being read must be
323 // valid for the lifetime of the asynchronous operation.
324 template <typename MutableBufferSequence, typename Handler>
325 void async_read_some(implementation_type& impl,
326 const MutableBufferSequence& buffers, Handler& handler)
327 {
328 bool is_continuation =
329 boost_asio_handler_cont_helpers::is_continuation(handler);
330
331 // Allocate and construct an operation to wrap the handler.
332 typedef descriptor_read_op<MutableBufferSequence, Handler> op;
333 typename op::ptr p = { boost::asio::detail::addressof(handler),
334 op::ptr::allocate(handler), 0 };
335 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
336
337 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
338 &impl, impl.descriptor_, "async_read_some"));
339
340 start_op(impl, reactor::read_op, p.p, is_continuation, true,
341 buffer_sequence_adapter<boost::asio::mutable_buffer,
342 MutableBufferSequence>::all_empty(buffers));
343 p.v = p.p = 0;
344 }
345
346 // Wait until data can be read without blocking.
347 template <typename Handler>
348 void async_read_some(implementation_type& impl,
349 const null_buffers&, Handler& handler)
350 {
351 bool is_continuation =
352 boost_asio_handler_cont_helpers::is_continuation(handler);
353
354 // Allocate and construct an operation to wrap the handler.
355 typedef reactive_null_buffers_op<Handler> op;
356 typename op::ptr p = { boost::asio::detail::addressof(handler),
357 op::ptr::allocate(handler), 0 };
358 p.p = new (p.v) op(handler);
359
360 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
361 &impl, impl.descriptor_, "async_read_some(null_buffers)"));
362
363 start_op(impl, reactor::read_op, p.p, is_continuation, false, false);
364 p.v = p.p = 0;
365 }
366
367 private:
368 // Start the asynchronous operation.
369 BOOST_ASIO_DECL void start_op(implementation_type& impl, int op_type,
370 reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
371
372 // The selector that performs event demultiplexing for the service.
373 reactor& reactor_;
374 };
375
376 } // namespace detail
377 } // namespace asio
378 } // namespace boost
379
380 #include <boost/asio/detail/pop_options.hpp>
381
382 #if defined(BOOST_ASIO_HEADER_ONLY)
383 # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
384 #endif // defined(BOOST_ASIO_HEADER_ONLY)
385
386 #endif // !defined(BOOST_ASIO_WINDOWS)
387 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
388 // && !defined(__CYGWIN__)
389
390 #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP