]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/asio/detail/reactive_descriptor_service.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / asio / detail / reactive_descriptor_service.hpp
1 //
2 // detail/reactive_descriptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2011 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_WINDOWS) && !defined(__CYGWIN__)
21
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>
34
35 #include <boost/asio/detail/push_options.hpp>
36
37 namespace boost {
38 namespace asio {
39 namespace detail {
40
41 class reactive_descriptor_service
42 {
43 public:
44   // The native type of a descriptor.
45   typedef int native_handle_type;
46
47   // The implementation type of the descriptor.
48   class implementation_type
49     : private boost::asio::detail::noncopyable
50   {
51   public:
52     // Default constructor.
53     implementation_type()
54       : descriptor_(-1),
55         state_(0)
56     {
57     }
58
59   private:
60     // Only this service will have access to the internal values.
61     friend class reactive_descriptor_service;
62
63     // The native descriptor representation.
64     int descriptor_;
65
66     // The current state of the descriptor.
67     descriptor_ops::state_type state_;
68
69     // Per-descriptor data used by the reactor.
70     reactor::per_descriptor_data reactor_data_;
71   };
72
73   // Constructor.
74   BOOST_ASIO_DECL reactive_descriptor_service(
75       boost::asio::io_service& io_service);
76
77   // Destroy all user-defined handler objects owned by the service.
78   BOOST_ASIO_DECL void shutdown_service();
79
80   // Construct a new descriptor implementation.
81   BOOST_ASIO_DECL void construct(implementation_type& impl);
82
83   // Move-construct a new descriptor implementation.
84   BOOST_ASIO_DECL void move_construct(implementation_type& impl,
85       implementation_type& other_impl);
86
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);
91
92   // Destroy a descriptor implementation.
93   BOOST_ASIO_DECL void destroy(implementation_type& impl);
94
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);
99
100   // Determine whether the descriptor is open.
101   bool is_open(const implementation_type& impl) const
102   {
103     return impl.descriptor_ != -1;
104   }
105
106   // Destroy a descriptor implementation.
107   BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
108       boost::system::error_code& ec);
109
110   // Get the native descriptor representation.
111   native_handle_type native_handle(const implementation_type& impl) const
112   {
113     return impl.descriptor_;
114   }
115
116   // Release ownership of the native descriptor representation.
117   BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
118
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);
122
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)
127   {
128     descriptor_ops::ioctl(impl.descriptor_, impl.state_,
129         command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
130     return ec;
131   }
132
133   // Gets the non-blocking mode of the descriptor.
134   bool non_blocking(const implementation_type& impl) const
135   {
136     return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
137   }
138
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)
142   {
143     descriptor_ops::set_user_non_blocking(
144         impl.descriptor_, impl.state_, mode, ec);
145     return ec;
146   }
147
148   // Gets the non-blocking mode of the native descriptor implementation.
149   bool native_non_blocking(const implementation_type& impl) const
150   {
151     return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
152   }
153
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)
157   {
158     descriptor_ops::set_internal_non_blocking(
159         impl.descriptor_, impl.state_, mode, ec);
160     return ec;
161   }
162
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)
167   {
168     buffer_sequence_adapter<boost::asio::const_buffer,
169         ConstBufferSequence> bufs(buffers);
170
171     return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
172         bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
173   }
174
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)
178   {
179     // Wait for descriptor to become ready.
180     descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
181
182     return 0;
183   }
184
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)
190   {
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);
197
198     BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
199
200     start_op(impl, reactor::write_op, p.p, true,
201         buffer_sequence_adapter<boost::asio::const_buffer,
202           ConstBufferSequence>::all_empty(buffers));
203     p.v = p.p = 0;
204   }
205
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)
210   {
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);
217
218     BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
219           &impl, "async_write_some(null_buffers)"));
220
221     start_op(impl, reactor::write_op, p.p, false, false);
222     p.v = p.p = 0;
223   }
224
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)
229   {
230     buffer_sequence_adapter<boost::asio::mutable_buffer,
231         MutableBufferSequence> bufs(buffers);
232
233     return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
234         bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
235   }
236
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)
240   {
241     // Wait for descriptor to become ready.
242     descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
243
244     return 0;
245   }
246
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)
252   {
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);
259
260     BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
261
262     start_op(impl, reactor::read_op, p.p, true,
263         buffer_sequence_adapter<boost::asio::mutable_buffer,
264           MutableBufferSequence>::all_empty(buffers));
265     p.v = p.p = 0;
266   }
267
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)
272   {
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);
279
280     BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
281           &impl, "async_read_some(null_buffers)"));
282
283     start_op(impl, reactor::read_op, p.p, false, false);
284     p.v = p.p = 0;
285   }
286
287 private:
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);
291
292   // The selector that performs event demultiplexing for the service.
293   reactor& reactor_;
294 };
295
296 } // namespace detail
297 } // namespace asio
298 } // namespace boost
299
300 #include <boost/asio/detail/pop_options.hpp>
301
302 #if defined(BOOST_ASIO_HEADER_ONLY)
303 # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
304 #endif // defined(BOOST_ASIO_HEADER_ONLY)
305
306 #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
307
308 #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP