]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/asio/windows/stream_handle.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / asio / windows / stream_handle.hpp
CommitLineData
7c673cae 1//
b32b8144
FG
2// windows/stream_handle.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~
7c673cae 4//
11fdf7f2 5// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7c673cae
FG
6//
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)
9//
10
b32b8144
FG
11#ifndef BOOST_ASIO_WINDOWS_STREAM_HANDLE_HPP
12#define BOOST_ASIO_WINDOWS_STREAM_HANDLE_HPP
7c673cae
FG
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
b32b8144 19#include <boost/asio/windows/overlapped_handle.hpp>
7c673cae
FG
20
21#if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
22 || defined(GENERATING_DOCUMENTATION)
23
b32b8144
FG
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)
7c673cae
FG
27
28#include <boost/asio/detail/push_options.hpp>
29
30namespace boost {
31namespace asio {
32namespace windows {
33
b32b8144
FG
34#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
35// Typedef for the typical usage of a stream-oriented handle.
36typedef basic_stream_handle<> stream_handle;
37#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
7c673cae
FG
38/// Provides stream-oriented handle functionality.
39/**
b32b8144
FG
40 * The windows::stream_handle class provides asynchronous and blocking
41 * stream-oriented handle functionality.
7c673cae
FG
42 *
43 * @par Thread Safety
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
46 *
47 * @par Concepts:
48 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
49 */
b32b8144
FG
50class stream_handle
51 : public overlapped_handle
7c673cae
FG
52{
53public:
b32b8144 54 /// Construct a stream_handle without opening it.
7c673cae
FG
55 /**
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
58 * or received on it.
59 *
b32b8144 60 * @param io_context The io_context object that the stream handle will use to
7c673cae
FG
61 * dispatch handlers for any asynchronous operations performed on the handle.
62 */
b32b8144
FG
63 explicit stream_handle(boost::asio::io_context& io_context)
64 : overlapped_handle(io_context)
7c673cae
FG
65 {
66 }
67
b32b8144 68 /// Construct a stream_handle on an existing native handle.
7c673cae
FG
69 /**
70 * This constructor creates a stream handle object to hold an existing native
71 * handle.
72 *
b32b8144 73 * @param io_context The io_context object that the stream handle will use to
7c673cae
FG
74 * dispatch handlers for any asynchronous operations performed on the handle.
75 *
76 * @param handle The new underlying handle implementation.
77 *
78 * @throws boost::system::system_error Thrown on failure.
79 */
b32b8144 80 stream_handle(boost::asio::io_context& io_context,
7c673cae 81 const native_handle_type& handle)
b32b8144 82 : overlapped_handle(io_context, handle)
7c673cae
FG
83 {
84 }
85
86#if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
b32b8144 87 /// Move-construct a stream_handle from another.
7c673cae
FG
88 /**
89 * This constructor moves a stream handle from one object to another.
90 *
b32b8144 91 * @param other The other stream_handle object from which the move
7c673cae
FG
92 * will occur.
93 *
94 * @note Following the move, the moved-from object is in the same state as if
b32b8144 95 * constructed using the @c stream_handle(io_context&) constructor.
7c673cae 96 */
b32b8144
FG
97 stream_handle(stream_handle&& other)
98 : overlapped_handle(std::move(other))
7c673cae
FG
99 {
100 }
101
b32b8144 102 /// Move-assign a stream_handle from another.
7c673cae
FG
103 /**
104 * This assignment operator moves a stream handle from one object to
105 * another.
106 *
b32b8144 107 * @param other The other stream_handle object from which the move
7c673cae
FG
108 * will occur.
109 *
110 * @note Following the move, the moved-from object is in the same state as if
b32b8144 111 * constructed using the @c stream_handle(io_context&) constructor.
7c673cae 112 */
b32b8144 113 stream_handle& operator=(stream_handle&& other)
7c673cae 114 {
b32b8144 115 overlapped_handle::operator=(std::move(other));
7c673cae
FG
116 return *this;
117 }
118#endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
119
120 /// Write some data to the handle.
121 /**
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.
125 *
126 * @param buffers One or more data buffers to be written to the handle.
127 *
128 * @returns The number of bytes written.
129 *
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
132 * peer.
133 *
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.
137 *
138 * @par Example
139 * To write a single data buffer use the @ref buffer function as follows:
140 * @code
141 * handle.write_some(boost::asio::buffer(data, size));
142 * @endcode
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
145 * std::vector.
146 */
147 template <typename ConstBufferSequence>
148 std::size_t write_some(const ConstBufferSequence& buffers)
149 {
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");
154 return s;
155 }
156
157 /// Write some data to the handle.
158 /**
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.
162 *
163 * @param buffers One or more data buffers to be written to the handle.
164 *
165 * @param ec Set to indicate what error occurred, if any.
166 *
167 * @returns The number of bytes written. Returns 0 if an error occurred.
168 *
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.
172 */
173 template <typename ConstBufferSequence>
174 std::size_t write_some(const ConstBufferSequence& buffers,
175 boost::system::error_code& ec)
176 {
177 return this->get_service().write_some(
178 this->get_implementation(), buffers, ec);
179 }
180
181 /// Start an asynchronous write.
182 /**
183 * This function is used to asynchronously write data to the stream handle.
184 * The function call always returns immediately.
185 *
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.
190 *
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.
197 * ); @endcode
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
b32b8144 201 * boost::asio::io_context::post().
7c673cae
FG
202 *
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.
206 *
207 * @par Example
208 * To write a single data buffer use the @ref buffer function as follows:
209 * @code
210 * handle.async_write_some(boost::asio::buffer(data, size), handler);
211 * @endcode
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
214 * std::vector.
215 */
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)
221 {
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;
225
b32b8144
FG
226 boost::asio::async_completion<WriteHandler,
227 void (boost::system::error_code, std::size_t)> init(handler);
228
229 this->get_service().async_write_some(
230 this->get_implementation(), buffers, init.completion_handler);
231
232 return init.result.get();
7c673cae
FG
233 }
234
235 /// Read some data from the handle.
236 /**
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.
240 *
241 * @param buffers One or more buffers into which the data will be read.
242 *
243 * @returns The number of bytes read.
244 *
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
247 * peer.
248 *
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
252 * completes.
253 *
254 * @par Example
255 * To read into a single data buffer use the @ref buffer function as follows:
256 * @code
257 * handle.read_some(boost::asio::buffer(data, size));
258 * @endcode
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
261 * std::vector.
262 */
263 template <typename MutableBufferSequence>
264 std::size_t read_some(const MutableBufferSequence& buffers)
265 {
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");
270 return s;
271 }
272
273 /// Read some data from the handle.
274 /**
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.
278 *
279 * @param buffers One or more buffers into which the data will be read.
280 *
281 * @param ec Set to indicate what error occurred, if any.
282 *
283 * @returns The number of bytes read. Returns 0 if an error occurred.
284 *
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
288 * completes.
289 */
290 template <typename MutableBufferSequence>
291 std::size_t read_some(const MutableBufferSequence& buffers,
292 boost::system::error_code& ec)
293 {
294 return this->get_service().read_some(
295 this->get_implementation(), buffers, ec);
296 }
297
298 /// Start an asynchronous read.
299 /**
300 * This function is used to asynchronously read data from the stream handle.
301 * The function call always returns immediately.
302 *
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.
307 *
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.
314 * ); @endcode
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
b32b8144 318 * boost::asio::io_context::post().
7c673cae
FG
319 *
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
323 * completes.
324 *
325 * @par Example
326 * To read into a single data buffer use the @ref buffer function as follows:
327 * @code
328 * handle.async_read_some(boost::asio::buffer(data, size), handler);
329 * @endcode
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
332 * std::vector.
333 */
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)
339 {
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;
343
b32b8144
FG
344 boost::asio::async_completion<ReadHandler,
345 void (boost::system::error_code, std::size_t)> init(handler);
346
347 this->get_service().async_read_some(
348 this->get_implementation(), buffers, init.completion_handler);
349
350 return init.result.get();
7c673cae
FG
351 }
352};
b32b8144 353#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
7c673cae
FG
354
355} // namespace windows
356} // namespace asio
357} // namespace boost
358
359#include <boost/asio/detail/pop_options.hpp>
360
361#endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
362 // || defined(GENERATING_DOCUMENTATION)
363
b32b8144 364#endif // BOOST_ASIO_WINDOWS_STREAM_HANDLE_HPP