Chris@16
|
1 //
|
Chris@16
|
2 // posix/stream_descriptor_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|
Chris@16
|
21 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstddef>
|
Chris@16
|
24 #include <boost/asio/async_result.hpp>
|
Chris@16
|
25 #include <boost/asio/error.hpp>
|
Chris@16
|
26 #include <boost/asio/io_service.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/reactive_descriptor_service.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace asio {
|
Chris@16
|
33 namespace posix {
|
Chris@16
|
34
|
Chris@16
|
35 /// Default service implementation for a stream descriptor.
|
Chris@16
|
36 class stream_descriptor_service
|
Chris@16
|
37 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
38 : public boost::asio::io_service::service
|
Chris@16
|
39 #else
|
Chris@16
|
40 : public boost::asio::detail::service_base<stream_descriptor_service>
|
Chris@16
|
41 #endif
|
Chris@16
|
42 {
|
Chris@16
|
43 public:
|
Chris@16
|
44 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
45 /// The unique service identifier.
|
Chris@16
|
46 static boost::asio::io_service::id id;
|
Chris@16
|
47 #endif
|
Chris@16
|
48
|
Chris@16
|
49 private:
|
Chris@16
|
50 // The type of the platform-specific implementation.
|
Chris@16
|
51 typedef detail::reactive_descriptor_service service_impl_type;
|
Chris@16
|
52
|
Chris@16
|
53 public:
|
Chris@16
|
54 /// The type of a stream descriptor implementation.
|
Chris@16
|
55 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
56 typedef implementation_defined implementation_type;
|
Chris@16
|
57 #else
|
Chris@16
|
58 typedef service_impl_type::implementation_type implementation_type;
|
Chris@16
|
59 #endif
|
Chris@16
|
60
|
Chris@16
|
61 /// (Deprecated: Use native_handle_type.) The native descriptor type.
|
Chris@16
|
62 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
63 typedef implementation_defined native_type;
|
Chris@16
|
64 #else
|
Chris@16
|
65 typedef service_impl_type::native_handle_type native_type;
|
Chris@16
|
66 #endif
|
Chris@16
|
67
|
Chris@16
|
68 /// The native descriptor type.
|
Chris@16
|
69 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
70 typedef implementation_defined native_handle_type;
|
Chris@16
|
71 #else
|
Chris@16
|
72 typedef service_impl_type::native_handle_type native_handle_type;
|
Chris@16
|
73 #endif
|
Chris@16
|
74
|
Chris@16
|
75 /// Construct a new stream descriptor service for the specified io_service.
|
Chris@16
|
76 explicit stream_descriptor_service(boost::asio::io_service& io_service)
|
Chris@16
|
77 : boost::asio::detail::service_base<stream_descriptor_service>(io_service),
|
Chris@16
|
78 service_impl_(io_service)
|
Chris@16
|
79 {
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 /// Construct a new stream descriptor implementation.
|
Chris@16
|
83 void construct(implementation_type& impl)
|
Chris@16
|
84 {
|
Chris@16
|
85 service_impl_.construct(impl);
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
89 /// Move-construct a new stream descriptor implementation.
|
Chris@16
|
90 void move_construct(implementation_type& impl,
|
Chris@16
|
91 implementation_type& other_impl)
|
Chris@16
|
92 {
|
Chris@16
|
93 service_impl_.move_construct(impl, other_impl);
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 /// Move-assign from another stream descriptor implementation.
|
Chris@16
|
97 void move_assign(implementation_type& impl,
|
Chris@16
|
98 stream_descriptor_service& other_service,
|
Chris@16
|
99 implementation_type& other_impl)
|
Chris@16
|
100 {
|
Chris@16
|
101 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
|
Chris@16
|
102 }
|
Chris@16
|
103 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
104
|
Chris@16
|
105 /// Destroy a stream descriptor implementation.
|
Chris@16
|
106 void destroy(implementation_type& impl)
|
Chris@16
|
107 {
|
Chris@16
|
108 service_impl_.destroy(impl);
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 /// Assign an existing native descriptor to a stream descriptor.
|
Chris@16
|
112 boost::system::error_code assign(implementation_type& impl,
|
Chris@16
|
113 const native_handle_type& native_descriptor,
|
Chris@16
|
114 boost::system::error_code& ec)
|
Chris@16
|
115 {
|
Chris@16
|
116 return service_impl_.assign(impl, native_descriptor, ec);
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 /// Determine whether the descriptor is open.
|
Chris@16
|
120 bool is_open(const implementation_type& impl) const
|
Chris@16
|
121 {
|
Chris@16
|
122 return service_impl_.is_open(impl);
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 /// Close a stream descriptor implementation.
|
Chris@16
|
126 boost::system::error_code close(implementation_type& impl,
|
Chris@16
|
127 boost::system::error_code& ec)
|
Chris@16
|
128 {
|
Chris@16
|
129 return service_impl_.close(impl, ec);
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 /// (Deprecated: Use native_handle().) Get the native descriptor
|
Chris@16
|
133 /// implementation.
|
Chris@16
|
134 native_type native(implementation_type& impl)
|
Chris@16
|
135 {
|
Chris@16
|
136 return service_impl_.native_handle(impl);
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 /// Get the native descriptor implementation.
|
Chris@16
|
140 native_handle_type native_handle(implementation_type& impl)
|
Chris@16
|
141 {
|
Chris@16
|
142 return service_impl_.native_handle(impl);
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 /// Release ownership of the native descriptor implementation.
|
Chris@16
|
146 native_handle_type release(implementation_type& impl)
|
Chris@16
|
147 {
|
Chris@16
|
148 return service_impl_.release(impl);
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 /// Cancel all asynchronous operations associated with the descriptor.
|
Chris@16
|
152 boost::system::error_code cancel(implementation_type& impl,
|
Chris@16
|
153 boost::system::error_code& ec)
|
Chris@16
|
154 {
|
Chris@16
|
155 return service_impl_.cancel(impl, ec);
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 /// Perform an IO control command on the descriptor.
|
Chris@16
|
159 template <typename IoControlCommand>
|
Chris@16
|
160 boost::system::error_code io_control(implementation_type& impl,
|
Chris@16
|
161 IoControlCommand& command, boost::system::error_code& ec)
|
Chris@16
|
162 {
|
Chris@16
|
163 return service_impl_.io_control(impl, command, ec);
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 /// Gets the non-blocking mode of the descriptor.
|
Chris@16
|
167 bool non_blocking(const implementation_type& impl) const
|
Chris@16
|
168 {
|
Chris@16
|
169 return service_impl_.non_blocking(impl);
|
Chris@16
|
170 }
|
Chris@16
|
171
|
Chris@16
|
172 /// Sets the non-blocking mode of the descriptor.
|
Chris@16
|
173 boost::system::error_code non_blocking(implementation_type& impl,
|
Chris@16
|
174 bool mode, boost::system::error_code& ec)
|
Chris@16
|
175 {
|
Chris@16
|
176 return service_impl_.non_blocking(impl, mode, ec);
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 /// Gets the non-blocking mode of the native descriptor implementation.
|
Chris@16
|
180 bool native_non_blocking(const implementation_type& impl) const
|
Chris@16
|
181 {
|
Chris@16
|
182 return service_impl_.native_non_blocking(impl);
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 /// Sets the non-blocking mode of the native descriptor implementation.
|
Chris@16
|
186 boost::system::error_code native_non_blocking(implementation_type& impl,
|
Chris@16
|
187 bool mode, boost::system::error_code& ec)
|
Chris@16
|
188 {
|
Chris@16
|
189 return service_impl_.native_non_blocking(impl, mode, ec);
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 /// Write the given data to the stream.
|
Chris@16
|
193 template <typename ConstBufferSequence>
|
Chris@16
|
194 std::size_t write_some(implementation_type& impl,
|
Chris@16
|
195 const ConstBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
196 {
|
Chris@16
|
197 return service_impl_.write_some(impl, buffers, ec);
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 /// Start an asynchronous write.
|
Chris@16
|
201 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
202 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
203 void (boost::system::error_code, std::size_t))
|
Chris@16
|
204 async_write_some(implementation_type& impl,
|
Chris@16
|
205 const ConstBufferSequence& buffers,
|
Chris@16
|
206 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
207 {
|
Chris@16
|
208 boost::asio::detail::async_result_init<
|
Chris@16
|
209 WriteHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
210 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
211
|
Chris@16
|
212 service_impl_.async_write_some(impl, buffers, init.handler);
|
Chris@16
|
213
|
Chris@16
|
214 return init.result.get();
|
Chris@16
|
215 }
|
Chris@16
|
216
|
Chris@16
|
217 /// Read some data from the stream.
|
Chris@16
|
218 template <typename MutableBufferSequence>
|
Chris@16
|
219 std::size_t read_some(implementation_type& impl,
|
Chris@16
|
220 const MutableBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
221 {
|
Chris@16
|
222 return service_impl_.read_some(impl, buffers, ec);
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 /// Start an asynchronous read.
|
Chris@16
|
226 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
227 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
228 void (boost::system::error_code, std::size_t))
|
Chris@16
|
229 async_read_some(implementation_type& impl,
|
Chris@16
|
230 const MutableBufferSequence& buffers,
|
Chris@16
|
231 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
232 {
|
Chris@16
|
233 boost::asio::detail::async_result_init<
|
Chris@16
|
234 ReadHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
235 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
236
|
Chris@16
|
237 service_impl_.async_read_some(impl, buffers, init.handler);
|
Chris@16
|
238
|
Chris@16
|
239 return init.result.get();
|
Chris@16
|
240 }
|
Chris@16
|
241
|
Chris@16
|
242 private:
|
Chris@16
|
243 // Destroy all user-defined handler objects owned by the service.
|
Chris@16
|
244 void shutdown_service()
|
Chris@16
|
245 {
|
Chris@16
|
246 service_impl_.shutdown_service();
|
Chris@16
|
247 }
|
Chris@16
|
248
|
Chris@16
|
249 // The platform-specific implementation.
|
Chris@16
|
250 service_impl_type service_impl_;
|
Chris@16
|
251 };
|
Chris@16
|
252
|
Chris@16
|
253 } // namespace posix
|
Chris@16
|
254 } // namespace asio
|
Chris@16
|
255 } // namespace boost
|
Chris@16
|
256
|
Chris@16
|
257 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
258
|
Chris@16
|
259 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
|
Chris@16
|
260 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
261
|
Chris@16
|
262 #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
|