1//
2// detail/reactive_descriptor_service.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 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_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>
36
37#include <boost/asio/detail/push_options.hpp>
38
39namespace boost {
40namespace asio {
41namespace detail {
42
43class reactive_descriptor_service
44{
45public:
46 // The native type of a descriptor.
47 typedef int native_handle_type;
48
49 // The implementation type of the descriptor.
50 class implementation_type
51 : private boost::asio::detail::noncopyable
52 {
53 public:
54 // Default constructor.
55 implementation_type()
56 : descriptor_(-1),
57 state_(0)
58 {
59 }
60
61 private:
62 // Only this service will have access to the internal values.
63 friend class reactive_descriptor_service;
64
65 // The native descriptor representation.
66 int descriptor_;
67
68 // The current state of the descriptor.
69 descriptor_ops::state_type state_;
70
71 // Per-descriptor data used by the reactor.
72 reactor::per_descriptor_data reactor_data_;
73 };
74
75 // Constructor.
76 BOOST_ASIO_DECL reactive_descriptor_service(
77 boost::asio::io_service& io_service);
78
79 // Destroy all user-defined handler objects owned by the service.
80 BOOST_ASIO_DECL void shutdown_service();
81
82 // Construct a new descriptor implementation.
83 BOOST_ASIO_DECL void construct(implementation_type& impl);
84
85 // Move-construct a new descriptor implementation.
86 BOOST_ASIO_DECL void move_construct(implementation_type& impl,
87 implementation_type& other_impl);
88
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);
93
94 // Destroy a descriptor implementation.
95 BOOST_ASIO_DECL void destroy(implementation_type& impl);
96
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);
101
102 // Determine whether the descriptor is open.
103 bool is_open(const implementation_type& impl) const
104 {
105 return impl.descriptor_ != -1;
106 }
107
108 // Destroy a descriptor implementation.
109 BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
110 boost::system::error_code& ec);
111
112 // Get the native descriptor representation.
113 native_handle_type native_handle(const implementation_type& impl) const
114 {
115 return impl.descriptor_;
116 }
117
118 // Release ownership of the native descriptor representation.
119 BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
120
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);
124
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)
129 {
130 descriptor_ops::ioctl(d: impl.descriptor_, state&: impl.state_,
131 cmd: command.name(), arg: static_cast<ioctl_arg_type*>(command.data()), ec);
132 return ec;
133 }
134
135 // Gets the non-blocking mode of the descriptor.
136 bool non_blocking(const implementation_type& impl) const
137 {
138 return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
139 }
140
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)
144 {
145 descriptor_ops::set_user_non_blocking(
146 d: impl.descriptor_, state&: impl.state_, value: mode, ec);
147 return ec;
148 }
149
150 // Gets the non-blocking mode of the native descriptor implementation.
151 bool native_non_blocking(const implementation_type& impl) const
152 {
153 return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
154 }
155
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)
159 {
160 descriptor_ops::set_internal_non_blocking(
161 d: impl.descriptor_, state&: impl.state_, value: mode, ec);
162 return ec;
163 }
164
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)
169 {
170 buffer_sequence_adapter<boost::asio::const_buffer,
171 ConstBufferSequence> bufs(buffers);
172
173 return descriptor_ops::sync_write(d: impl.descriptor_, state: impl.state_,
174 bufs: bufs.buffers(), count: bufs.count(), all_empty: bufs.all_empty(), ec);
175 }
176
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)
180 {
181 // Wait for descriptor to become ready.
182 descriptor_ops::poll_write(d: impl.descriptor_, state: impl.state_, ec);
183
184 return 0;
185 }
186
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)
192 {
193 bool is_continuation =
194 boost_asio_handler_cont_helpers::is_continuation(handler);
195
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);
202
203 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
204
205 start_op(impl, op_type: reactor::write_op, op: p.p, is_continuation, is_non_blocking: true,
206 noop: buffer_sequence_adapter<boost::asio::const_buffer,
207 ConstBufferSequence>::all_empty(buffers));
208 p.v = p.p = 0;
209 }
210
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)
215 {
216 bool is_continuation =
217 boost_asio_handler_cont_helpers::is_continuation(handler);
218
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);
225
226 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
227 &impl, "async_write_some(null_buffers)"));
228
229 start_op(impl, op_type: reactor::write_op, op: p.p, is_continuation, is_non_blocking: false, noop: false);
230 p.v = p.p = 0;
231 }
232
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)
237 {
238 buffer_sequence_adapter<boost::asio::mutable_buffer,
239 MutableBufferSequence> bufs(buffers);
240
241 return descriptor_ops::sync_read(d: impl.descriptor_, state: impl.state_,
242 bufs: bufs.buffers(), count: bufs.count(), all_empty: bufs.all_empty(), ec);
243 }
244
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)
248 {
249 // Wait for descriptor to become ready.
250 descriptor_ops::poll_read(d: impl.descriptor_, state: impl.state_, ec);
251
252 return 0;
253 }
254
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)
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_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);
270
271 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
272
273 start_op(impl, op_type: reactor::read_op, op: p.p, is_continuation, is_non_blocking: true,
274 noop: buffer_sequence_adapter<boost::asio::mutable_buffer,
275 MutableBufferSequence>::all_empty(buffers));
276 p.v = p.p = 0;
277 }
278
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)
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 boost_asio_handler_alloc_helpers::allocate(
291 sizeof(op), handler), 0 };
292 p.p = new (p.v) op(handler);
293
294 BOOST_ASIO_HANDLER_CREATION((p.p, "descriptor",
295 &impl, "async_read_some(null_buffers)"));
296
297 start_op(impl, op_type: reactor::read_op, op: p.p, is_continuation, is_non_blocking: false, noop: false);
298 p.v = p.p = 0;
299 }
300
301private:
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);
305
306 // The selector that performs event demultiplexing for the service.
307 reactor& reactor_;
308};
309
310} // namespace detail
311} // namespace asio
312} // namespace boost
313
314#include <boost/asio/detail/pop_options.hpp>
315
316#if defined(BOOST_ASIO_HEADER_ONLY)
317# include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
318#endif // defined(BOOST_ASIO_HEADER_ONLY)
319
320#endif // !defined(BOOST_ASIO_WINDOWS)
321 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
322 // && !defined(__CYGWIN__)
323
324#endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
325

source code of boost/boost/asio/detail/reactive_descriptor_service.hpp