File: 04_serializer_streams.qbk

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (112 lines) | stat: -rw-r--r-- 3,037 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[/
    Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

    Official repository: https://github.com/boostorg/beast
]

[section Serializer Stream Operations]

Non-trivial algorithms need to do more than send entire messages
at once, such as:

* Send the header first, and the body later.

* Send a message incrementally: bounded work in each I/O cycle.

* Use a series of caller-provided buffers to represent the body.

These tasks may be performed by using the serializer stream interfaces.
To use these interfaces, first construct an appropriate serializer
from the message to be sent:

[table Serializer
[[Name][Description]]
[[
    __serializer__
][
    ```
    /// Provides buffer oriented HTTP message serialization functionality.
    template<
        bool isRequest,
        class Body,
        class Fields = fields
    >
    class serializer;
    ```
]]
[[
    [link beast.ref.boost__beast__http__request_serializer `request_serializer`]
][
    ```
    /// A serializer for HTTP/1 requests
    template<
        class Body,
        class Fields = fields
    >
    using request_serializer = serializer<true, Body, Fields>;
    ```
]]
[[
    [link beast.ref.boost__beast__http__response_serializer `response_serializer`]
][
    ```
    /// A serializer for HTTP/1 responses
    template<
        class Body,
        class Fields = fields
    >
    using response_serializer = serializer<false, Body, Fields>;
    ```
]]
]

The choices for template types must match the message passed on construction.
This code creates an HTTP response and the corresponding serializer:

[http_snippet_10]

The stream operations which work on serializers are:

[table Serializer Stream Operations
[[Name][Description]]
[[
    [link beast.ref.boost__beast__http__write.overload1 [*write]]
][
    Send everything in a __serializer__ to a __SyncWriteStream__.
]]
[[
    [link beast.ref.boost__beast__http__async_write.overload1 [*async_write]]
][
    Send everything in a __serializer__ asynchronously to an __AsyncWriteStream__.
]]
[[
    [link beast.ref.boost__beast__http__write_header.overload1 [*write_header]]
][
    Send only the header from a __serializer__ to a __SyncWriteStream__.
]]
[[
    [link beast.ref.boost__beast__http__async_write_header [*async_write_header]]
][
    Send only the header from a __serializer__ asynchronously to an __AsyncWriteStream__.
]]
[[
    [link beast.ref.boost__beast__http__write_some.overload1 [*write_some]]
][
    Send part of a __serializer__ to a __SyncWriteStream__.
]]
[[
    [link beast.ref.boost__beast__http__async_write_some [*async_write_some]]
][
    Send part of a __serializer__ asynchronously to an __AsyncWriteStream__.
]]
]

Here is an example of using a serializer to send a message on a stream
synchronously. This performs the same operation as calling `write(stream, m)`:

[http_snippet_12]

[endsect]