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