buffers.qbk 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:buffers Buffers]
  8. Fundamentally, I/O involves the transfer of data to and from contiguous regions
  9. of memory, called buffers. These buffers can be simply expressed as a tuple
  10. consisting of a pointer and a size in bytes. However, to allow the development
  11. of efficient network applications, Boost.Asio includes support for scatter-gather
  12. operations. These operations involve one or more buffers:
  13. * A scatter-read receives data into multiple buffers.
  14. * A gather-write transmits multiple buffers.
  15. Therefore we require an abstraction to represent a collection of buffers. The
  16. approach used in Boost.Asio is to define a type (actually two types) to
  17. represent a single buffer. These can be stored in a container, which may be
  18. passed to the scatter-gather operations.
  19. In addition to specifying buffers as a pointer and size in bytes, Boost.Asio makes a
  20. distinction between modifiable memory (called mutable) and non-modifiable
  21. memory (where the latter is created from the storage for a const-qualified
  22. variable). These two types could therefore be defined as follows:
  23. typedef std::pair<void*, std::size_t> mutable_buffer;
  24. typedef std::pair<const void*, std::size_t> const_buffer;
  25. Here, a mutable_buffer would be convertible to a const_buffer, but conversion
  26. in the opposite direction is not valid.
  27. However, Boost.Asio does not use the above definitions as-is, but instead defines two
  28. classes: `mutable_buffer` and `const_buffer`. The goal of these is to provide
  29. an opaque representation of contiguous memory, where:
  30. * Types behave as std::pair would in conversions. That is, a `mutable_buffer` is
  31. convertible to a `const_buffer`, but the opposite conversion is disallowed.
  32. * There is protection against buffer overruns. Given a buffer instance, a user
  33. can only create another buffer representing the same range of memory or a
  34. sub-range of it. To provide further safety, the library also includes
  35. mechanisms for automatically determining the size of a buffer from an array,
  36. `boost::array` or `std::vector` of POD elements, or from a `std::string`.
  37. * The underlying memory is explicitly accessed using the `data()` member
  38. function. In general an application should never need to do this, but it is
  39. required by the library implementation to pass the raw memory to the
  40. underlying operating system functions.
  41. Finally, multiple buffers can be passed to scatter-gather operations (such as
  42. [link boost_asio.reference.read read()] or [link boost_asio.reference.write write()]) by
  43. putting the buffer objects into a container. The `MutableBufferSequence` and
  44. `ConstBufferSequence` concepts have been defined so that containers such as
  45. `std::vector`, `std::list`, `std::array` or `boost::array` can be used.
  46. [heading Streambuf for Integration with Iostreams]
  47. The class `boost::asio::basic_streambuf` is derived from `std::basic_streambuf` to
  48. associate the input sequence and output sequence with one or more objects of
  49. some character array type, whose elements store arbitrary values. These
  50. character array objects are internal to the streambuf object, but direct access
  51. to the array elements is provided to permit them to be used with I/O
  52. operations, such as the send or receive operations of a socket:
  53. * The input sequence of the streambuf is accessible via the [link
  54. boost_asio.reference.basic_streambuf.data data()] member function. The return type
  55. of this function meets the `ConstBufferSequence` requirements.
  56. * The output sequence of the streambuf is accessible via the [link
  57. boost_asio.reference.basic_streambuf.prepare prepare()] member function. The return
  58. type of this function meets the `MutableBufferSequence` requirements.
  59. * Data is transferred from the front of the output sequence to the back of the
  60. input sequence by calling the [link boost_asio.reference.basic_streambuf.commit
  61. commit()] member function.
  62. * Data is removed from the front of the input sequence by calling the [link
  63. boost_asio.reference.basic_streambuf.consume consume()] member function.
  64. The streambuf constructor accepts a `size_t` argument specifying the maximum of
  65. the sum of the sizes of the input sequence and output sequence. Any operation
  66. that would, if successful, grow the internal data beyond this limit will throw
  67. a `std::length_error` exception.
  68. [heading Bytewise Traversal of Buffer Sequences]
  69. The `buffers_iterator<>` class template allows buffer sequences (i.e. types
  70. meeting `MutableBufferSequence` or `ConstBufferSequence` requirements) to be
  71. traversed as though they were a contiguous sequence of bytes. Helper functions
  72. called buffers_begin() and buffers_end() are also provided, where the
  73. buffers_iterator<> template parameter is automatically deduced.
  74. As an example, to read a single line from a socket and into a `std::string`,
  75. you may write:
  76. boost::asio::streambuf sb;
  77. ...
  78. std::size_t n = boost::asio::read_until(sock, sb, '\n');
  79. boost::asio::streambuf::const_buffers_type bufs = sb.data();
  80. std::string line(
  81. boost::asio::buffers_begin(bufs),
  82. boost::asio::buffers_begin(bufs) + n);
  83. [heading Buffer Debugging]
  84. Some standard library implementations, such as the one that ships with
  85. Microsoft Visual C++ 8.0 and later, provide a feature called iterator
  86. debugging. What this means is that the validity of iterators is checked at
  87. runtime. If a program tries to use an iterator that has been invalidated, an
  88. assertion will be triggered. For example:
  89. std::vector<int> v(1)
  90. std::vector<int>::iterator i = v.begin();
  91. v.clear(); // invalidates iterators
  92. *i = 0; // assertion!
  93. Boost.Asio takes advantage of this feature to add buffer debugging. Consider the
  94. following code:
  95. void dont_do_this()
  96. {
  97. std::string msg = "Hello, world!";
  98. boost::asio::async_write(sock, boost::asio::buffer(msg), my_handler);
  99. }
  100. When you call an asynchronous read or write you need to ensure that the buffers
  101. for the operation are valid until the completion handler is called. In the
  102. above example, the buffer is the `std::string` variable `msg`. This variable is
  103. on the stack, and so it goes out of scope before the asynchronous operation
  104. completes. If you're lucky then the application will crash, but random failures
  105. are more likely.
  106. When buffer debugging is enabled, Boost.Asio stores an iterator into the string until
  107. the asynchronous operation completes, and then dereferences it to check its
  108. validity. In the above example you would observe an assertion failure just
  109. before Boost.Asio tries to call the completion handler.
  110. This feature is automatically made available for Microsoft Visual Studio 8.0 or
  111. later and for GCC when `_GLIBCXX_DEBUG` is defined. There is a performance cost
  112. to this checking, so buffer debugging is only enabled in debug builds. For
  113. other compilers it may be enabled by defining `BOOST_ASIO_ENABLE_BUFFER_DEBUGGING`.
  114. It can also be explicitly disabled by defining `BOOST_ASIO_DISABLE_BUFFER_DEBUGGING`.
  115. [heading See Also]
  116. [link boost_asio.reference.buffer buffer],
  117. [link boost_asio.reference.buffers_begin buffers_begin],
  118. [link boost_asio.reference.buffers_end buffers_end],
  119. [link boost_asio.reference.buffers_iterator buffers_iterator],
  120. [link boost_asio.reference.const_buffer const_buffer],
  121. [link boost_asio.reference.const_buffers_1 const_buffers_1],
  122. [link boost_asio.reference.mutable_buffer mutable_buffer],
  123. [link boost_asio.reference.mutable_buffers_1 mutable_buffers_1],
  124. [link boost_asio.reference.streambuf streambuf],
  125. [link boost_asio.reference.ConstBufferSequence ConstBufferSequence],
  126. [link boost_asio.reference.MutableBufferSequence MutableBufferSequence],
  127. [link boost_asio.examples.cpp03_examples.buffers buffers example (C++03)],
  128. [link boost_asio.examples.cpp11_examples.buffers buffers example (c++11)].
  129. [endsect]