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