Chris@16
|
1 //
|
Chris@16
|
2 // windows/basic_stream_handle.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
|
Chris@16
|
21 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
22
|
Chris@16
|
23 #include <cstddef>
|
Chris@16
|
24 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
26 #include <boost/asio/error.hpp>
|
Chris@16
|
27 #include <boost/asio/windows/basic_handle.hpp>
|
Chris@16
|
28 #include <boost/asio/windows/stream_handle_service.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost {
|
Chris@16
|
33 namespace asio {
|
Chris@16
|
34 namespace windows {
|
Chris@16
|
35
|
Chris@16
|
36 /// Provides stream-oriented handle functionality.
|
Chris@16
|
37 /**
|
Chris@16
|
38 * The windows::basic_stream_handle class template provides asynchronous and
|
Chris@16
|
39 * blocking stream-oriented handle functionality.
|
Chris@16
|
40 *
|
Chris@16
|
41 * @par Thread Safety
|
Chris@16
|
42 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
43 * @e Shared @e objects: Unsafe.
|
Chris@16
|
44 *
|
Chris@16
|
45 * @par Concepts:
|
Chris@16
|
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
|
Chris@16
|
47 */
|
Chris@16
|
48 template <typename StreamHandleService = stream_handle_service>
|
Chris@16
|
49 class basic_stream_handle
|
Chris@16
|
50 : public basic_handle<StreamHandleService>
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 /// (Deprecated: Use native_handle_type.) The native representation of a
|
Chris@16
|
54 /// handle.
|
Chris@16
|
55 typedef typename StreamHandleService::native_handle_type native_type;
|
Chris@16
|
56
|
Chris@16
|
57 /// The native representation of a handle.
|
Chris@16
|
58 typedef typename StreamHandleService::native_handle_type native_handle_type;
|
Chris@16
|
59
|
Chris@16
|
60 /// Construct a basic_stream_handle without opening it.
|
Chris@16
|
61 /**
|
Chris@16
|
62 * This constructor creates a stream handle without opening it. The handle
|
Chris@16
|
63 * needs to be opened and then connected or accepted before data can be sent
|
Chris@16
|
64 * or received on it.
|
Chris@16
|
65 *
|
Chris@16
|
66 * @param io_service The io_service object that the stream handle will use to
|
Chris@16
|
67 * dispatch handlers for any asynchronous operations performed on the handle.
|
Chris@16
|
68 */
|
Chris@16
|
69 explicit basic_stream_handle(boost::asio::io_service& io_service)
|
Chris@16
|
70 : basic_handle<StreamHandleService>(io_service)
|
Chris@16
|
71 {
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 /// Construct a basic_stream_handle on an existing native handle.
|
Chris@16
|
75 /**
|
Chris@16
|
76 * This constructor creates a stream handle object to hold an existing native
|
Chris@16
|
77 * handle.
|
Chris@16
|
78 *
|
Chris@16
|
79 * @param io_service The io_service object that the stream handle will use to
|
Chris@16
|
80 * dispatch handlers for any asynchronous operations performed on the handle.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param handle The new underlying handle implementation.
|
Chris@16
|
83 *
|
Chris@16
|
84 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
85 */
|
Chris@16
|
86 basic_stream_handle(boost::asio::io_service& io_service,
|
Chris@16
|
87 const native_handle_type& handle)
|
Chris@16
|
88 : basic_handle<StreamHandleService>(io_service, handle)
|
Chris@16
|
89 {
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
93 /// Move-construct a basic_stream_handle from another.
|
Chris@16
|
94 /**
|
Chris@16
|
95 * This constructor moves a stream handle from one object to another.
|
Chris@16
|
96 *
|
Chris@16
|
97 * @param other The other basic_stream_handle object from which the move
|
Chris@16
|
98 * will occur.
|
Chris@16
|
99 *
|
Chris@16
|
100 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
101 * constructed using the @c basic_stream_handle(io_service&) constructor.
|
Chris@16
|
102 */
|
Chris@16
|
103 basic_stream_handle(basic_stream_handle&& other)
|
Chris@16
|
104 : basic_handle<StreamHandleService>(
|
Chris@16
|
105 BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other))
|
Chris@16
|
106 {
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 /// Move-assign a basic_stream_handle from another.
|
Chris@16
|
110 /**
|
Chris@16
|
111 * This assignment operator moves a stream handle from one object to
|
Chris@16
|
112 * another.
|
Chris@16
|
113 *
|
Chris@16
|
114 * @param other The other basic_stream_handle object from which the move
|
Chris@16
|
115 * will occur.
|
Chris@16
|
116 *
|
Chris@16
|
117 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
118 * constructed using the @c basic_stream_handle(io_service&) constructor.
|
Chris@16
|
119 */
|
Chris@16
|
120 basic_stream_handle& operator=(basic_stream_handle&& other)
|
Chris@16
|
121 {
|
Chris@16
|
122 basic_handle<StreamHandleService>::operator=(
|
Chris@16
|
123 BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other));
|
Chris@16
|
124 return *this;
|
Chris@16
|
125 }
|
Chris@16
|
126 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
127
|
Chris@16
|
128 /// Write some data to the handle.
|
Chris@16
|
129 /**
|
Chris@16
|
130 * This function is used to write data to the stream handle. The function call
|
Chris@16
|
131 * will block until one or more bytes of the data has been written
|
Chris@16
|
132 * successfully, or until an error occurs.
|
Chris@16
|
133 *
|
Chris@16
|
134 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
135 *
|
Chris@16
|
136 * @returns The number of bytes written.
|
Chris@16
|
137 *
|
Chris@16
|
138 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
139 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
140 * peer.
|
Chris@16
|
141 *
|
Chris@16
|
142 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
143 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
144 * all data is written before the blocking operation completes.
|
Chris@16
|
145 *
|
Chris@16
|
146 * @par Example
|
Chris@16
|
147 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
148 * @code
|
Chris@16
|
149 * handle.write_some(boost::asio::buffer(data, size));
|
Chris@16
|
150 * @endcode
|
Chris@16
|
151 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
152 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
153 * std::vector.
|
Chris@16
|
154 */
|
Chris@16
|
155 template <typename ConstBufferSequence>
|
Chris@16
|
156 std::size_t write_some(const ConstBufferSequence& buffers)
|
Chris@16
|
157 {
|
Chris@16
|
158 boost::system::error_code ec;
|
Chris@16
|
159 std::size_t s = this->get_service().write_some(
|
Chris@16
|
160 this->get_implementation(), buffers, ec);
|
Chris@16
|
161 boost::asio::detail::throw_error(ec, "write_some");
|
Chris@16
|
162 return s;
|
Chris@16
|
163 }
|
Chris@16
|
164
|
Chris@16
|
165 /// Write some data to the handle.
|
Chris@16
|
166 /**
|
Chris@16
|
167 * This function is used to write data to the stream handle. The function call
|
Chris@16
|
168 * will block until one or more bytes of the data has been written
|
Chris@16
|
169 * successfully, or until an error occurs.
|
Chris@16
|
170 *
|
Chris@16
|
171 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
172 *
|
Chris@16
|
173 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
174 *
|
Chris@16
|
175 * @returns The number of bytes written. Returns 0 if an error occurred.
|
Chris@16
|
176 *
|
Chris@16
|
177 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
178 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
179 * all data is written before the blocking operation completes.
|
Chris@16
|
180 */
|
Chris@16
|
181 template <typename ConstBufferSequence>
|
Chris@16
|
182 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
183 boost::system::error_code& ec)
|
Chris@16
|
184 {
|
Chris@16
|
185 return this->get_service().write_some(
|
Chris@16
|
186 this->get_implementation(), buffers, ec);
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 /// Start an asynchronous write.
|
Chris@16
|
190 /**
|
Chris@16
|
191 * This function is used to asynchronously write data to the stream handle.
|
Chris@16
|
192 * The function call always returns immediately.
|
Chris@16
|
193 *
|
Chris@16
|
194 * @param buffers One or more data buffers to be written to the handle.
|
Chris@16
|
195 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
196 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
197 * that they remain valid until the handler is called.
|
Chris@16
|
198 *
|
Chris@16
|
199 * @param handler The handler to be called when the write operation completes.
|
Chris@16
|
200 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
201 * the handler must be:
|
Chris@16
|
202 * @code void handler(
|
Chris@16
|
203 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
204 * std::size_t bytes_transferred // Number of bytes written.
|
Chris@16
|
205 * ); @endcode
|
Chris@16
|
206 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
207 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
208 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
209 * boost::asio::io_service::post().
|
Chris@16
|
210 *
|
Chris@16
|
211 * @note The write operation may not transmit all of the data to the peer.
|
Chris@16
|
212 * Consider using the @ref async_write function if you need to ensure that all
|
Chris@16
|
213 * data is written before the asynchronous operation completes.
|
Chris@16
|
214 *
|
Chris@16
|
215 * @par Example
|
Chris@16
|
216 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
217 * @code
|
Chris@16
|
218 * handle.async_write_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
219 * @endcode
|
Chris@16
|
220 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
221 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
222 * std::vector.
|
Chris@16
|
223 */
|
Chris@16
|
224 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
225 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
226 void (boost::system::error_code, std::size_t))
|
Chris@16
|
227 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
228 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
229 {
|
Chris@16
|
230 // If you get an error on the following line it means that your handler does
|
Chris@16
|
231 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
232 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
233
|
Chris@16
|
234 return this->get_service().async_write_some(this->get_implementation(),
|
Chris@16
|
235 buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 /// Read some data from the handle.
|
Chris@16
|
239 /**
|
Chris@16
|
240 * This function is used to read data from the stream handle. The function
|
Chris@16
|
241 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
242 * or until an error occurs.
|
Chris@16
|
243 *
|
Chris@16
|
244 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
245 *
|
Chris@16
|
246 * @returns The number of bytes read.
|
Chris@16
|
247 *
|
Chris@16
|
248 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
249 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
250 * peer.
|
Chris@16
|
251 *
|
Chris@16
|
252 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
253 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
254 * the requested amount of data is read before the blocking operation
|
Chris@16
|
255 * completes.
|
Chris@16
|
256 *
|
Chris@16
|
257 * @par Example
|
Chris@16
|
258 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
259 * @code
|
Chris@16
|
260 * handle.read_some(boost::asio::buffer(data, size));
|
Chris@16
|
261 * @endcode
|
Chris@16
|
262 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
263 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
264 * std::vector.
|
Chris@16
|
265 */
|
Chris@16
|
266 template <typename MutableBufferSequence>
|
Chris@16
|
267 std::size_t read_some(const MutableBufferSequence& buffers)
|
Chris@16
|
268 {
|
Chris@16
|
269 boost::system::error_code ec;
|
Chris@16
|
270 std::size_t s = this->get_service().read_some(
|
Chris@16
|
271 this->get_implementation(), buffers, ec);
|
Chris@16
|
272 boost::asio::detail::throw_error(ec, "read_some");
|
Chris@16
|
273 return s;
|
Chris@16
|
274 }
|
Chris@16
|
275
|
Chris@16
|
276 /// Read some data from the handle.
|
Chris@16
|
277 /**
|
Chris@16
|
278 * This function is used to read data from the stream handle. The function
|
Chris@16
|
279 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
280 * or until an error occurs.
|
Chris@16
|
281 *
|
Chris@16
|
282 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
283 *
|
Chris@16
|
284 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
285 *
|
Chris@16
|
286 * @returns The number of bytes read. Returns 0 if an error occurred.
|
Chris@16
|
287 *
|
Chris@16
|
288 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
289 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
290 * the requested amount of data is read before the blocking operation
|
Chris@16
|
291 * completes.
|
Chris@16
|
292 */
|
Chris@16
|
293 template <typename MutableBufferSequence>
|
Chris@16
|
294 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
295 boost::system::error_code& ec)
|
Chris@16
|
296 {
|
Chris@16
|
297 return this->get_service().read_some(
|
Chris@16
|
298 this->get_implementation(), buffers, ec);
|
Chris@16
|
299 }
|
Chris@16
|
300
|
Chris@16
|
301 /// Start an asynchronous read.
|
Chris@16
|
302 /**
|
Chris@16
|
303 * This function is used to asynchronously read data from the stream handle.
|
Chris@16
|
304 * The function call always returns immediately.
|
Chris@16
|
305 *
|
Chris@16
|
306 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
307 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
308 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
309 * that they remain valid until the handler is called.
|
Chris@16
|
310 *
|
Chris@16
|
311 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
312 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
313 * the handler must be:
|
Chris@16
|
314 * @code void handler(
|
Chris@16
|
315 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
316 * std::size_t bytes_transferred // Number of bytes read.
|
Chris@16
|
317 * ); @endcode
|
Chris@16
|
318 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
319 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
320 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
321 * boost::asio::io_service::post().
|
Chris@16
|
322 *
|
Chris@16
|
323 * @note The read operation may not read all of the requested number of bytes.
|
Chris@16
|
324 * Consider using the @ref async_read function if you need to ensure that the
|
Chris@16
|
325 * requested amount of data is read before the asynchronous operation
|
Chris@16
|
326 * completes.
|
Chris@16
|
327 *
|
Chris@16
|
328 * @par Example
|
Chris@16
|
329 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
330 * @code
|
Chris@16
|
331 * handle.async_read_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
332 * @endcode
|
Chris@16
|
333 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
334 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
335 * std::vector.
|
Chris@16
|
336 */
|
Chris@16
|
337 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
338 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
339 void (boost::system::error_code, std::size_t))
|
Chris@16
|
340 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
341 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
342 {
|
Chris@16
|
343 // If you get an error on the following line it means that your handler does
|
Chris@16
|
344 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
345 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
346
|
Chris@16
|
347 return this->get_service().async_read_some(this->get_implementation(),
|
Chris@16
|
348 buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
349 }
|
Chris@16
|
350 };
|
Chris@16
|
351
|
Chris@16
|
352 } // namespace windows
|
Chris@16
|
353 } // namespace asio
|
Chris@16
|
354 } // namespace boost
|
Chris@16
|
355
|
Chris@16
|
356 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
357
|
Chris@16
|
358 #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
|
Chris@16
|
359 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
360
|
Chris@16
|
361 #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
|