]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // windows/basic_stream_handle.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 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 | ||
11 | #ifndef BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP | |
12 | #define BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP | |
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> | |
19 | ||
b32b8144 FG |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
21 | ||
7c673cae FG |
22 | #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \ |
23 | || defined(GENERATING_DOCUMENTATION) | |
24 | ||
25 | #include <cstddef> | |
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> | |
31 | ||
32 | #include <boost/asio/detail/push_options.hpp> | |
33 | ||
34 | namespace boost { | |
35 | namespace asio { | |
36 | namespace windows { | |
37 | ||
38 | /// Provides stream-oriented handle functionality. | |
39 | /** | |
40 | * The windows::basic_stream_handle class template provides asynchronous and | |
41 | * blocking stream-oriented handle functionality. | |
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 | */ | |
50 | template <typename StreamHandleService = stream_handle_service> | |
51 | class basic_stream_handle | |
52 | : public basic_handle<StreamHandleService> | |
53 | { | |
54 | public: | |
7c673cae FG |
55 | /// The native representation of a handle. |
56 | typedef typename StreamHandleService::native_handle_type native_handle_type; | |
57 | ||
58 | /// Construct a basic_stream_handle without opening it. | |
59 | /** | |
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 | |
62 | * or received on it. | |
63 | * | |
b32b8144 | 64 | * @param io_context The io_context object that the stream handle will use to |
7c673cae FG |
65 | * dispatch handlers for any asynchronous operations performed on the handle. |
66 | */ | |
b32b8144 FG |
67 | explicit basic_stream_handle(boost::asio::io_context& io_context) |
68 | : basic_handle<StreamHandleService>(io_context) | |
7c673cae FG |
69 | { |
70 | } | |
71 | ||
72 | /// Construct a basic_stream_handle on an existing native handle. | |
73 | /** | |
74 | * This constructor creates a stream handle object to hold an existing native | |
75 | * handle. | |
76 | * | |
b32b8144 | 77 | * @param io_context The io_context object that the stream handle will use to |
7c673cae FG |
78 | * dispatch handlers for any asynchronous operations performed on the handle. |
79 | * | |
80 | * @param handle The new underlying handle implementation. | |
81 | * | |
82 | * @throws boost::system::system_error Thrown on failure. | |
83 | */ | |
b32b8144 | 84 | basic_stream_handle(boost::asio::io_context& io_context, |
7c673cae | 85 | const native_handle_type& handle) |
b32b8144 | 86 | : basic_handle<StreamHandleService>(io_context, handle) |
7c673cae FG |
87 | { |
88 | } | |
89 | ||
90 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
91 | /// Move-construct a basic_stream_handle from another. | |
92 | /** | |
93 | * This constructor moves a stream handle from one object to another. | |
94 | * | |
95 | * @param other The other basic_stream_handle object from which the move | |
96 | * will occur. | |
97 | * | |
98 | * @note Following the move, the moved-from object is in the same state as if | |
b32b8144 | 99 | * constructed using the @c basic_stream_handle(io_context&) constructor. |
7c673cae FG |
100 | */ |
101 | basic_stream_handle(basic_stream_handle&& other) | |
102 | : basic_handle<StreamHandleService>( | |
103 | BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other)) | |
104 | { | |
105 | } | |
106 | ||
107 | /// Move-assign a basic_stream_handle from another. | |
108 | /** | |
109 | * This assignment operator moves a stream handle from one object to | |
110 | * another. | |
111 | * | |
112 | * @param other The other basic_stream_handle object from which the move | |
113 | * will occur. | |
114 | * | |
115 | * @note Following the move, the moved-from object is in the same state as if | |
b32b8144 | 116 | * constructed using the @c basic_stream_handle(io_context&) constructor. |
7c673cae FG |
117 | */ |
118 | basic_stream_handle& operator=(basic_stream_handle&& other) | |
119 | { | |
120 | basic_handle<StreamHandleService>::operator=( | |
121 | BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other)); | |
122 | return *this; | |
123 | } | |
124 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
125 | ||
126 | /// Write some data to the handle. | |
127 | /** | |
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. | |
131 | * | |
132 | * @param buffers One or more data buffers to be written to the handle. | |
133 | * | |
134 | * @returns The number of bytes written. | |
135 | * | |
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 | |
138 | * peer. | |
139 | * | |
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. | |
143 | * | |
144 | * @par Example | |
145 | * To write a single data buffer use the @ref buffer function as follows: | |
146 | * @code | |
147 | * handle.write_some(boost::asio::buffer(data, size)); | |
148 | * @endcode | |
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 | |
151 | * std::vector. | |
152 | */ | |
153 | template <typename ConstBufferSequence> | |
154 | std::size_t write_some(const ConstBufferSequence& buffers) | |
155 | { | |
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"); | |
160 | return s; | |
161 | } | |
162 | ||
163 | /// Write some data to the handle. | |
164 | /** | |
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. | |
168 | * | |
169 | * @param buffers One or more data buffers to be written to the handle. | |
170 | * | |
171 | * @param ec Set to indicate what error occurred, if any. | |
172 | * | |
173 | * @returns The number of bytes written. Returns 0 if an error occurred. | |
174 | * | |
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. | |
178 | */ | |
179 | template <typename ConstBufferSequence> | |
180 | std::size_t write_some(const ConstBufferSequence& buffers, | |
181 | boost::system::error_code& ec) | |
182 | { | |
183 | return this->get_service().write_some( | |
184 | this->get_implementation(), buffers, ec); | |
185 | } | |
186 | ||
187 | /// Start an asynchronous write. | |
188 | /** | |
189 | * This function is used to asynchronously write data to the stream handle. | |
190 | * The function call always returns immediately. | |
191 | * | |
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. | |
196 | * | |
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. | |
203 | * ); @endcode | |
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 | |
b32b8144 | 207 | * boost::asio::io_context::post(). |
7c673cae FG |
208 | * |
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. | |
212 | * | |
213 | * @par Example | |
214 | * To write a single data buffer use the @ref buffer function as follows: | |
215 | * @code | |
216 | * handle.async_write_some(boost::asio::buffer(data, size), handler); | |
217 | * @endcode | |
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 | |
220 | * std::vector. | |
221 | */ | |
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) | |
227 | { | |
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; | |
231 | ||
232 | return this->get_service().async_write_some(this->get_implementation(), | |
233 | buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); | |
234 | } | |
235 | ||
236 | /// Read some data from the handle. | |
237 | /** | |
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. | |
241 | * | |
242 | * @param buffers One or more buffers into which the data will be read. | |
243 | * | |
244 | * @returns The number of bytes read. | |
245 | * | |
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 | |
248 | * peer. | |
249 | * | |
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 | |
253 | * completes. | |
254 | * | |
255 | * @par Example | |
256 | * To read into a single data buffer use the @ref buffer function as follows: | |
257 | * @code | |
258 | * handle.read_some(boost::asio::buffer(data, size)); | |
259 | * @endcode | |
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 | |
262 | * std::vector. | |
263 | */ | |
264 | template <typename MutableBufferSequence> | |
265 | std::size_t read_some(const MutableBufferSequence& buffers) | |
266 | { | |
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"); | |
271 | return s; | |
272 | } | |
273 | ||
274 | /// Read some data from the handle. | |
275 | /** | |
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. | |
279 | * | |
280 | * @param buffers One or more buffers into which the data will be read. | |
281 | * | |
282 | * @param ec Set to indicate what error occurred, if any. | |
283 | * | |
284 | * @returns The number of bytes read. Returns 0 if an error occurred. | |
285 | * | |
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 | |
289 | * completes. | |
290 | */ | |
291 | template <typename MutableBufferSequence> | |
292 | std::size_t read_some(const MutableBufferSequence& buffers, | |
293 | boost::system::error_code& ec) | |
294 | { | |
295 | return this->get_service().read_some( | |
296 | this->get_implementation(), buffers, ec); | |
297 | } | |
298 | ||
299 | /// Start an asynchronous read. | |
300 | /** | |
301 | * This function is used to asynchronously read data from the stream handle. | |
302 | * The function call always returns immediately. | |
303 | * | |
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. | |
308 | * | |
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. | |
315 | * ); @endcode | |
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 | |
b32b8144 | 319 | * boost::asio::io_context::post(). |
7c673cae FG |
320 | * |
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 | |
324 | * completes. | |
325 | * | |
326 | * @par Example | |
327 | * To read into a single data buffer use the @ref buffer function as follows: | |
328 | * @code | |
329 | * handle.async_read_some(boost::asio::buffer(data, size), handler); | |
330 | * @endcode | |
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 | |
333 | * std::vector. | |
334 | */ | |
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) | |
340 | { | |
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; | |
344 | ||
345 | return this->get_service().async_read_some(this->get_implementation(), | |
346 | buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); | |
347 | } | |
348 | }; | |
349 | ||
350 | } // namespace windows | |
351 | } // namespace asio | |
352 | } // namespace boost | |
353 | ||
354 | #include <boost/asio/detail/pop_options.hpp> | |
355 | ||
356 | #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) | |
357 | // || defined(GENERATING_DOCUMENTATION) | |
358 | ||
b32b8144 FG |
359 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
360 | ||
7c673cae | 361 | #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP |