04_serializer_streams.qbk 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Serializer Stream Operations]
  8. Non-trivial algorithms need to do more than send entire messages
  9. at once, such as:
  10. * Send the header first, and the body later.
  11. * Send a message incrementally: bounded work in each I/O cycle.
  12. * Use a series of caller-provided buffers to represent the body.
  13. These tasks may be performed by using the serializer stream interfaces.
  14. To use these interfaces, first construct an appropriate serializer
  15. from the message to be sent:
  16. [table Serializer
  17. [[Name][Description]]
  18. [[
  19. __serializer__
  20. ][
  21. ```
  22. /// Provides buffer oriented HTTP message serialization functionality.
  23. template<
  24. bool isRequest,
  25. class Body,
  26. class Fields = fields
  27. >
  28. class serializer;
  29. ```
  30. ]]
  31. [[
  32. [link beast.ref.boost__beast__http__request_serializer `request_serializer`]
  33. ][
  34. ```
  35. /// A serializer for HTTP/1 requests
  36. template<
  37. class Body,
  38. class Fields = fields
  39. >
  40. using request_serializer = serializer<true, Body, Fields>;
  41. ```
  42. ]]
  43. [[
  44. [link beast.ref.boost__beast__http__response_serializer `response_serializer`]
  45. ][
  46. ```
  47. /// A serializer for HTTP/1 responses
  48. template<
  49. class Body,
  50. class Fields = fields
  51. >
  52. using response_serializer = serializer<false, Body, Fields>;
  53. ```
  54. ]]
  55. ]
  56. The choices for template types must match the message passed on construction.
  57. This code creates an HTTP response and the corresponding serializer:
  58. [http_snippet_10]
  59. The stream operations which work on serializers are:
  60. [table Serializer Stream Operations
  61. [[Name][Description]]
  62. [[
  63. [link beast.ref.boost__beast__http__write.overload1 [*write]]
  64. ][
  65. Send everything in a __serializer__ to a __SyncWriteStream__.
  66. ]]
  67. [[
  68. [link beast.ref.boost__beast__http__async_write.overload1 [*async_write]]
  69. ][
  70. Send everything in a __serializer__ asynchronously to an __AsyncWriteStream__.
  71. ]]
  72. [[
  73. [link beast.ref.boost__beast__http__write_header.overload1 [*write_header]]
  74. ][
  75. Send only the header from a __serializer__ to a __SyncWriteStream__.
  76. ]]
  77. [[
  78. [link beast.ref.boost__beast__http__async_write_header [*async_write_header]]
  79. ][
  80. Send only the header from a __serializer__ asynchronously to an __AsyncWriteStream__.
  81. ]]
  82. [[
  83. [link beast.ref.boost__beast__http__write_some.overload1 [*write_some]]
  84. ][
  85. Send part of a __serializer__ to a __SyncWriteStream__.
  86. ]]
  87. [[
  88. [link beast.ref.boost__beast__http__async_write_some [*async_write_some]]
  89. ][
  90. Send part of a __serializer__ asynchronously to an __AsyncWriteStream__.
  91. ]]
  92. ]
  93. Here is an example of using a serializer to send a message on a stream
  94. synchronously. This performs the same operation as calling `write(stream, m)`:
  95. [http_snippet_12]
  96. [endsect]