2 // posix/stream_descriptor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_HPP
12 #define BOOST_ASIO_POSIX_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 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
25 # include <boost/asio/posix/basic_stream_descriptor.hpp>
26 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
32 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
33 // Typedef for the typical usage of a stream-oriented descriptor.
34 typedef basic_stream_descriptor<> stream_descriptor;
35 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
36 /// Provides stream-oriented descriptor functionality.
38 * The posix::stream_descriptor class template provides asynchronous and
39 * blocking stream-oriented descriptor functionality.
42 * @e Distinct @e objects: Safe.@n
43 * @e Shared @e objects: Unsafe.
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
48 class stream_descriptor
52 /// Construct a stream_descriptor without opening it.
54 * This constructor creates a stream descriptor without opening it. The
55 * descriptor needs to be opened and then connected or accepted before data
56 * can be sent or received on it.
58 * @param io_context The io_context object that the stream descriptor will
59 * use to dispatch handlers for any asynchronous operations performed on the
62 explicit stream_descriptor(boost::asio::io_context& io_context)
63 : descriptor(io_context)
67 /// Construct a stream_descriptor on an existing native descriptor.
69 * This constructor creates a stream descriptor object to hold an existing
72 * @param io_context The io_context object that the stream descriptor will
73 * use to dispatch handlers for any asynchronous operations performed on the
76 * @param native_descriptor The new underlying descriptor implementation.
78 * @throws boost::system::system_error Thrown on failure.
80 stream_descriptor(boost::asio::io_context& io_context,
81 const native_handle_type& native_descriptor)
82 : descriptor(io_context, native_descriptor)
86 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
87 /// Move-construct a stream_descriptor from another.
89 * This constructor moves a stream descriptor from one object to another.
91 * @param other The other stream_descriptor object from which the move
94 * @note Following the move, the moved-from object is in the same state as if
95 * constructed using the @c stream_descriptor(io_context&) constructor.
97 stream_descriptor(stream_descriptor&& other)
98 : descriptor(std::move(other))
102 /// Move-assign a stream_descriptor from another.
104 * This assignment operator moves a stream descriptor from one object to
107 * @param other The other stream_descriptor object from which the move
110 * @note Following the move, the moved-from object is in the same state as if
111 * constructed using the @c stream_descriptor(io_context&) constructor.
113 stream_descriptor& operator=(stream_descriptor&& other)
115 descriptor::operator=(std::move(other));
118 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
120 /// Write some data to the descriptor.
122 * This function is used to write data to the stream descriptor. The function
123 * call will block until one or more bytes of the data has been written
124 * successfully, or until an error occurs.
126 * @param buffers One or more data buffers to be written to the descriptor.
128 * @returns The number of bytes written.
130 * @throws boost::system::system_error Thrown on failure. An error code of
131 * boost::asio::error::eof indicates that the connection was closed by the
134 * @note The write_some operation may not transmit all of the data to the
135 * peer. Consider using the @ref write function if you need to ensure that
136 * all data is written before the blocking operation completes.
139 * To write a single data buffer use the @ref buffer function as follows:
141 * descriptor.write_some(boost::asio::buffer(data, size));
143 * See the @ref buffer documentation for information on writing multiple
144 * buffers in one go, and how to use it with arrays, boost::array or
147 template <typename ConstBufferSequence>
148 std::size_t write_some(const ConstBufferSequence& buffers)
150 boost::system::error_code ec;
151 std::size_t s = this->get_service().write_some(
152 this->get_implementation(), buffers, ec);
153 boost::asio::detail::throw_error(ec, "write_some");
157 /// Write some data to the descriptor.
159 * This function is used to write data to the stream descriptor. The function
160 * call will block until one or more bytes of the data has been written
161 * successfully, or until an error occurs.
163 * @param buffers One or more data buffers to be written to the descriptor.
165 * @param ec Set to indicate what error occurred, if any.
167 * @returns The number of bytes written. Returns 0 if an error occurred.
169 * @note The write_some operation may not transmit all of the data to the
170 * peer. Consider using the @ref write function if you need to ensure that
171 * all data is written before the blocking operation completes.
173 template <typename ConstBufferSequence>
174 std::size_t write_some(const ConstBufferSequence& buffers,
175 boost::system::error_code& ec)
177 return this->get_service().write_some(
178 this->get_implementation(), buffers, ec);
181 /// Start an asynchronous write.
183 * This function is used to asynchronously write data to the stream
184 * descriptor. The function call always returns immediately.
186 * @param buffers One or more data buffers to be written to the descriptor.
187 * Although the buffers object may be copied as necessary, ownership of the
188 * underlying memory blocks is retained by the caller, which must guarantee
189 * that they remain valid until the handler is called.
191 * @param handler The handler to be called when the write operation completes.
192 * Copies will be made of the handler as required. The function signature of
193 * the handler must be:
194 * @code void handler(
195 * const boost::system::error_code& error, // Result of operation.
196 * std::size_t bytes_transferred // Number of bytes written.
198 * Regardless of whether the asynchronous operation completes immediately or
199 * not, the handler will not be invoked from within this function. Invocation
200 * of the handler will be performed in a manner equivalent to using
201 * boost::asio::io_context::post().
203 * @note The write operation may not transmit all of the data to the peer.
204 * Consider using the @ref async_write function if you need to ensure that all
205 * data is written before the asynchronous operation completes.
208 * To write a single data buffer use the @ref buffer function as follows:
210 * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
212 * See the @ref buffer documentation for information on writing multiple
213 * buffers in one go, and how to use it with arrays, boost::array or
216 template <typename ConstBufferSequence, typename WriteHandler>
217 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
218 void (boost::system::error_code, std::size_t))
219 async_write_some(const ConstBufferSequence& buffers,
220 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
222 // If you get an error on the following line it means that your handler does
223 // not meet the documented type requirements for a WriteHandler.
224 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
226 boost::asio::async_completion<WriteHandler,
227 void (boost::system::error_code, std::size_t)> init(handler);
229 this->get_service().async_write_some(
230 this->get_implementation(), buffers, init.completion_handler);
232 return init.result.get();
235 /// Read some data from the descriptor.
237 * This function is used to read data from the stream descriptor. The function
238 * call will block until one or more bytes of data has been read successfully,
239 * or until an error occurs.
241 * @param buffers One or more buffers into which the data will be read.
243 * @returns The number of bytes read.
245 * @throws boost::system::system_error Thrown on failure. An error code of
246 * boost::asio::error::eof indicates that the connection was closed by the
249 * @note The read_some operation may not read all of the requested number of
250 * bytes. Consider using the @ref read function if you need to ensure that
251 * the requested amount of data is read before the blocking operation
255 * To read into a single data buffer use the @ref buffer function as follows:
257 * descriptor.read_some(boost::asio::buffer(data, size));
259 * See the @ref buffer documentation for information on reading into multiple
260 * buffers in one go, and how to use it with arrays, boost::array or
263 template <typename MutableBufferSequence>
264 std::size_t read_some(const MutableBufferSequence& buffers)
266 boost::system::error_code ec;
267 std::size_t s = this->get_service().read_some(
268 this->get_implementation(), buffers, ec);
269 boost::asio::detail::throw_error(ec, "read_some");
273 /// Read some data from the descriptor.
275 * This function is used to read data from the stream descriptor. The function
276 * call will block until one or more bytes of data has been read successfully,
277 * or until an error occurs.
279 * @param buffers One or more buffers into which the data will be read.
281 * @param ec Set to indicate what error occurred, if any.
283 * @returns The number of bytes read. Returns 0 if an error occurred.
285 * @note The read_some operation may not read all of the requested number of
286 * bytes. Consider using the @ref read function if you need to ensure that
287 * the requested amount of data is read before the blocking operation
290 template <typename MutableBufferSequence>
291 std::size_t read_some(const MutableBufferSequence& buffers,
292 boost::system::error_code& ec)
294 return this->get_service().read_some(
295 this->get_implementation(), buffers, ec);
298 /// Start an asynchronous read.
300 * This function is used to asynchronously read data from the stream
301 * descriptor. The function call always returns immediately.
303 * @param buffers One or more buffers into which the data will be read.
304 * Although the buffers object may be copied as necessary, ownership of the
305 * underlying memory blocks is retained by the caller, which must guarantee
306 * that they remain valid until the handler is called.
308 * @param handler The handler to be called when the read operation completes.
309 * Copies will be made of the handler as required. The function signature of
310 * the handler must be:
311 * @code void handler(
312 * const boost::system::error_code& error, // Result of operation.
313 * std::size_t bytes_transferred // Number of bytes read.
315 * Regardless of whether the asynchronous operation completes immediately or
316 * not, the handler will not be invoked from within this function. Invocation
317 * of the handler will be performed in a manner equivalent to using
318 * boost::asio::io_context::post().
320 * @note The read operation may not read all of the requested number of bytes.
321 * Consider using the @ref async_read function if you need to ensure that the
322 * requested amount of data is read before the asynchronous operation
326 * To read into a single data buffer use the @ref buffer function as follows:
328 * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
330 * See the @ref buffer documentation for information on reading into multiple
331 * buffers in one go, and how to use it with arrays, boost::array or
334 template <typename MutableBufferSequence, typename ReadHandler>
335 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
336 void (boost::system::error_code, std::size_t))
337 async_read_some(const MutableBufferSequence& buffers,
338 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
340 // If you get an error on the following line it means that your handler does
341 // not meet the documented type requirements for a ReadHandler.
342 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
344 boost::asio::async_completion<ReadHandler,
345 void (boost::system::error_code, std::size_t)> init(handler);
347 this->get_service().async_read_some(
348 this->get_implementation(), buffers, init.completion_handler);
350 return init.result.get();
353 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
359 #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
360 // || defined(GENERATING_DOCUMENTATION)
362 #endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_HPP