2 // posix/stream_descriptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP
12 #define BOOST_ASIO_POSIX_STREAM_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_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
21 || defined(GENERATING_DOCUMENTATION)
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>
29 #include <boost/asio/detail/push_options.hpp>
35 /// Default service implementation for a stream descriptor.
36 class stream_descriptor_service
37 #if defined(GENERATING_DOCUMENTATION)
38 : public boost::asio::io_service::service
40 : public boost::asio::detail::service_base<stream_descriptor_service>
44 #if defined(GENERATING_DOCUMENTATION)
45 /// The unique service identifier.
46 static boost::asio::io_service::id id;
50 // The type of the platform-specific implementation.
51 typedef detail::reactive_descriptor_service service_impl_type;
54 /// The type of a stream descriptor implementation.
55 #if defined(GENERATING_DOCUMENTATION)
56 typedef implementation_defined implementation_type;
58 typedef service_impl_type::implementation_type implementation_type;
61 /// (Deprecated: Use native_handle_type.) The native descriptor type.
62 #if defined(GENERATING_DOCUMENTATION)
63 typedef implementation_defined native_type;
65 typedef service_impl_type::native_handle_type native_type;
68 /// The native descriptor type.
69 #if defined(GENERATING_DOCUMENTATION)
70 typedef implementation_defined native_handle_type;
72 typedef service_impl_type::native_handle_type native_handle_type;
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)
82 /// Construct a new stream descriptor implementation.
83 void construct(implementation_type& impl)
85 service_impl_.construct(impl);
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)
93 service_impl_.move_construct(impl, other_impl);
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)
101 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
103 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
105 /// Destroy a stream descriptor implementation.
106 void destroy(implementation_type& impl)
108 service_impl_.destroy(impl);
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)
116 return service_impl_.assign(impl, native_descriptor, ec);
119 /// Determine whether the descriptor is open.
120 bool is_open(const implementation_type& impl) const
122 return service_impl_.is_open(impl);
125 /// Close a stream descriptor implementation.
126 boost::system::error_code close(implementation_type& impl,
127 boost::system::error_code& ec)
129 return service_impl_.close(impl, ec);
132 /// (Deprecated: Use native_handle().) Get the native descriptor
134 native_type native(implementation_type& impl)
136 return service_impl_.native_handle(impl);
139 /// Get the native descriptor implementation.
140 native_handle_type native_handle(implementation_type& impl)
142 return service_impl_.native_handle(impl);
145 /// Release ownership of the native descriptor implementation.
146 native_handle_type release(implementation_type& impl)
148 return service_impl_.release(impl);
151 /// Cancel all asynchronous operations associated with the descriptor.
152 boost::system::error_code cancel(implementation_type& impl,
153 boost::system::error_code& ec)
155 return service_impl_.cancel(impl, ec);
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)
163 return service_impl_.io_control(impl, command, ec);
166 /// Gets the non-blocking mode of the descriptor.
167 bool non_blocking(const implementation_type& impl) const
169 return service_impl_.non_blocking(impl);
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)
176 return service_impl_.non_blocking(impl, mode, ec);
179 /// Gets the non-blocking mode of the native descriptor implementation.
180 bool native_non_blocking(const implementation_type& impl) const
182 return service_impl_.native_non_blocking(impl);
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)
189 return service_impl_.native_non_blocking(impl, mode, ec);
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)
197 return service_impl_.write_some(impl, buffers, ec);
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)
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));
212 service_impl_.async_write_some(impl, buffers, init.handler);
214 return init.result.get();
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)
222 return service_impl_.read_some(impl, buffers, ec);
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)
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));
237 service_impl_.async_read_some(impl, buffers, init.handler);
239 return init.result.get();
243 // Destroy all user-defined handler objects owned by the service.
244 void shutdown_service()
246 service_impl_.shutdown_service();
249 // The platform-specific implementation.
250 service_impl_type service_impl_;
257 #include <boost/asio/detail/pop_options.hpp>
259 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
260 // || defined(GENERATING_DOCUMENTATION)
262 #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_SERVICE_HPP