2 // posix/basic_stream_descriptor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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>
19 #include <boost/asio/posix/descriptor.hpp>
21 #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
22 || defined(GENERATING_DOCUMENTATION)
24 #include <boost/asio/detail/push_options.hpp>
30 /// Provides stream-oriented descriptor functionality.
32 * The posix::basic_stream_descriptor class template provides asynchronous and
33 * blocking stream-oriented descriptor functionality.
36 * @e Distinct @e objects: Safe.@n
37 * @e Shared @e objects: Unsafe.
40 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
42 template <typename Executor = any_io_executor>
43 class basic_stream_descriptor
44 : public basic_descriptor<Executor>
47 /// The type of the executor associated with the object.
48 typedef Executor executor_type;
50 /// Rebinds the descriptor type to another executor.
51 template <typename Executor1>
52 struct rebind_executor
54 /// The descriptor type when rebound to the specified executor.
55 typedef basic_stream_descriptor<Executor1> other;
58 /// The native representation of a descriptor.
59 typedef typename basic_descriptor<Executor>::native_handle_type
62 /// Construct a stream descriptor without opening it.
64 * This constructor creates a stream descriptor without opening it. The
65 * descriptor needs to be opened and then connected or accepted before data
66 * can be sent or received on it.
68 * @param ex The I/O executor that the descriptor will use, by default, to
69 * dispatch handlers for any asynchronous operations performed on the
72 explicit basic_stream_descriptor(const executor_type& ex)
73 : basic_descriptor<Executor>(ex)
77 /// Construct a stream descriptor without opening it.
79 * This constructor creates a stream descriptor without opening it. The
80 * descriptor needs to be opened and then connected or accepted before data
81 * can be sent or received on it.
83 * @param context An execution context which provides the I/O executor that
84 * the descriptor will use, by default, to dispatch handlers for any
85 * asynchronous operations performed on the descriptor.
87 template <typename ExecutionContext>
88 explicit basic_stream_descriptor(ExecutionContext& context,
90 is_convertible<ExecutionContext&, execution_context&>::value
92 : basic_descriptor<Executor>(context)
96 /// Construct a stream descriptor on an existing native descriptor.
98 * This constructor creates a stream descriptor object to hold an existing
101 * @param ex The I/O executor that the descriptor will use, by default, to
102 * dispatch handlers for any asynchronous operations performed on the
105 * @param native_descriptor The new underlying descriptor implementation.
107 * @throws boost::system::system_error Thrown on failure.
109 basic_stream_descriptor(const executor_type& ex,
110 const native_handle_type& native_descriptor)
111 : basic_descriptor<Executor>(ex, native_descriptor)
115 /// Construct a stream descriptor on an existing native descriptor.
117 * This constructor creates a stream descriptor object to hold an existing
120 * @param context An execution context which provides the I/O executor that
121 * the descriptor will use, by default, to dispatch handlers for any
122 * asynchronous operations performed on the descriptor.
124 * @param native_descriptor The new underlying descriptor implementation.
126 * @throws boost::system::system_error Thrown on failure.
128 template <typename ExecutionContext>
129 basic_stream_descriptor(ExecutionContext& context,
130 const native_handle_type& native_descriptor,
132 is_convertible<ExecutionContext&, execution_context&>::value
134 : basic_descriptor<Executor>(context, native_descriptor)
138 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
139 /// Move-construct a stream descriptor from another.
141 * This constructor moves a stream descriptor from one object to another.
143 * @param other The other stream descriptor object from which the move
146 * @note Following the move, the moved-from object is in the same state as if
147 * constructed using the @c basic_stream_descriptor(const executor_type&)
150 basic_stream_descriptor(basic_stream_descriptor&& other) BOOST_ASIO_NOEXCEPT
151 : descriptor(std::move(other))
155 /// Move-assign a stream descriptor from another.
157 * This assignment operator moves a stream descriptor from one object to
160 * @param other The other stream descriptor object from which the move
163 * @note Following the move, the moved-from object is in the same state as if
164 * constructed using the @c basic_stream_descriptor(const executor_type&)
167 basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
169 descriptor::operator=(std::move(other));
172 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
174 /// Write some data to the descriptor.
176 * This function is used to write data to the stream descriptor. The function
177 * call will block until one or more bytes of the data has been written
178 * successfully, or until an error occurs.
180 * @param buffers One or more data buffers to be written to the descriptor.
182 * @returns The number of bytes written.
184 * @throws boost::system::system_error Thrown on failure. An error code of
185 * boost::asio::error::eof indicates that the connection was closed by the
188 * @note The write_some operation may not transmit all of the data to the
189 * peer. Consider using the @ref write function if you need to ensure that
190 * all data is written before the blocking operation completes.
193 * To write a single data buffer use the @ref buffer function as follows:
195 * descriptor.write_some(boost::asio::buffer(data, size));
197 * See the @ref buffer documentation for information on writing multiple
198 * buffers in one go, and how to use it with arrays, boost::array or
201 template <typename ConstBufferSequence>
202 std::size_t write_some(const ConstBufferSequence& buffers)
204 boost::system::error_code ec;
205 std::size_t s = this->impl_.get_service().write_some(
206 this->impl_.get_implementation(), buffers, ec);
207 boost::asio::detail::throw_error(ec, "write_some");
211 /// Write some data to the descriptor.
213 * This function is used to write data to the stream descriptor. The function
214 * call will block until one or more bytes of the data has been written
215 * successfully, or until an error occurs.
217 * @param buffers One or more data buffers to be written to the descriptor.
219 * @param ec Set to indicate what error occurred, if any.
221 * @returns The number of bytes written. Returns 0 if an error occurred.
223 * @note The write_some operation may not transmit all of the data to the
224 * peer. Consider using the @ref write function if you need to ensure that
225 * all data is written before the blocking operation completes.
227 template <typename ConstBufferSequence>
228 std::size_t write_some(const ConstBufferSequence& buffers,
229 boost::system::error_code& ec)
231 return this->impl_.get_service().write_some(
232 this->impl_.get_implementation(), buffers, ec);
235 /// Start an asynchronous write.
237 * This function is used to asynchronously write data to the stream
238 * descriptor. The function call always returns immediately.
240 * @param buffers One or more data buffers to be written to the descriptor.
241 * Although the buffers object may be copied as necessary, ownership of the
242 * underlying memory blocks is retained by the caller, which must guarantee
243 * that they remain valid until the handler is called.
245 * @param handler The handler to be called when the write operation completes.
246 * Copies will be made of the handler as required. The function signature of
247 * the handler must be:
248 * @code void handler(
249 * const boost::system::error_code& error, // Result of operation.
250 * std::size_t bytes_transferred // Number of bytes written.
252 * Regardless of whether the asynchronous operation completes immediately or
253 * not, the handler will not be invoked from within this function. On
254 * immediate completion, invocation of the handler will be performed in a
255 * manner equivalent to using boost::asio::post().
257 * @note The write operation may not transmit all of the data to the peer.
258 * Consider using the @ref async_write function if you need to ensure that all
259 * data is written before the asynchronous operation completes.
262 * To write a single data buffer use the @ref buffer function as follows:
264 * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
266 * See the @ref buffer documentation for information on writing multiple
267 * buffers in one go, and how to use it with arrays, boost::array or
270 template <typename ConstBufferSequence,
271 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
272 std::size_t)) WriteHandler
273 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
274 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
275 void (boost::system::error_code, std::size_t))
276 async_write_some(const ConstBufferSequence& buffers,
277 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
278 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
280 return async_initiate<WriteHandler,
281 void (boost::system::error_code, std::size_t)>(
282 initiate_async_write_some(this), handler, buffers);
285 /// Read some data from the descriptor.
287 * This function is used to read data from the stream descriptor. The function
288 * call will block until one or more bytes of data has been read successfully,
289 * or until an error occurs.
291 * @param buffers One or more buffers into which the data will be read.
293 * @returns The number of bytes read.
295 * @throws boost::system::system_error Thrown on failure. An error code of
296 * boost::asio::error::eof indicates that the connection was closed by the
299 * @note The read_some operation may not read all of the requested number of
300 * bytes. Consider using the @ref read function if you need to ensure that
301 * the requested amount of data is read before the blocking operation
305 * To read into a single data buffer use the @ref buffer function as follows:
307 * descriptor.read_some(boost::asio::buffer(data, size));
309 * See the @ref buffer documentation for information on reading into multiple
310 * buffers in one go, and how to use it with arrays, boost::array or
313 template <typename MutableBufferSequence>
314 std::size_t read_some(const MutableBufferSequence& buffers)
316 boost::system::error_code ec;
317 std::size_t s = this->impl_.get_service().read_some(
318 this->impl_.get_implementation(), buffers, ec);
319 boost::asio::detail::throw_error(ec, "read_some");
323 /// Read some data from the descriptor.
325 * This function is used to read data from the stream descriptor. The function
326 * call will block until one or more bytes of data has been read successfully,
327 * or until an error occurs.
329 * @param buffers One or more buffers into which the data will be read.
331 * @param ec Set to indicate what error occurred, if any.
333 * @returns The number of bytes read. Returns 0 if an error occurred.
335 * @note The read_some operation may not read all of the requested number of
336 * bytes. Consider using the @ref read function if you need to ensure that
337 * the requested amount of data is read before the blocking operation
340 template <typename MutableBufferSequence>
341 std::size_t read_some(const MutableBufferSequence& buffers,
342 boost::system::error_code& ec)
344 return this->impl_.get_service().read_some(
345 this->impl_.get_implementation(), buffers, ec);
348 /// Start an asynchronous read.
350 * This function is used to asynchronously read data from the stream
351 * descriptor. The function call always returns immediately.
353 * @param buffers One or more buffers into which the data will be read.
354 * Although the buffers object may be copied as necessary, ownership of the
355 * underlying memory blocks is retained by the caller, which must guarantee
356 * that they remain valid until the handler is called.
358 * @param handler The handler to be called when the read operation completes.
359 * Copies will be made of the handler as required. The function signature of
360 * the handler must be:
361 * @code void handler(
362 * const boost::system::error_code& error, // Result of operation.
363 * std::size_t bytes_transferred // Number of bytes read.
365 * Regardless of whether the asynchronous operation completes immediately or
366 * not, the handler will not be invoked from within this function. On
367 * immediate completion, invocation of the handler will be performed in a
368 * manner equivalent to using boost::asio::post().
370 * @note The read operation may not read all of the requested number of bytes.
371 * Consider using the @ref async_read function if you need to ensure that the
372 * requested amount of data is read before the asynchronous operation
376 * To read into a single data buffer use the @ref buffer function as follows:
378 * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
380 * See the @ref buffer documentation for information on reading into multiple
381 * buffers in one go, and how to use it with arrays, boost::array or
384 template <typename MutableBufferSequence,
385 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
386 std::size_t)) ReadHandler
387 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
388 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
389 void (boost::system::error_code, std::size_t))
390 async_read_some(const MutableBufferSequence& buffers,
391 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
392 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
394 return async_initiate<ReadHandler,
395 void (boost::system::error_code, std::size_t)>(
396 initiate_async_read_some(this), handler, buffers);
400 class initiate_async_write_some
403 typedef Executor executor_type;
405 explicit initiate_async_write_some(basic_stream_descriptor* self)
410 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
412 return self_->get_executor();
415 template <typename WriteHandler, typename ConstBufferSequence>
416 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
417 const ConstBufferSequence& buffers) const
419 // If you get an error on the following line it means that your handler
420 // does not meet the documented type requirements for a WriteHandler.
421 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
423 detail::non_const_lvalue<WriteHandler> handler2(handler);
424 self_->impl_.get_service().async_write_some(
425 self_->impl_.get_implementation(), buffers,
426 handler2.value, self_->impl_.get_executor());
430 basic_stream_descriptor* self_;
433 class initiate_async_read_some
436 typedef Executor executor_type;
438 explicit initiate_async_read_some(basic_stream_descriptor* self)
443 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
445 return self_->get_executor();
448 template <typename ReadHandler, typename MutableBufferSequence>
449 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
450 const MutableBufferSequence& buffers) const
452 // If you get an error on the following line it means that your handler
453 // does not meet the documented type requirements for a ReadHandler.
454 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
456 detail::non_const_lvalue<ReadHandler> handler2(handler);
457 self_->impl_.get_service().async_read_some(
458 self_->impl_.get_implementation(), buffers,
459 handler2.value, self_->impl_.get_executor());
463 basic_stream_descriptor* self_;
471 #include <boost/asio/detail/pop_options.hpp>
473 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
474 // || defined(GENERATING_DOCUMENTATION)
476 #endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP