5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WRITE_HPP
12 #define BOOST_ASIO_WRITE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/basic_streambuf_fwd.hpp>
22 #include <boost/asio/error.hpp>
24 #include <boost/asio/detail/push_options.hpp>
30 * @defgroup write boost::asio::write
32 * @brief Write a certain amount of data to a stream before returning.
36 /// Write all of the supplied data to a stream before returning.
38 * This function is used to write a certain number of bytes of data to a stream.
39 * The call will block until one of the following conditions is true:
41 * @li All of the data in the supplied buffers has been written. That is, the
42 * bytes transferred is equal to the sum of the buffer sizes.
44 * @li An error occurred.
46 * This operation is implemented in terms of zero or more calls to the stream's
47 * write_some function.
49 * @param s The stream to which the data is to be written. The type must support
50 * the SyncWriteStream concept.
52 * @param buffers One or more buffers containing the data to be written. The sum
53 * of the buffer sizes indicates the maximum number of bytes to write to the
56 * @returns The number of bytes transferred.
58 * @throws boost::system::system_error Thrown on failure.
61 * To write a single data buffer use the @ref buffer function as follows:
62 * @code boost::asio::write(s, boost::asio::buffer(data, size)); @endcode
63 * See the @ref buffer documentation for information on writing multiple
64 * buffers in one go, and how to use it with arrays, boost::array or
67 * @note This overload is equivalent to calling:
68 * @code boost::asio::write(
70 * boost::asio::transfer_all()); @endcode
72 template <typename SyncWriteStream, typename ConstBufferSequence>
73 std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers);
75 /// Write all of the supplied data to a stream before returning.
77 * This function is used to write a certain number of bytes of data to a stream.
78 * The call will block until one of the following conditions is true:
80 * @li All of the data in the supplied buffers has been written. That is, the
81 * bytes transferred is equal to the sum of the buffer sizes.
83 * @li An error occurred.
85 * This operation is implemented in terms of zero or more calls to the stream's
86 * write_some function.
88 * @param s The stream to which the data is to be written. The type must support
89 * the SyncWriteStream concept.
91 * @param buffers One or more buffers containing the data to be written. The sum
92 * of the buffer sizes indicates the maximum number of bytes to write to the
95 * @param ec Set to indicate what error occurred, if any.
97 * @returns The number of bytes transferred.
100 * To write a single data buffer use the @ref buffer function as follows:
101 * @code boost::asio::write(s, boost::asio::buffer(data, size), ec); @endcode
102 * See the @ref buffer documentation for information on writing multiple
103 * buffers in one go, and how to use it with arrays, boost::array or
106 * @note This overload is equivalent to calling:
107 * @code boost::asio::write(
109 * boost::asio::transfer_all(), ec); @endcode
111 template <typename SyncWriteStream, typename ConstBufferSequence>
112 std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
113 boost::system::error_code& ec);
115 /// Write a certain amount of data to a stream before returning.
117 * This function is used to write a certain number of bytes of data to a stream.
118 * The call will block until one of the following conditions is true:
120 * @li All of the data in the supplied buffers has been written. That is, the
121 * bytes transferred is equal to the sum of the buffer sizes.
123 * @li The completion_condition function object returns 0.
125 * This operation is implemented in terms of zero or more calls to the stream's
126 * write_some function.
128 * @param s The stream to which the data is to be written. The type must support
129 * the SyncWriteStream concept.
131 * @param buffers One or more buffers containing the data to be written. The sum
132 * of the buffer sizes indicates the maximum number of bytes to write to the
135 * @param completion_condition The function object to be called to determine
136 * whether the write operation is complete. The signature of the function object
138 * @code std::size_t completion_condition(
139 * // Result of latest write_some operation.
140 * const boost::system::error_code& error,
142 * // Number of bytes transferred so far.
143 * std::size_t bytes_transferred
145 * A return value of 0 indicates that the write operation is complete. A
146 * non-zero return value indicates the maximum number of bytes to be written on
147 * the next call to the stream's write_some function.
149 * @returns The number of bytes transferred.
151 * @throws boost::system::system_error Thrown on failure.
154 * To write a single data buffer use the @ref buffer function as follows:
155 * @code boost::asio::write(s, boost::asio::buffer(data, size),
156 * boost::asio::transfer_at_least(32)); @endcode
157 * See the @ref buffer documentation for information on writing multiple
158 * buffers in one go, and how to use it with arrays, boost::array or
161 template <typename SyncWriteStream, typename ConstBufferSequence,
162 typename CompletionCondition>
163 std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
164 CompletionCondition completion_condition);
166 /// Write a certain amount of data to a stream before returning.
168 * This function is used to write a certain number of bytes of data to a stream.
169 * The call will block until one of the following conditions is true:
171 * @li All of the data in the supplied buffers has been written. That is, the
172 * bytes transferred is equal to the sum of the buffer sizes.
174 * @li The completion_condition function object returns 0.
176 * This operation is implemented in terms of zero or more calls to the stream's
177 * write_some function.
179 * @param s The stream to which the data is to be written. The type must support
180 * the SyncWriteStream concept.
182 * @param buffers One or more buffers containing the data to be written. The sum
183 * of the buffer sizes indicates the maximum number of bytes to write to the
186 * @param completion_condition The function object to be called to determine
187 * whether the write operation is complete. The signature of the function object
189 * @code std::size_t completion_condition(
190 * // Result of latest write_some operation.
191 * const boost::system::error_code& error,
193 * // Number of bytes transferred so far.
194 * std::size_t bytes_transferred
196 * A return value of 0 indicates that the write operation is complete. A
197 * non-zero return value indicates the maximum number of bytes to be written on
198 * the next call to the stream's write_some function.
200 * @param ec Set to indicate what error occurred, if any.
202 * @returns The number of bytes written. If an error occurs, returns the total
203 * number of bytes successfully transferred prior to the error.
205 template <typename SyncWriteStream, typename ConstBufferSequence,
206 typename CompletionCondition>
207 std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
208 CompletionCondition completion_condition, boost::system::error_code& ec);
210 #if !defined(BOOST_ASIO_NO_IOSTREAM)
212 /// Write all of the supplied data to a stream before returning.
214 * This function is used to write a certain number of bytes of data to a stream.
215 * The call will block until one of the following conditions is true:
217 * @li All of the data in the supplied basic_streambuf has been written.
219 * @li An error occurred.
221 * This operation is implemented in terms of zero or more calls to the stream's
222 * write_some function.
224 * @param s The stream to which the data is to be written. The type must support
225 * the SyncWriteStream concept.
227 * @param b The basic_streambuf object from which data will be written.
229 * @returns The number of bytes transferred.
231 * @throws boost::system::system_error Thrown on failure.
233 * @note This overload is equivalent to calling:
234 * @code boost::asio::write(
236 * boost::asio::transfer_all()); @endcode
238 template <typename SyncWriteStream, typename Allocator>
239 std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b);
241 /// Write all of the supplied data to a stream before returning.
243 * This function is used to write a certain number of bytes of data to a stream.
244 * The call will block until one of the following conditions is true:
246 * @li All of the data in the supplied basic_streambuf has been written.
248 * @li An error occurred.
250 * This operation is implemented in terms of zero or more calls to the stream's
251 * write_some function.
253 * @param s The stream to which the data is to be written. The type must support
254 * the SyncWriteStream concept.
256 * @param b The basic_streambuf object from which data will be written.
258 * @param ec Set to indicate what error occurred, if any.
260 * @returns The number of bytes transferred.
262 * @note This overload is equivalent to calling:
263 * @code boost::asio::write(
265 * boost::asio::transfer_all(), ec); @endcode
267 template <typename SyncWriteStream, typename Allocator>
268 std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
269 boost::system::error_code& ec);
271 /// Write a certain amount of data to a stream before returning.
273 * This function is used to write a certain number of bytes of data to a stream.
274 * The call will block until one of the following conditions is true:
276 * @li All of the data in the supplied basic_streambuf has been written.
278 * @li The completion_condition function object returns 0.
280 * This operation is implemented in terms of zero or more calls to the stream's
281 * write_some function.
283 * @param s The stream to which the data is to be written. The type must support
284 * the SyncWriteStream concept.
286 * @param b The basic_streambuf object from which data will be written.
288 * @param completion_condition The function object to be called to determine
289 * whether the write operation is complete. The signature of the function object
291 * @code std::size_t completion_condition(
292 * // Result of latest write_some operation.
293 * const boost::system::error_code& error,
295 * // Number of bytes transferred so far.
296 * std::size_t bytes_transferred
298 * A return value of 0 indicates that the write operation is complete. A
299 * non-zero return value indicates the maximum number of bytes to be written on
300 * the next call to the stream's write_some function.
302 * @returns The number of bytes transferred.
304 * @throws boost::system::system_error Thrown on failure.
306 template <typename SyncWriteStream, typename Allocator,
307 typename CompletionCondition>
308 std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
309 CompletionCondition completion_condition);
311 /// Write a certain amount of data to a stream before returning.
313 * This function is used to write a certain number of bytes of data to a stream.
314 * The call will block until one of the following conditions is true:
316 * @li All of the data in the supplied basic_streambuf has been written.
318 * @li The completion_condition function object returns 0.
320 * This operation is implemented in terms of zero or more calls to the stream's
321 * write_some function.
323 * @param s The stream to which the data is to be written. The type must support
324 * the SyncWriteStream concept.
326 * @param b The basic_streambuf object from which data will be written.
328 * @param completion_condition The function object to be called to determine
329 * whether the write operation is complete. The signature of the function object
331 * @code std::size_t completion_condition(
332 * // Result of latest write_some operation.
333 * const boost::system::error_code& error,
335 * // Number of bytes transferred so far.
336 * std::size_t bytes_transferred
338 * A return value of 0 indicates that the write operation is complete. A
339 * non-zero return value indicates the maximum number of bytes to be written on
340 * the next call to the stream's write_some function.
342 * @param ec Set to indicate what error occurred, if any.
344 * @returns The number of bytes written. If an error occurs, returns the total
345 * number of bytes successfully transferred prior to the error.
347 template <typename SyncWriteStream, typename Allocator,
348 typename CompletionCondition>
349 std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
350 CompletionCondition completion_condition, boost::system::error_code& ec);
352 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
356 * @defgroup async_write boost::asio::async_write
358 * @brief Start an asynchronous operation to write a certain amount of data to a
363 /// Start an asynchronous operation to write all of the supplied data to a
366 * This function is used to asynchronously write a certain number of bytes of
367 * data to a stream. The function call always returns immediately. The
368 * asynchronous operation will continue until one of the following conditions
371 * @li All of the data in the supplied buffers has been written. That is, the
372 * bytes transferred is equal to the sum of the buffer sizes.
374 * @li An error occurred.
376 * This operation is implemented in terms of zero or more calls to the stream's
377 * async_write_some function, and is known as a <em>composed operation</em>. The
378 * program must ensure that the stream performs no other write operations (such
379 * as async_write, the stream's async_write_some function, or any other composed
380 * operations that perform writes) until this operation completes.
382 * @param s The stream to which the data is to be written. The type must support
383 * the AsyncWriteStream concept.
385 * @param buffers One or more buffers containing the data to be written.
386 * Although the buffers object may be copied as necessary, ownership of the
387 * underlying memory blocks is retained by the caller, which must guarantee
388 * that they remain valid until the handler is called.
390 * @param handler The handler to be called when the write operation completes.
391 * Copies will be made of the handler as required. The function signature of
392 * the handler must be:
393 * @code void handler(
394 * const boost::system::error_code& error, // Result of operation.
396 * std::size_t bytes_transferred // Number of bytes written from the
397 * // buffers. If an error occurred,
398 * // this will be less than the sum
399 * // of the buffer sizes.
401 * Regardless of whether the asynchronous operation completes immediately or
402 * not, the handler will not be invoked from within this function. Invocation of
403 * the handler will be performed in a manner equivalent to using
404 * boost::asio::io_service::post().
407 * To write a single data buffer use the @ref buffer function as follows:
409 * boost::asio::async_write(s, boost::asio::buffer(data, size), handler);
411 * See the @ref buffer documentation for information on writing multiple
412 * buffers in one go, and how to use it with arrays, boost::array or
415 template <typename AsyncWriteStream, typename ConstBufferSequence,
416 typename WriteHandler>
417 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
418 void (boost::system::error_code, std::size_t))
419 async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
420 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
422 /// Start an asynchronous operation to write a certain amount of data to a
425 * This function is used to asynchronously write a certain number of bytes of
426 * data to a stream. The function call always returns immediately. The
427 * asynchronous operation will continue until one of the following conditions
430 * @li All of the data in the supplied buffers has been written. That is, the
431 * bytes transferred is equal to the sum of the buffer sizes.
433 * @li The completion_condition function object returns 0.
435 * This operation is implemented in terms of zero or more calls to the stream's
436 * async_write_some function, and is known as a <em>composed operation</em>. The
437 * program must ensure that the stream performs no other write operations (such
438 * as async_write, the stream's async_write_some function, or any other composed
439 * operations that perform writes) until this operation completes.
441 * @param s The stream to which the data is to be written. The type must support
442 * the AsyncWriteStream concept.
444 * @param buffers One or more buffers containing the data to be written.
445 * Although the buffers object may be copied as necessary, ownership of the
446 * underlying memory blocks is retained by the caller, which must guarantee
447 * that they remain valid until the handler is called.
449 * @param completion_condition The function object to be called to determine
450 * whether the write operation is complete. The signature of the function object
452 * @code std::size_t completion_condition(
453 * // Result of latest async_write_some operation.
454 * const boost::system::error_code& error,
456 * // Number of bytes transferred so far.
457 * std::size_t bytes_transferred
459 * A return value of 0 indicates that the write operation is complete. A
460 * non-zero return value indicates the maximum number of bytes to be written on
461 * the next call to the stream's async_write_some function.
463 * @param handler The handler to be called when the write operation completes.
464 * Copies will be made of the handler as required. The function signature of the
466 * @code void handler(
467 * const boost::system::error_code& error, // Result of operation.
469 * std::size_t bytes_transferred // Number of bytes written from the
470 * // buffers. If an error occurred,
471 * // this will be less than the sum
472 * // of the buffer sizes.
474 * Regardless of whether the asynchronous operation completes immediately or
475 * not, the handler will not be invoked from within this function. Invocation of
476 * the handler will be performed in a manner equivalent to using
477 * boost::asio::io_service::post().
480 * To write a single data buffer use the @ref buffer function as follows:
481 * @code boost::asio::async_write(s,
482 * boost::asio::buffer(data, size),
483 * boost::asio::transfer_at_least(32),
485 * See the @ref buffer documentation for information on writing multiple
486 * buffers in one go, and how to use it with arrays, boost::array or
489 template <typename AsyncWriteStream, typename ConstBufferSequence,
490 typename CompletionCondition, typename WriteHandler>
491 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
492 void (boost::system::error_code, std::size_t))
493 async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
494 CompletionCondition completion_condition,
495 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
497 #if !defined(BOOST_ASIO_NO_IOSTREAM)
499 /// Start an asynchronous operation to write all of the supplied data to a
502 * This function is used to asynchronously write a certain number of bytes of
503 * data to a stream. The function call always returns immediately. The
504 * asynchronous operation will continue until one of the following conditions
507 * @li All of the data in the supplied basic_streambuf has been written.
509 * @li An error occurred.
511 * This operation is implemented in terms of zero or more calls to the stream's
512 * async_write_some function, and is known as a <em>composed operation</em>. The
513 * program must ensure that the stream performs no other write operations (such
514 * as async_write, the stream's async_write_some function, or any other composed
515 * operations that perform writes) until this operation completes.
517 * @param s The stream to which the data is to be written. The type must support
518 * the AsyncWriteStream concept.
520 * @param b A basic_streambuf object from which data will be written. Ownership
521 * of the streambuf is retained by the caller, which must guarantee that it
522 * remains valid until the handler is called.
524 * @param handler The handler to be called when the write operation completes.
525 * Copies will be made of the handler as required. The function signature of the
527 * @code void handler(
528 * const boost::system::error_code& error, // Result of operation.
530 * std::size_t bytes_transferred // Number of bytes written from the
531 * // buffers. If an error occurred,
532 * // this will be less than the sum
533 * // of the buffer sizes.
535 * Regardless of whether the asynchronous operation completes immediately or
536 * not, the handler will not be invoked from within this function. Invocation of
537 * the handler will be performed in a manner equivalent to using
538 * boost::asio::io_service::post().
540 template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
541 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
542 void (boost::system::error_code, std::size_t))
543 async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
544 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
546 /// Start an asynchronous operation to write a certain amount of data to a
549 * This function is used to asynchronously write a certain number of bytes of
550 * data to a stream. The function call always returns immediately. The
551 * asynchronous operation will continue until one of the following conditions
554 * @li All of the data in the supplied basic_streambuf has been written.
556 * @li The completion_condition function object returns 0.
558 * This operation is implemented in terms of zero or more calls to the stream's
559 * async_write_some function, and is known as a <em>composed operation</em>. The
560 * program must ensure that the stream performs no other write operations (such
561 * as async_write, the stream's async_write_some function, or any other composed
562 * operations that perform writes) until this operation completes.
564 * @param s The stream to which the data is to be written. The type must support
565 * the AsyncWriteStream concept.
567 * @param b A basic_streambuf object from which data will be written. Ownership
568 * of the streambuf is retained by the caller, which must guarantee that it
569 * remains valid until the handler is called.
571 * @param completion_condition The function object to be called to determine
572 * whether the write operation is complete. The signature of the function object
574 * @code std::size_t completion_condition(
575 * // Result of latest async_write_some operation.
576 * const boost::system::error_code& error,
578 * // Number of bytes transferred so far.
579 * std::size_t bytes_transferred
581 * A return value of 0 indicates that the write operation is complete. A
582 * non-zero return value indicates the maximum number of bytes to be written on
583 * the next call to the stream's async_write_some function.
585 * @param handler The handler to be called when the write operation completes.
586 * Copies will be made of the handler as required. The function signature of the
588 * @code void handler(
589 * const boost::system::error_code& error, // Result of operation.
591 * std::size_t bytes_transferred // Number of bytes written from the
592 * // buffers. If an error occurred,
593 * // this will be less than the sum
594 * // of the buffer sizes.
596 * Regardless of whether the asynchronous operation completes immediately or
597 * not, the handler will not be invoked from within this function. Invocation of
598 * the handler will be performed in a manner equivalent to using
599 * boost::asio::io_service::post().
601 template <typename AsyncWriteStream, typename Allocator,
602 typename CompletionCondition, typename WriteHandler>
603 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
604 void (boost::system::error_code, std::size_t))
605 async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
606 CompletionCondition completion_condition,
607 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
609 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
616 #include <boost/asio/detail/pop_options.hpp>
618 #include <boost/asio/impl/write.hpp>
620 #endif // BOOST_ASIO_WRITE_HPP