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