2 // detail/reactive_descriptor_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_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_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_WINDOWS) \
21 && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
22 && !defined(__CYGWIN__)
24 #include <boost/asio/buffer.hpp>
25 #include <boost/asio/io_service.hpp>
26 #include <boost/asio/detail/addressof.hpp>
27 #include <boost/asio/detail/bind_handler.hpp>
28 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
29 #include <boost/asio/detail/descriptor_ops.hpp>
30 #include <boost/asio/detail/descriptor_read_op.hpp>
31 #include <boost/asio/detail/descriptor_write_op.hpp>
32 #include <boost/asio/detail/fenced_block.hpp>
33 #include <boost/asio/detail/noncopyable.hpp>
34 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
35 #include <boost/asio/detail/reactor.hpp>
37 #include <boost/asio/detail/push_options.hpp>
43 class reactive_descriptor_service
46 // The native type of a descriptor.
47 typedef int native_handle_type;
49 // The implementation type of the descriptor.
50 class implementation_type
51 : private boost::asio::detail::noncopyable
54 // Default constructor.
62 // Only this service will have access to the internal values.
63 friend class reactive_descriptor_service;
65 // The native descriptor representation.
68 // The current state of the descriptor.
69 descriptor_ops::state_type state_;
71 // Per-descriptor data used by the reactor.
72 reactor::per_descriptor_data reactor_data_;
76 BOOST_ASIO_DECL reactive_descriptor_service(
77 boost::asio::io_service& io_service);
79 // Destroy all user-defined handler objects owned by the service.
80 BOOST_ASIO_DECL void shutdown_service();
82 // Construct a new descriptor implementation.
83 BOOST_ASIO_DECL void construct(implementation_type& impl);
85 // Move-construct a new descriptor implementation.
86 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
87 implementation_type& other_impl);
89 // Move-assign from another descriptor implementation.
90 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
91 reactive_descriptor_service& other_service,
92 implementation_type& other_impl);
94 // Destroy a descriptor implementation.
95 BOOST_ASIO_DECL void destroy(implementation_type& impl);
97 // Assign a native descriptor to a descriptor implementation.
98 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
99 const native_handle_type& native_descriptor,
100 boost::system::error_code& ec);
102 // Determine whether the descriptor is open.
103 bool is_open(const implementation_type& impl) const
105 return impl.descriptor_ != -1;
108 // Destroy a descriptor implementation.
109 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
110 boost::system::error_code& ec);
112 // Get the native descriptor representation.
113 native_handle_type native_handle(const implementation_type& impl) const
115 return impl.descriptor_;
118 // Release ownership of the native descriptor representation.
119 BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
121 // Cancel all operations associated with the descriptor.
122 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
123 boost::system::error_code& ec);
125 // Perform an IO control command on the descriptor.
126 template <typename IO_Control_Command>
127 boost::system::error_code io_control(implementation_type& impl,
128 IO_Control_Command& command, boost::system::error_code& ec)
130 descriptor_ops::ioctl(impl.descriptor_, impl.state_,
131 command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
135 // Gets the non-blocking mode of the descriptor.
136 bool non_blocking(const implementation_type& impl) const
138 return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
141 // Sets the non-blocking mode of the descriptor.
142 boost::system::error_code non_blocking(implementation_type& impl,
143 bool mode, boost::system::error_code& ec)
145 descriptor_ops::set_user_non_blocking(
146 impl.descriptor_, impl.state_, mode, ec);
150 // Gets the non-blocking mode of the native descriptor implementation.
151 bool native_non_blocking(const implementation_type& impl) const
153 return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
156 // Sets the non-blocking mode of the native descriptor implementation.
157 boost::system::error_code native_non_blocking(implementation_type& impl,
158 bool mode, boost::system::error_code& ec)
160 descriptor_ops::set_internal_non_blocking(
161 impl.descriptor_, impl.state_, mode, ec);
165 // Write some data to the descriptor.
166 template <typename ConstBufferSequence>
167 size_t write_some(implementation_type& impl,
168 const ConstBufferSequence& buffers, boost::system::error_code& ec)
170 buffer_sequence_adapter<boost::asio::const_buffer,
171 ConstBufferSequence> bufs(buffers);
173 return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
174 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
177 // Wait until data can be written without blocking.
178 size_t write_some(implementation_type& impl,
179 const null_buffers&, boost::system::error_code& ec)
181 // Wait for descriptor to become ready.
182 descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
187 // Start an asynchronous write. The data being sent must be valid for the
188 // lifetime of the asynchronous operation.
189 template <typename ConstBufferSequence, typename Handler>
190 void async_write_some(implementation_type& impl,
191 const ConstBufferSequence& buffers, Handler& handler)
193 bool is_continuation =
194 boost_asio_handler_cont_helpers::is_continuation(handler);
196 // Allocate and construct an operation to wrap the handler.
197 typedef descriptor_write_op<ConstBufferSequence, Handler> op;
198 typename op::ptr p = { boost::asio::detail::addressof(handler),
199 boost_asio_handler_alloc_helpers::allocate(
200 sizeof(op), handler), 0 };
201 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
203 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
205 start_op(impl, reactor::write_op, p.p, is_continuation, true,
206 buffer_sequence_adapter<boost::asio::const_buffer,
207 ConstBufferSequence>::all_empty(buffers));
211 // Start an asynchronous wait until data can be written without blocking.
212 template <typename Handler>
213 void async_write_some(implementation_type& impl,
214 const null_buffers&, Handler& handler)
216 bool is_continuation =
217 boost_asio_handler_cont_helpers::is_continuation(handler);
219 // Allocate and construct an operation to wrap the handler.
220 typedef reactive_null_buffers_op<Handler> op;
221 typename op::ptr p = { boost::asio::detail::addressof(handler),
222 boost_asio_handler_alloc_helpers::allocate(
223 sizeof(op), handler), 0 };
224 p.p = new (p.v) op(handler);
226 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
227 &impl, "async_write_some(null_buffers)"));
229 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
233 // Read some data from the stream. Returns the number of bytes read.
234 template <typename MutableBufferSequence>
235 size_t read_some(implementation_type& impl,
236 const MutableBufferSequence& buffers, boost::system::error_code& ec)
238 buffer_sequence_adapter<boost::asio::mutable_buffer,
239 MutableBufferSequence> bufs(buffers);
241 return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
242 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
245 // Wait until data can be read without blocking.
246 size_t read_some(implementation_type& impl,
247 const null_buffers&, boost::system::error_code& ec)
249 // Wait for descriptor to become ready.
250 descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
255 // Start an asynchronous read. The buffer for the data being read must be
256 // valid for the lifetime of the asynchronous operation.
257 template <typename MutableBufferSequence, typename Handler>
258 void async_read_some(implementation_type& impl,
259 const MutableBufferSequence& buffers, Handler& handler)
261 bool is_continuation =
262 boost_asio_handler_cont_helpers::is_continuation(handler);
264 // Allocate and construct an operation to wrap the handler.
265 typedef descriptor_read_op<MutableBufferSequence, Handler> op;
266 typename op::ptr p = { boost::asio::detail::addressof(handler),
267 boost_asio_handler_alloc_helpers::allocate(
268 sizeof(op), handler), 0 };
269 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
271 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
273 start_op(impl, reactor::read_op, p.p, is_continuation, true,
274 buffer_sequence_adapter<boost::asio::mutable_buffer,
275 MutableBufferSequence>::all_empty(buffers));
279 // Wait until data can be read without blocking.
280 template <typename Handler>
281 void async_read_some(implementation_type& impl,
282 const null_buffers&, Handler& handler)
284 bool is_continuation =
285 boost_asio_handler_cont_helpers::is_continuation(handler);
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 boost_asio_handler_alloc_helpers::allocate(
291 sizeof(op), handler), 0 };
292 p.p = new (p.v) op(handler);
294 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
295 &impl, "async_read_some(null_buffers)"));
297 start_op(impl, reactor::read_op, p.p, is_continuation, false, false);
302 // Start the asynchronous operation.
303 BOOST_ASIO_DECL void start_op(implementation_type& impl, int op_type,
304 reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
306 // The selector that performs event demultiplexing for the service.
310 } // namespace detail
314 #include <boost/asio/detail/pop_options.hpp>
316 #if defined(BOOST_ASIO_HEADER_ONLY)
317 # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
318 #endif // defined(BOOST_ASIO_HEADER_ONLY)
320 #endif // !defined(BOOST_ASIO_WINDOWS)
321 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
322 // && !defined(__CYGWIN__)
324 #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP