2 // windows/stream_handle.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_WINDOWS_STREAM_HANDLE_HPP
12 #define BOOST_ASIO_WINDOWS_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>
19 #include <boost/asio/windows/overlapped_handle.hpp>
21 #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
22 || defined(GENERATING_DOCUMENTATION)
24 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
25 # include <boost/asio/windows/basic_stream_handle.hpp>
26 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
28 #include <boost/asio/detail/push_options.hpp>
34 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
35 // Typedef for the typical usage of a stream-oriented handle.
36 typedef basic_stream_handle<> stream_handle;
37 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
38 /// Provides stream-oriented handle functionality.
40 * The windows::stream_handle class provides asynchronous and blocking
41 * stream-oriented handle functionality.
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
48 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
51 : public overlapped_handle
54 /// Construct a stream_handle without opening it.
56 * This constructor creates a stream handle without opening it. The handle
57 * needs to be opened and then connected or accepted before data can be sent
60 * @param io_context The io_context object that the stream handle will use to
61 * dispatch handlers for any asynchronous operations performed on the handle.
63 explicit stream_handle(boost::asio::io_context& io_context)
64 : overlapped_handle(io_context)
68 /// Construct a stream_handle on an existing native handle.
70 * This constructor creates a stream handle object to hold an existing native
73 * @param io_context The io_context object that the stream handle will use to
74 * dispatch handlers for any asynchronous operations performed on the handle.
76 * @param handle The new underlying handle implementation.
78 * @throws boost::system::system_error Thrown on failure.
80 stream_handle(boost::asio::io_context& io_context,
81 const native_handle_type& handle)
82 : overlapped_handle(io_context, handle)
86 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
87 /// Move-construct a stream_handle from another.
89 * This constructor moves a stream handle from one object to another.
91 * @param other The other stream_handle 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_handle(io_context&) constructor.
97 stream_handle(stream_handle&& other)
98 : overlapped_handle(std::move(other))
102 /// Move-assign a stream_handle from another.
104 * This assignment operator moves a stream handle from one object to
107 * @param other The other stream_handle 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_handle(io_context&) constructor.
113 stream_handle& operator=(stream_handle&& other)
115 overlapped_handle::operator=(std::move(other));
118 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
120 /// Write some data to the handle.
122 * This function is used to write data to the stream handle. The function call
123 * 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 handle.
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 * handle.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 handle.
159 * This function is used to write data to the stream handle. The function call
160 * 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 handle.
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 handle.
184 * The function call always returns immediately.
186 * @param buffers One or more data buffers to be written to the handle.
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 * handle.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 handle.
237 * This function is used to read data from the stream handle. 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 * handle.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 handle.
275 * This function is used to read data from the stream handle. 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 handle.
301 * 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 * handle.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)
355 } // namespace windows
359 #include <boost/asio/detail/pop_options.hpp>
361 #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
362 // || defined(GENERATING_DOCUMENTATION)
364 #endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_HPP