06_serializer_buffers.qbk 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [section Buffer-Oriented Serializing]
  8. [block'''<?dbhtml stop-chunking?>''']
  9. An instance of __serializer__ can be invoked directly, without using
  10. the provided stream operations. This could be useful for implementing
  11. algorithms on objects whose interface does not conform to __Stream__.
  12. For example, a
  13. [@https://github.com/libuv/libuv *libuv* socket].
  14. The serializer interface is interactive; the caller invokes it repeatedly
  15. to produce buffers until all of the serialized octets have been generated.
  16. Then the serializer is destroyed.
  17. To obtain the serialized next buffer sequence, call
  18. [link beast.ref.boost__beast__http__serializer.next `serializer::next`].
  19. Then, call
  20. [link beast.ref.boost__beast__http__serializer.consume `serializer::consume`]
  21. to indicate the number of bytes consumed. This updates the next
  22. set of buffers to be returned, if any.
  23. `serializer::next` takes an error code parameter and invokes a visitor
  24. argument with the error code and buffer of unspecified type. In C++14
  25. this is easily expressed with a generic lambda. The function
  26. [link beast.ref.boost__beast__http__serializer.is_done `serializer::is_done`]
  27. will return `true` when all the buffers have been produced. This C++14
  28. example prints the buffers to standard output:
  29. [http_snippet_14]
  30. Generic lambda expressions are only available in C++14 or later. A functor
  31. with a templated function call operator is necessary to use C++11 as shown:
  32. [http_snippet_15]
  33. [heading Split Serialization]
  34. In some cases, such as the handling of the
  35. [@https://tools.ietf.org/html/rfc7231#section-5.1.1 Expect: 100-continue]
  36. field, it may be desired to first serialize the header, perform some other
  37. action, and then continue with serialization of the body. This is
  38. accomplished by calling
  39. [link beast.ref.boost__beast__http__serializer.split `serializer::split`]
  40. with a boolean indicating that when buffers are produced, the last buffer
  41. containing serialized header octets will not contain any octets corresponding
  42. to the body. The function
  43. [link beast.ref.boost__beast__http__serializer.is_header_done `serializer::is_header_done`]
  44. informs the caller whether the header been serialized fully. In this
  45. C++14 example we print the header first, followed by the body:
  46. [http_snippet_16]
  47. [section:write_to_std_ostream Write To std::ostream __example__]
  48. The standard library provides the type `std::ostream` for performing high
  49. level write operations on character streams. The variable `std::cout` is
  50. based on this output stream. This example uses the buffer oriented interface
  51. of __serializer__ to write an HTTP message to a `std::ostream`:
  52. [example_http_write_ostream]
  53. [tip
  54. Serializing to a `std::ostream` could be implemented using an alternate
  55. strategy: adapt the `std::ostream` interface to a __SyncWriteStream__,
  56. enabling use with the library's existing stream algorithms. This is
  57. left as an exercise for the reader.
  58. ]
  59. [endsect]
  60. [endsect]