2 // windows/basic_stream_handle.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_WINDOWS_BASIC_STREAM_HANDLE_HPP
12 #define BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_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_WINDOWS_STREAM_HANDLE) \
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/windows/basic_handle.hpp>
30 #include <boost/asio/windows/stream_handle_service.hpp>
32 #include <boost/asio/detail/push_options.hpp>
38 /// Provides stream-oriented handle functionality.
40 * The windows::basic_stream_handle class template provides asynchronous and
41 * blocking stream-oriented handle functionality.
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
48 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
50 template <typename StreamHandleService = stream_handle_service>
51 class basic_stream_handle
52 : public basic_handle<StreamHandleService>
55 /// The native representation of a handle.
56 typedef typename StreamHandleService::native_handle_type native_handle_type;
58 /// Construct a basic_stream_handle without opening it.
60 * This constructor creates a stream handle without opening it. The handle
61 * needs to be opened and then connected or accepted before data can be sent
64 * @param io_context The io_context object that the stream handle will use to
65 * dispatch handlers for any asynchronous operations performed on the handle.
67 explicit basic_stream_handle(boost::asio::io_context& io_context)
68 : basic_handle<StreamHandleService>(io_context)
72 /// Construct a basic_stream_handle on an existing native handle.
74 * This constructor creates a stream handle object to hold an existing native
77 * @param io_context The io_context object that the stream handle will use to
78 * dispatch handlers for any asynchronous operations performed on the handle.
80 * @param handle The new underlying handle implementation.
82 * @throws boost::system::system_error Thrown on failure.
84 basic_stream_handle(boost::asio::io_context& io_context,
85 const native_handle_type& handle)
86 : basic_handle<StreamHandleService>(io_context, handle)
90 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
91 /// Move-construct a basic_stream_handle from another.
93 * This constructor moves a stream handle from one object to another.
95 * @param other The other basic_stream_handle object from which the move
98 * @note Following the move, the moved-from object is in the same state as if
99 * constructed using the @c basic_stream_handle(io_context&) constructor.
101 basic_stream_handle(basic_stream_handle&& other)
102 : basic_handle<StreamHandleService>(
103 BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other))
107 /// Move-assign a basic_stream_handle from another.
109 * This assignment operator moves a stream handle from one object to
112 * @param other The other basic_stream_handle object from which the move
115 * @note Following the move, the moved-from object is in the same state as if
116 * constructed using the @c basic_stream_handle(io_context&) constructor.
118 basic_stream_handle& operator=(basic_stream_handle&& other)
120 basic_handle<StreamHandleService>::operator=(
121 BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other));
124 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
126 /// Write some data to the handle.
128 * This function is used to write data to the stream handle. The function call
129 * will block until one or more bytes of the data has been written
130 * successfully, or until an error occurs.
132 * @param buffers One or more data buffers to be written to the handle.
134 * @returns The number of bytes written.
136 * @throws boost::system::system_error Thrown on failure. An error code of
137 * boost::asio::error::eof indicates that the connection was closed by the
140 * @note The write_some operation may not transmit all of the data to the
141 * peer. Consider using the @ref write function if you need to ensure that
142 * all data is written before the blocking operation completes.
145 * To write a single data buffer use the @ref buffer function as follows:
147 * handle.write_some(boost::asio::buffer(data, size));
149 * See the @ref buffer documentation for information on writing multiple
150 * buffers in one go, and how to use it with arrays, boost::array or
153 template <typename ConstBufferSequence>
154 std::size_t write_some(const ConstBufferSequence& buffers)
156 boost::system::error_code ec;
157 std::size_t s = this->get_service().write_some(
158 this->get_implementation(), buffers, ec);
159 boost::asio::detail::throw_error(ec, "write_some");
163 /// Write some data to the handle.
165 * This function is used to write data to the stream handle. The function call
166 * will block until one or more bytes of the data has been written
167 * successfully, or until an error occurs.
169 * @param buffers One or more data buffers to be written to the handle.
171 * @param ec Set to indicate what error occurred, if any.
173 * @returns The number of bytes written. Returns 0 if an error occurred.
175 * @note The write_some operation may not transmit all of the data to the
176 * peer. Consider using the @ref write function if you need to ensure that
177 * all data is written before the blocking operation completes.
179 template <typename ConstBufferSequence>
180 std::size_t write_some(const ConstBufferSequence& buffers,
181 boost::system::error_code& ec)
183 return this->get_service().write_some(
184 this->get_implementation(), buffers, ec);
187 /// Start an asynchronous write.
189 * This function is used to asynchronously write data to the stream handle.
190 * The function call always returns immediately.
192 * @param buffers One or more data buffers to be written to the handle.
193 * Although the buffers object may be copied as necessary, ownership of the
194 * underlying memory blocks is retained by the caller, which must guarantee
195 * that they remain valid until the handler is called.
197 * @param handler The handler to be called when the write operation completes.
198 * Copies will be made of the handler as required. The function signature of
199 * the handler must be:
200 * @code void handler(
201 * const boost::system::error_code& error, // Result of operation.
202 * std::size_t bytes_transferred // Number of bytes written.
204 * Regardless of whether the asynchronous operation completes immediately or
205 * not, the handler will not be invoked from within this function. Invocation
206 * of the handler will be performed in a manner equivalent to using
207 * boost::asio::io_context::post().
209 * @note The write operation may not transmit all of the data to the peer.
210 * Consider using the @ref async_write function if you need to ensure that all
211 * data is written before the asynchronous operation completes.
214 * To write a single data buffer use the @ref buffer function as follows:
216 * handle.async_write_some(boost::asio::buffer(data, size), handler);
218 * See the @ref buffer documentation for information on writing multiple
219 * buffers in one go, and how to use it with arrays, boost::array or
222 template <typename ConstBufferSequence, typename WriteHandler>
223 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
224 void (boost::system::error_code, std::size_t))
225 async_write_some(const ConstBufferSequence& buffers,
226 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
228 // If you get an error on the following line it means that your handler does
229 // not meet the documented type requirements for a WriteHandler.
230 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
232 return this->get_service().async_write_some(this->get_implementation(),
233 buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
236 /// Read some data from the handle.
238 * This function is used to read data from the stream handle. The function
239 * call will block until one or more bytes of data has been read successfully,
240 * or until an error occurs.
242 * @param buffers One or more buffers into which the data will be read.
244 * @returns The number of bytes read.
246 * @throws boost::system::system_error Thrown on failure. An error code of
247 * boost::asio::error::eof indicates that the connection was closed by the
250 * @note The read_some operation may not read all of the requested number of
251 * bytes. Consider using the @ref read function if you need to ensure that
252 * the requested amount of data is read before the blocking operation
256 * To read into a single data buffer use the @ref buffer function as follows:
258 * handle.read_some(boost::asio::buffer(data, size));
260 * See the @ref buffer documentation for information on reading into multiple
261 * buffers in one go, and how to use it with arrays, boost::array or
264 template <typename MutableBufferSequence>
265 std::size_t read_some(const MutableBufferSequence& buffers)
267 boost::system::error_code ec;
268 std::size_t s = this->get_service().read_some(
269 this->get_implementation(), buffers, ec);
270 boost::asio::detail::throw_error(ec, "read_some");
274 /// Read some data from the handle.
276 * This function is used to read data from the stream handle. The function
277 * call will block until one or more bytes of data has been read successfully,
278 * or until an error occurs.
280 * @param buffers One or more buffers into which the data will be read.
282 * @param ec Set to indicate what error occurred, if any.
284 * @returns The number of bytes read. Returns 0 if an error occurred.
286 * @note The read_some operation may not read all of the requested number of
287 * bytes. Consider using the @ref read function if you need to ensure that
288 * the requested amount of data is read before the blocking operation
291 template <typename MutableBufferSequence>
292 std::size_t read_some(const MutableBufferSequence& buffers,
293 boost::system::error_code& ec)
295 return this->get_service().read_some(
296 this->get_implementation(), buffers, ec);
299 /// Start an asynchronous read.
301 * This function is used to asynchronously read data from the stream handle.
302 * The function call always returns immediately.
304 * @param buffers One or more buffers into which the data will be read.
305 * Although the buffers object may be copied as necessary, ownership of the
306 * underlying memory blocks is retained by the caller, which must guarantee
307 * that they remain valid until the handler is called.
309 * @param handler The handler to be called when the read operation completes.
310 * Copies will be made of the handler as required. The function signature of
311 * the handler must be:
312 * @code void handler(
313 * const boost::system::error_code& error, // Result of operation.
314 * std::size_t bytes_transferred // Number of bytes read.
316 * Regardless of whether the asynchronous operation completes immediately or
317 * not, the handler will not be invoked from within this function. Invocation
318 * of the handler will be performed in a manner equivalent to using
319 * boost::asio::io_context::post().
321 * @note The read operation may not read all of the requested number of bytes.
322 * Consider using the @ref async_read function if you need to ensure that the
323 * requested amount of data is read before the asynchronous operation
327 * To read into a single data buffer use the @ref buffer function as follows:
329 * handle.async_read_some(boost::asio::buffer(data, size), handler);
331 * See the @ref buffer documentation for information on reading into multiple
332 * buffers in one go, and how to use it with arrays, boost::array or
335 template <typename MutableBufferSequence, typename ReadHandler>
336 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
337 void (boost::system::error_code, std::size_t))
338 async_read_some(const MutableBufferSequence& buffers,
339 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
341 // If you get an error on the following line it means that your handler does
342 // not meet the documented type requirements for a ReadHandler.
343 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
345 return this->get_service().async_read_some(this->get_implementation(),
346 buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
350 } // namespace windows
354 #include <boost/asio/detail/pop_options.hpp>
356 #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
357 // || defined(GENERATING_DOCUMENTATION)
359 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
361 #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP