2 // detail/reactive_descriptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 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_WINDOWS) && !defined(__CYGWIN__)
22 #include <boost/utility/addressof.hpp>
23 #include <boost/asio/buffer.hpp>
24 #include <boost/asio/io_service.hpp>
25 #include <boost/asio/detail/bind_handler.hpp>
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
27 #include <boost/asio/detail/descriptor_ops.hpp>
28 #include <boost/asio/detail/descriptor_read_op.hpp>
29 #include <boost/asio/detail/descriptor_write_op.hpp>
30 #include <boost/asio/detail/fenced_block.hpp>
31 #include <boost/asio/detail/noncopyable.hpp>
32 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
33 #include <boost/asio/detail/reactor.hpp>
35 #include <boost/asio/detail/push_options.hpp>
41 class reactive_descriptor_service
44 // The native type of a descriptor.
45 typedef int native_handle_type;
47 // The implementation type of the descriptor.
48 class implementation_type
49 : private boost::asio::detail::noncopyable
52 // Default constructor.
60 // Only this service will have access to the internal values.
61 friend class reactive_descriptor_service;
63 // The native descriptor representation.
66 // The current state of the descriptor.
67 descriptor_ops::state_type state_;
69 // Per-descriptor data used by the reactor.
70 reactor::per_descriptor_data reactor_data_;
74 BOOST_ASIO_DECL reactive_descriptor_service(
75 boost::asio::io_service& io_service);
77 // Destroy all user-defined handler objects owned by the service.
78 BOOST_ASIO_DECL void shutdown_service();
80 // Construct a new descriptor implementation.
81 BOOST_ASIO_DECL void construct(implementation_type& impl);
83 // Move-construct a new descriptor implementation.
84 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
85 implementation_type& other_impl);
87 // Move-assign from another descriptor implementation.
88 BOOST_ASIO_DECL void move_assign(implementation_type& impl,
89 reactive_descriptor_service& other_service,
90 implementation_type& other_impl);
92 // Destroy a descriptor implementation.
93 BOOST_ASIO_DECL void destroy(implementation_type& impl);
95 // Assign a native descriptor to a descriptor implementation.
96 BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
97 const native_handle_type& native_descriptor,
98 boost::system::error_code& ec);
100 // Determine whether the descriptor is open.
101 bool is_open(const implementation_type& impl) const
103 return impl.descriptor_ != -1;
106 // Destroy a descriptor implementation.
107 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
108 boost::system::error_code& ec);
110 // Get the native descriptor representation.
111 native_handle_type native_handle(const implementation_type& impl) const
113 return impl.descriptor_;
116 // Release ownership of the native descriptor representation.
117 BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
119 // Cancel all operations associated with the descriptor.
120 BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
121 boost::system::error_code& ec);
123 // Perform an IO control command on the descriptor.
124 template <typename IO_Control_Command>
125 boost::system::error_code io_control(implementation_type& impl,
126 IO_Control_Command& command, boost::system::error_code& ec)
128 descriptor_ops::ioctl(impl.descriptor_, impl.state_,
129 command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
133 // Gets the non-blocking mode of the descriptor.
134 bool non_blocking(const implementation_type& impl) const
136 return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
139 // Sets the non-blocking mode of the descriptor.
140 boost::system::error_code non_blocking(implementation_type& impl,
141 bool mode, boost::system::error_code& ec)
143 descriptor_ops::set_user_non_blocking(
144 impl.descriptor_, impl.state_, mode, ec);
148 // Gets the non-blocking mode of the native descriptor implementation.
149 bool native_non_blocking(const implementation_type& impl) const
151 return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
154 // Sets the non-blocking mode of the native descriptor implementation.
155 boost::system::error_code native_non_blocking(implementation_type& impl,
156 bool mode, boost::system::error_code& ec)
158 descriptor_ops::set_internal_non_blocking(
159 impl.descriptor_, impl.state_, mode, ec);
163 // Write some data to the descriptor.
164 template <typename ConstBufferSequence>
165 size_t write_some(implementation_type& impl,
166 const ConstBufferSequence& buffers, boost::system::error_code& ec)
168 buffer_sequence_adapter<boost::asio::const_buffer,
169 ConstBufferSequence> bufs(buffers);
171 return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
172 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
175 // Wait until data can be written without blocking.
176 size_t write_some(implementation_type& impl,
177 const null_buffers&, boost::system::error_code& ec)
179 // Wait for descriptor to become ready.
180 descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
185 // Start an asynchronous write. The data being sent must be valid for the
186 // lifetime of the asynchronous operation.
187 template <typename ConstBufferSequence, typename Handler>
188 void async_write_some(implementation_type& impl,
189 const ConstBufferSequence& buffers, Handler handler)
191 // Allocate and construct an operation to wrap the handler.
192 typedef descriptor_write_op<ConstBufferSequence, Handler> op;
193 typename op::ptr p = { boost::addressof(handler),
194 boost_asio_handler_alloc_helpers::allocate(
195 sizeof(op), handler), 0 };
196 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
198 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
200 start_op(impl, reactor::write_op, p.p, true,
201 buffer_sequence_adapter<boost::asio::const_buffer,
202 ConstBufferSequence>::all_empty(buffers));
206 // Start an asynchronous wait until data can be written without blocking.
207 template <typename Handler>
208 void async_write_some(implementation_type& impl,
209 const null_buffers&, Handler handler)
211 // Allocate and construct an operation to wrap the handler.
212 typedef reactive_null_buffers_op<Handler> op;
213 typename op::ptr p = { boost::addressof(handler),
214 boost_asio_handler_alloc_helpers::allocate(
215 sizeof(op), handler), 0 };
216 p.p = new (p.v) op(handler);
218 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
219 &impl, "async_write_some(null_buffers)"));
221 start_op(impl, reactor::write_op, p.p, false, false);
225 // Read some data from the stream. Returns the number of bytes read.
226 template <typename MutableBufferSequence>
227 size_t read_some(implementation_type& impl,
228 const MutableBufferSequence& buffers, boost::system::error_code& ec)
230 buffer_sequence_adapter<boost::asio::mutable_buffer,
231 MutableBufferSequence> bufs(buffers);
233 return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
234 bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
237 // Wait until data can be read without blocking.
238 size_t read_some(implementation_type& impl,
239 const null_buffers&, boost::system::error_code& ec)
241 // Wait for descriptor to become ready.
242 descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
247 // Start an asynchronous read. The buffer for the data being read must be
248 // valid for the lifetime of the asynchronous operation.
249 template <typename MutableBufferSequence, typename Handler>
250 void async_read_some(implementation_type& impl,
251 const MutableBufferSequence& buffers, Handler handler)
253 // Allocate and construct an operation to wrap the handler.
254 typedef descriptor_read_op<MutableBufferSequence, Handler> op;
255 typename op::ptr p = { boost::addressof(handler),
256 boost_asio_handler_alloc_helpers::allocate(
257 sizeof(op), handler), 0 };
258 p.p = new (p.v) op(impl.descriptor_, buffers, handler);
260 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
262 start_op(impl, reactor::read_op, p.p, true,
263 buffer_sequence_adapter<boost::asio::mutable_buffer,
264 MutableBufferSequence>::all_empty(buffers));
268 // Wait until data can be read without blocking.
269 template <typename Handler>
270 void async_read_some(implementation_type& impl,
271 const null_buffers&, Handler handler)
273 // Allocate and construct an operation to wrap the handler.
274 typedef reactive_null_buffers_op<Handler> op;
275 typename op::ptr p = { boost::addressof(handler),
276 boost_asio_handler_alloc_helpers::allocate(
277 sizeof(op), handler), 0 };
278 p.p = new (p.v) op(handler);
280 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
281 &impl, "async_read_some(null_buffers)"));
283 start_op(impl, reactor::read_op, p.p, false, false);
288 // Start the asynchronous operation.
289 BOOST_ASIO_DECL void start_op(implementation_type& impl, int op_type,
290 reactor_op* op, bool is_non_blocking, bool noop);
292 // The selector that performs event demultiplexing for the service.
296 } // namespace detail
300 #include <boost/asio/detail/pop_options.hpp>
302 #if defined(BOOST_ASIO_HEADER_ONLY)
303 # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
304 #endif // defined(BOOST_ASIO_HEADER_ONLY)
306 #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
308 #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP