2 // posix/basic_stream_descriptor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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)
28 /// Provides stream-oriented descriptor functionality.
30 * The posix::basic_stream_descriptor class template provides asynchronous and
31 * blocking stream-oriented descriptor functionality.
34 * @e Distinct @e objects: Safe.@n
35 * @e Shared @e objects: Unsafe.
38 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
40 template <typename Executor = executor>
41 class basic_stream_descriptor
42 : public basic_descriptor<Executor>
45 /// The type of the executor associated with the object.
46 typedef Executor executor_type;
48 /// Rebinds the descriptor type to another executor.
49 template <typename Executor1>
50 struct rebind_executor
52 /// The descriptor type when rebound to the specified executor.
53 typedef basic_stream_descriptor<Executor1> other;
56 /// The native representation of a descriptor.
57 typedef typename basic_descriptor<Executor>::native_handle_type
60 /// Construct a stream descriptor without opening it.
62 * This constructor creates a stream descriptor without opening it. The
63 * descriptor needs to be opened and then connected or accepted before data
64 * can be sent or received on it.
66 * @param ex The I/O executor that the descriptor will use, by default, to
67 * dispatch handlers for any asynchronous operations performed on the
70 explicit basic_stream_descriptor(const executor_type& ex)
71 : basic_descriptor<Executor>(ex)
75 /// Construct a stream descriptor without opening it.
77 * This constructor creates a stream descriptor without opening it. The
78 * descriptor needs to be opened and then connected or accepted before data
79 * can be sent or received on it.
81 * @param context An execution context which provides the I/O executor that
82 * the descriptor will use, by default, to dispatch handlers for any
83 * asynchronous operations performed on the descriptor.
85 template <typename ExecutionContext>
86 explicit basic_stream_descriptor(ExecutionContext& context,
88 is_convertible<ExecutionContext&, execution_context&>::value
90 : basic_descriptor<Executor>(context)
94 /// Construct a stream descriptor on an existing native descriptor.
96 * This constructor creates a stream descriptor object to hold an existing
99 * @param ex The I/O executor that the descriptor will use, by default, to
100 * dispatch handlers for any asynchronous operations performed on the
103 * @param native_descriptor The new underlying descriptor implementation.
105 * @throws boost::system::system_error Thrown on failure.
107 basic_stream_descriptor(const executor_type& ex,
108 const native_handle_type& native_descriptor)
109 : basic_descriptor<Executor>(ex, native_descriptor)
113 /// Construct a stream descriptor on an existing native descriptor.
115 * This constructor creates a stream descriptor object to hold an existing
118 * @param context An execution context which provides the I/O executor that
119 * the descriptor will use, by default, to dispatch handlers for any
120 * asynchronous operations performed on the descriptor.
122 * @param native_descriptor The new underlying descriptor implementation.
124 * @throws boost::system::system_error Thrown on failure.
126 template <typename ExecutionContext>
127 basic_stream_descriptor(ExecutionContext& context,
128 const native_handle_type& native_descriptor,
130 is_convertible<ExecutionContext&, execution_context&>::value
132 : basic_descriptor<Executor>(context, native_descriptor)
136 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
137 /// Move-construct a stream descriptor from another.
139 * This constructor moves a stream descriptor from one object to another.
141 * @param other The other stream descriptor object from which the move
144 * @note Following the move, the moved-from object is in the same state as if
145 * constructed using the @c basic_stream_descriptor(const executor_type&)
148 basic_stream_descriptor(basic_stream_descriptor&& other)
149 : descriptor(std::move(other))
153 /// Move-assign a stream descriptor from another.
155 * This assignment operator moves a stream descriptor from one object to
158 * @param other The other stream descriptor object from which the move
161 * @note Following the move, the moved-from object is in the same state as if
162 * constructed using the @c basic_stream_descriptor(const executor_type&)
165 basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
167 descriptor::operator=(std::move(other));
170 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
172 /// Write some data to the descriptor.
174 * This function is used to write data to the stream descriptor. The function
175 * call will block until one or more bytes of the data has been written
176 * successfully, or until an error occurs.
178 * @param buffers One or more data buffers to be written to the descriptor.
180 * @returns The number of bytes written.
182 * @throws boost::system::system_error Thrown on failure. An error code of
183 * boost::asio::error::eof indicates that the connection was closed by the
186 * @note The write_some operation may not transmit all of the data to the
187 * peer. Consider using the @ref write function if you need to ensure that
188 * all data is written before the blocking operation completes.
191 * To write a single data buffer use the @ref buffer function as follows:
193 * descriptor.write_some(boost::asio::buffer(data, size));
195 * See the @ref buffer documentation for information on writing multiple
196 * buffers in one go, and how to use it with arrays, boost::array or
199 template <typename ConstBufferSequence>
200 std::size_t write_some(const ConstBufferSequence& buffers)
202 boost::system::error_code ec;
203 std::size_t s = this->impl_.get_service().write_some(
204 this->impl_.get_implementation(), buffers, ec);
205 boost::asio::detail::throw_error(ec, "write_some");
209 /// Write some data to the descriptor.
211 * This function is used to write data to the stream descriptor. The function
212 * call will block until one or more bytes of the data has been written
213 * successfully, or until an error occurs.
215 * @param buffers One or more data buffers to be written to the descriptor.
217 * @param ec Set to indicate what error occurred, if any.
219 * @returns The number of bytes written. Returns 0 if an error occurred.
221 * @note The write_some operation may not transmit all of the data to the
222 * peer. Consider using the @ref write function if you need to ensure that
223 * all data is written before the blocking operation completes.
225 template <typename ConstBufferSequence>
226 std::size_t write_some(const ConstBufferSequence& buffers,
227 boost::system::error_code& ec)
229 return this->impl_.get_service().write_some(
230 this->impl_.get_implementation(), buffers, ec);
233 /// Start an asynchronous write.
235 * This function is used to asynchronously write data to the stream
236 * descriptor. The function call always returns immediately.
238 * @param buffers One or more data buffers to be written to the descriptor.
239 * Although the buffers object may be copied as necessary, ownership of the
240 * underlying memory blocks is retained by the caller, which must guarantee
241 * that they remain valid until the handler is called.
243 * @param handler The handler to be called when the write operation completes.
244 * Copies will be made of the handler as required. The function signature of
245 * the handler must be:
246 * @code void handler(
247 * const boost::system::error_code& error, // Result of operation.
248 * std::size_t bytes_transferred // Number of bytes written.
250 * Regardless of whether the asynchronous operation completes immediately or
251 * not, the handler will not be invoked from within this function. On
252 * immediate completion, invocation of the handler will be performed in a
253 * manner equivalent to using boost::asio::post().
255 * @note The write operation may not transmit all of the data to the peer.
256 * Consider using the @ref async_write function if you need to ensure that all
257 * data is written before the asynchronous operation completes.
260 * To write a single data buffer use the @ref buffer function as follows:
262 * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
264 * See the @ref buffer documentation for information on writing multiple
265 * buffers in one go, and how to use it with arrays, boost::array or
268 template <typename ConstBufferSequence,
269 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
270 std::size_t)) WriteHandler
271 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
272 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
273 void (boost::system::error_code, std::size_t))
274 async_write_some(const ConstBufferSequence& buffers,
275 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
276 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
278 return async_initiate<WriteHandler,
279 void (boost::system::error_code, std::size_t)>(
280 initiate_async_write_some(this), handler, buffers);
283 /// Read some data from the descriptor.
285 * This function is used to read data from the stream descriptor. The function
286 * call will block until one or more bytes of data has been read successfully,
287 * or until an error occurs.
289 * @param buffers One or more buffers into which the data will be read.
291 * @returns The number of bytes read.
293 * @throws boost::system::system_error Thrown on failure. An error code of
294 * boost::asio::error::eof indicates that the connection was closed by the
297 * @note The read_some operation may not read all of the requested number of
298 * bytes. Consider using the @ref read function if you need to ensure that
299 * the requested amount of data is read before the blocking operation
303 * To read into a single data buffer use the @ref buffer function as follows:
305 * descriptor.read_some(boost::asio::buffer(data, size));
307 * See the @ref buffer documentation for information on reading into multiple
308 * buffers in one go, and how to use it with arrays, boost::array or
311 template <typename MutableBufferSequence>
312 std::size_t read_some(const MutableBufferSequence& buffers)
314 boost::system::error_code ec;
315 std::size_t s = this->impl_.get_service().read_some(
316 this->impl_.get_implementation(), buffers, ec);
317 boost::asio::detail::throw_error(ec, "read_some");
321 /// Read some data from the descriptor.
323 * This function is used to read data from the stream descriptor. The function
324 * call will block until one or more bytes of data has been read successfully,
325 * or until an error occurs.
327 * @param buffers One or more buffers into which the data will be read.
329 * @param ec Set to indicate what error occurred, if any.
331 * @returns The number of bytes read. Returns 0 if an error occurred.
333 * @note The read_some operation may not read all of the requested number of
334 * bytes. Consider using the @ref read function if you need to ensure that
335 * the requested amount of data is read before the blocking operation
338 template <typename MutableBufferSequence>
339 std::size_t read_some(const MutableBufferSequence& buffers,
340 boost::system::error_code& ec)
342 return this->impl_.get_service().read_some(
343 this->impl_.get_implementation(), buffers, ec);
346 /// Start an asynchronous read.
348 * This function is used to asynchronously read data from the stream
349 * descriptor. The function call always returns immediately.
351 * @param buffers One or more buffers into which the data will be read.
352 * Although the buffers object may be copied as necessary, ownership of the
353 * underlying memory blocks is retained by the caller, which must guarantee
354 * that they remain valid until the handler is called.
356 * @param handler The handler to be called when the read operation completes.
357 * Copies will be made of the handler as required. The function signature of
358 * the handler must be:
359 * @code void handler(
360 * const boost::system::error_code& error, // Result of operation.
361 * std::size_t bytes_transferred // Number of bytes read.
363 * Regardless of whether the asynchronous operation completes immediately or
364 * not, the handler will not be invoked from within this function. On
365 * immediate completion, invocation of the handler will be performed in a
366 * manner equivalent to using boost::asio::post().
368 * @note The read operation may not read all of the requested number of bytes.
369 * Consider using the @ref async_read function if you need to ensure that the
370 * requested amount of data is read before the asynchronous operation
374 * To read into a single data buffer use the @ref buffer function as follows:
376 * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
378 * See the @ref buffer documentation for information on reading into multiple
379 * buffers in one go, and how to use it with arrays, boost::array or
382 template <typename MutableBufferSequence,
383 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
384 std::size_t)) ReadHandler
385 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
386 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
387 void (boost::system::error_code, std::size_t))
388 async_read_some(const MutableBufferSequence& buffers,
389 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
390 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
392 return async_initiate<ReadHandler,
393 void (boost::system::error_code, std::size_t)>(
394 initiate_async_read_some(this), handler, buffers);
398 class initiate_async_write_some
401 typedef Executor executor_type;
403 explicit initiate_async_write_some(basic_stream_descriptor* self)
408 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
410 return self_->get_executor();
413 template <typename WriteHandler, typename ConstBufferSequence>
414 void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
415 const ConstBufferSequence& buffers) const
417 // If you get an error on the following line it means that your handler
418 // does not meet the documented type requirements for a WriteHandler.
419 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
421 detail::non_const_lvalue<WriteHandler> handler2(handler);
422 self_->impl_.get_service().async_write_some(
423 self_->impl_.get_implementation(), buffers, handler2.value,
424 self_->impl_.get_implementation_executor());
428 basic_stream_descriptor* self_;
431 class initiate_async_read_some
434 typedef Executor executor_type;
436 explicit initiate_async_read_some(basic_stream_descriptor* self)
441 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
443 return self_->get_executor();
446 template <typename ReadHandler, typename MutableBufferSequence>
447 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
448 const MutableBufferSequence& buffers) const
450 // If you get an error on the following line it means that your handler
451 // does not meet the documented type requirements for a ReadHandler.
452 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
454 detail::non_const_lvalue<ReadHandler> handler2(handler);
455 self_->impl_.get_service().async_read_some(
456 self_->impl_.get_implementation(), buffers, handler2.value,
457 self_->impl_.get_implementation_executor());
461 basic_stream_descriptor* self_;
469 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
470 // || defined(GENERATING_DOCUMENTATION)
472 #endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP