2 // posix/basic_stream_descriptor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 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_BASIC_STREAM_DESCRIPTOR_HPP
12 #define BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_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_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
23 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/detail/handler_type_requirements.hpp>
27 #include <boost/asio/detail/throw_error.hpp>
28 #include <boost/asio/error.hpp>
29 #include <boost/asio/posix/basic_descriptor.hpp>
30 #include <boost/asio/posix/stream_descriptor_service.hpp>
32 #include <boost/asio/detail/push_options.hpp>
38 /// Provides stream-oriented descriptor functionality.
40 * The posix::basic_stream_descriptor class template provides asynchronous and
41 * blocking stream-oriented descriptor functionality.
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
48 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
50 template <typename StreamDescriptorService = stream_descriptor_service>
51 class basic_stream_descriptor
52 : public basic_descriptor<StreamDescriptorService>
55 /// The native representation of a descriptor.
56 typedef typename StreamDescriptorService::native_handle_type
59 /// Construct a basic_stream_descriptor without opening it.
61 * This constructor creates a stream descriptor without opening it. The
62 * descriptor needs to be opened and then connected or accepted before data
63 * can be sent or received on it.
65 * @param io_context The io_context object that the stream descriptor will
66 * use to dispatch handlers for any asynchronous operations performed on the
69 explicit basic_stream_descriptor(boost::asio::io_context& io_context)
70 : basic_descriptor<StreamDescriptorService>(io_context)
74 /// Construct a basic_stream_descriptor on an existing native descriptor.
76 * This constructor creates a stream descriptor object to hold an existing
79 * @param io_context The io_context object that the stream descriptor will
80 * use to dispatch handlers for any asynchronous operations performed on the
83 * @param native_descriptor The new underlying descriptor implementation.
85 * @throws boost::system::system_error Thrown on failure.
87 basic_stream_descriptor(boost::asio::io_context& io_context,
88 const native_handle_type& native_descriptor)
89 : basic_descriptor<StreamDescriptorService>(io_context, native_descriptor)
93 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
94 /// Move-construct a basic_stream_descriptor from another.
96 * This constructor moves a stream descriptor from one object to another.
98 * @param other The other basic_stream_descriptor object from which the move
101 * @note Following the move, the moved-from object is in the same state as if
102 * constructed using the @c basic_stream_descriptor(io_context&) constructor.
104 basic_stream_descriptor(basic_stream_descriptor&& other)
105 : basic_descriptor<StreamDescriptorService>(
106 BOOST_ASIO_MOVE_CAST(basic_stream_descriptor)(other))
110 /// Move-assign a basic_stream_descriptor from another.
112 * This assignment operator moves a stream descriptor from one object to
115 * @param other The other basic_stream_descriptor object from which the move
118 * @note Following the move, the moved-from object is in the same state as if
119 * constructed using the @c basic_stream_descriptor(io_context&) constructor.
121 basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
123 basic_descriptor<StreamDescriptorService>::operator=(
124 BOOST_ASIO_MOVE_CAST(basic_stream_descriptor)(other));
127 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
129 /// Write some data to the descriptor.
131 * This function is used to write data to the stream descriptor. The function
132 * call will block until one or more bytes of the data has been written
133 * successfully, or until an error occurs.
135 * @param buffers One or more data buffers to be written to the descriptor.
137 * @returns The number of bytes written.
139 * @throws boost::system::system_error Thrown on failure. An error code of
140 * boost::asio::error::eof indicates that the connection was closed by the
143 * @note The write_some operation may not transmit all of the data to the
144 * peer. Consider using the @ref write function if you need to ensure that
145 * all data is written before the blocking operation completes.
148 * To write a single data buffer use the @ref buffer function as follows:
150 * descriptor.write_some(boost::asio::buffer(data, size));
152 * See the @ref buffer documentation for information on writing multiple
153 * buffers in one go, and how to use it with arrays, boost::array or
156 template <typename ConstBufferSequence>
157 std::size_t write_some(const ConstBufferSequence& buffers)
159 boost::system::error_code ec;
160 std::size_t s = this->get_service().write_some(
161 this->get_implementation(), buffers, ec);
162 boost::asio::detail::throw_error(ec, "write_some");
166 /// Write some data to the descriptor.
168 * This function is used to write data to the stream descriptor. The function
169 * call will block until one or more bytes of the data has been written
170 * successfully, or until an error occurs.
172 * @param buffers One or more data buffers to be written to the descriptor.
174 * @param ec Set to indicate what error occurred, if any.
176 * @returns The number of bytes written. Returns 0 if an error occurred.
178 * @note The write_some operation may not transmit all of the data to the
179 * peer. Consider using the @ref write function if you need to ensure that
180 * all data is written before the blocking operation completes.
182 template <typename ConstBufferSequence>
183 std::size_t write_some(const ConstBufferSequence& buffers,
184 boost::system::error_code& ec)
186 return this->get_service().write_some(
187 this->get_implementation(), buffers, ec);
190 /// Start an asynchronous write.
192 * This function is used to asynchronously write data to the stream
193 * descriptor. The function call always returns immediately.
195 * @param buffers One or more data buffers to be written to the descriptor.
196 * Although the buffers object may be copied as necessary, ownership of the
197 * underlying memory blocks is retained by the caller, which must guarantee
198 * that they remain valid until the handler is called.
200 * @param handler The handler to be called when the write operation completes.
201 * Copies will be made of the handler as required. The function signature of
202 * the handler must be:
203 * @code void handler(
204 * const boost::system::error_code& error, // Result of operation.
205 * std::size_t bytes_transferred // Number of bytes written.
207 * Regardless of whether the asynchronous operation completes immediately or
208 * not, the handler will not be invoked from within this function. Invocation
209 * of the handler will be performed in a manner equivalent to using
210 * boost::asio::io_context::post().
212 * @note The write operation may not transmit all of the data to the peer.
213 * Consider using the @ref async_write function if you need to ensure that all
214 * data is written before the asynchronous operation completes.
217 * To write a single data buffer use the @ref buffer function as follows:
219 * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
221 * See the @ref buffer documentation for information on writing multiple
222 * buffers in one go, and how to use it with arrays, boost::array or
225 template <typename ConstBufferSequence, typename WriteHandler>
226 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
227 void (boost::system::error_code, std::size_t))
228 async_write_some(const ConstBufferSequence& buffers,
229 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
231 // If you get an error on the following line it means that your handler does
232 // not meet the documented type requirements for a WriteHandler.
233 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
235 return this->get_service().async_write_some(this->get_implementation(),
236 buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
239 /// Read some data from the descriptor.
241 * This function is used to read data from the stream descriptor. The function
242 * call will block until one or more bytes of data has been read successfully,
243 * or until an error occurs.
245 * @param buffers One or more buffers into which the data will be read.
247 * @returns The number of bytes read.
249 * @throws boost::system::system_error Thrown on failure. An error code of
250 * boost::asio::error::eof indicates that the connection was closed by the
253 * @note The read_some operation may not read all of the requested number of
254 * bytes. Consider using the @ref read function if you need to ensure that
255 * the requested amount of data is read before the blocking operation
259 * To read into a single data buffer use the @ref buffer function as follows:
261 * descriptor.read_some(boost::asio::buffer(data, size));
263 * See the @ref buffer documentation for information on reading into multiple
264 * buffers in one go, and how to use it with arrays, boost::array or
267 template <typename MutableBufferSequence>
268 std::size_t read_some(const MutableBufferSequence& buffers)
270 boost::system::error_code ec;
271 std::size_t s = this->get_service().read_some(
272 this->get_implementation(), buffers, ec);
273 boost::asio::detail::throw_error(ec, "read_some");
277 /// Read some data from the descriptor.
279 * This function is used to read data from the stream descriptor. The function
280 * call will block until one or more bytes of data has been read successfully,
281 * or until an error occurs.
283 * @param buffers One or more buffers into which the data will be read.
285 * @param ec Set to indicate what error occurred, if any.
287 * @returns The number of bytes read. Returns 0 if an error occurred.
289 * @note The read_some operation may not read all of the requested number of
290 * bytes. Consider using the @ref read function if you need to ensure that
291 * the requested amount of data is read before the blocking operation
294 template <typename MutableBufferSequence>
295 std::size_t read_some(const MutableBufferSequence& buffers,
296 boost::system::error_code& ec)
298 return this->get_service().read_some(
299 this->get_implementation(), buffers, ec);
302 /// Start an asynchronous read.
304 * This function is used to asynchronously read data from the stream
305 * descriptor. The function call always returns immediately.
307 * @param buffers One or more buffers into which the data will be read.
308 * Although the buffers object may be copied as necessary, ownership of the
309 * underlying memory blocks is retained by the caller, which must guarantee
310 * that they remain valid until the handler is called.
312 * @param handler The handler to be called when the read operation completes.
313 * Copies will be made of the handler as required. The function signature of
314 * the handler must be:
315 * @code void handler(
316 * const boost::system::error_code& error, // Result of operation.
317 * std::size_t bytes_transferred // Number of bytes read.
319 * Regardless of whether the asynchronous operation completes immediately or
320 * not, the handler will not be invoked from within this function. Invocation
321 * of the handler will be performed in a manner equivalent to using
322 * boost::asio::io_context::post().
324 * @note The read operation may not read all of the requested number of bytes.
325 * Consider using the @ref async_read function if you need to ensure that the
326 * requested amount of data is read before the asynchronous operation
330 * To read into a single data buffer use the @ref buffer function as follows:
332 * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
334 * See the @ref buffer documentation for information on reading into multiple
335 * buffers in one go, and how to use it with arrays, boost::array or
338 template <typename MutableBufferSequence, typename ReadHandler>
339 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
340 void (boost::system::error_code, std::size_t))
341 async_read_some(const MutableBufferSequence& buffers,
342 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
344 // If you get an error on the following line it means that your handler does
345 // not meet the documented type requirements for a ReadHandler.
346 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
348 return this->get_service().async_read_some(this->get_implementation(),
349 buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
357 #include <boost/asio/detail/pop_options.hpp>
359 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
360 // || defined(GENERATING_DOCUMENTATION)
362 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
364 #endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP