Skip to content

Commit 0fc6ac3

Browse files
committed
work in progress
1 parent 612f26e commit 0fc6ac3

4 files changed

Lines changed: 310 additions & 81 deletions

File tree

include/boost/beast2/body_write_stream.hpp

Lines changed: 107 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,41 @@ class body_write_stream
6363

6464
/** Constructor
6565
66-
This constructor creates the stream by forwarding all arguments to the
67-
underlying socket. The socket then needs to be open and connected or
68-
accepted before data can be sent or received on it.
66+
This constructor creates the stream which retains a reference to the
67+
underlying stream. The underlying stream then needs to be open before
68+
data can be written
6969
70-
@param s The underlying stream from which the HTTP message is write.
70+
@param s The underlying stream to which the HTTP message is written.
7171
This object's executor is initialized to that of the
7272
underlying stream.
7373
74-
@param pr A http_proto::parser object which will perform the parsing of
74+
@param sr A http_proto::serializer object which will perform the serialization of
7575
the HTTP message and extraction of the body. This must be
76-
initialized by the caller and ownership of the parser is
76+
initialized by the caller and ownership of the serializer is
7777
retained by the caller, which must guarantee that it remains
7878
valid until the handler is called.
79+
80+
@param srs A http_proto::serializer::stream object which must have been
81+
obtained by a call to `start_stream` on `sr`. Ownership of the
82+
serializer::stream is
83+
retained by the caller, which must guarantee that it remains
84+
valid until the handler is called.
7985
*/
80-
explicit body_write_stream(AsyncWriteStream& s, http_proto::serializer& sr);
86+
explicit body_write_stream(
87+
AsyncWriteStream& s,
88+
http_proto::serializer& sr,
89+
http_proto::serializer::stream& srs);
8190

8291
/** Write some data asynchronously.
8392
84-
This function is used to asynchronously write data from the stream.
93+
This function is used to asynchronously write data to the stream.
8594
8695
This call always returns immediately. The asynchronous operation will
8796
continue until one of the following conditions is true:
8897
89-
@li The HTTP headers are write in full from the underlying stream and one
90-
or more bytes of the body are write from the stream and stored in the
91-
buffer `mb`.
98+
@li One or more bytes are written from `cb` to the body stored in the
99+
serializer and one or more bytes are written from the serializer to the
100+
underlying stream.
92101
93102
@li An error occurs.
94103
@@ -98,13 +107,14 @@ class body_write_stream
98107
implemented using the underlying stream's `async_write_some` are
99108
performed until this operation completes.
100109
101-
@param mb The buffers into which the body data will be write. If the
102-
size of the buffers is zero bytes, the operation always completes
103-
immediately with no error. Although the buffers object may be copied as
104-
necessary, ownership of the underlying memory blocks is retained by the
105-
caller, which must guarantee that they remain valid until the handler is
106-
called. Where the mb buffer is not of sufficient size to hold the write
107-
data, the remainder may be write by subsequent calls to this function.
110+
@param cb The buffer sequence from which the body data will be read. If
111+
the size of the buffer sequence is zero bytes, the operation always
112+
completes immediately with no error. Although the buffers object may be
113+
copied as necessary, ownership of the underlying memory blocks is
114+
retained by the caller, which must guarantee that they remain valid until
115+
the handler is called. Where the internal buffer of the contained
116+
serializer is not of sufficient size to hold the data to be copied from
117+
cb, the remainder may be write by subsequent calls to this function.
108118
109119
@param handler The completion handler to invoke when the operation
110120
completes. The implementation takes ownership of the handler by
@@ -113,7 +123,8 @@ class body_write_stream
113123
@code
114124
void handler(
115125
error_code const& error, // result of operation
116-
std::size_t bytes_transferred // the number of bytes consumed by the parser
126+
std::size_t bytes_transferred // the number of bytes consumed from
127+
// cb by the serializer
117128
);
118129
@endcode
119130
Regardless of whether the asynchronous operation
@@ -122,11 +133,17 @@ class body_write_stream
122133
handler will be performed in a manner equivalent to using
123134
`asio::async_immediate`.
124135
125-
@note The `async_write_some` operation may not receive all of the
136+
@note The `async_write_some` operation may not transmit all of the
126137
requested number of bytes. Consider using the function
127138
`asio::async_write` if you need to ensure that the requested amount of
128139
data is write before the asynchronous operation completes.
129140
141+
@note This function does not guarantee that all of the consumed data is
142+
written to the underlying stream. For this reason one or more calls to
143+
`async_write_some` must be followed by a call to `async_close` to put the
144+
serializer into the `done` state and to write all data remaining in the
145+
serializer to the underlying stream.
146+
130147
@par Per-Operation Cancellation
131148
132149
This asynchronous operation supports cancellation for the following
@@ -148,9 +165,79 @@ class body_write_stream
148165
void(system::error_code, std::size_t))
149166
async_write_some(ConstBufferSequence mb, CompletionToken&& handler);
150167

168+
/** Close serializer::stream and flush remaining data to the underlying stream asynchronously.
169+
170+
This function is used to asynchronously call `close` on the
171+
`serializer::stream` object referenced in the construction of this
172+
`body_write_stream` and write all remaining data in the serializer to the
173+
underlying stream.
174+
175+
This call always returns immediately. The asynchronous operation will
176+
continue until one of the following conditions is true:
177+
178+
@li All remaining output bytes of the serializer are written to the
179+
underlying stream and the serializer's `is_done()` method returns true.
180+
181+
@li An error occurs.
182+
183+
The algorithm, known as a <em>composed asynchronous operation</em>, is
184+
implemented in terms of calls to the underlying stream's
185+
`async_write_some` function. The program must ensure that no other calls
186+
implemented using the underlying stream's `async_write_some` are
187+
performed until this operation completes.
188+
189+
@param handler The completion handler to invoke when the operation
190+
completes. The implementation takes ownership of the handler by
191+
performing a decay-copy. The equivalent function signature of the
192+
handler must be:
193+
@code
194+
void handler(
195+
error_code const& error, // result of operation
196+
std::size_t bytes_transferred // the number of bytes consumed from
197+
// cb by the serializer
198+
);
199+
@endcode
200+
Regardless of whether the asynchronous operation
201+
completes immediately or not, the completion handler will not be invoked
202+
from within this function. On immediate completion, invocation of the
203+
handler will be performed in a manner equivalent to using
204+
`asio::async_immediate`.
205+
206+
@note The `async_write_some` operation may not transmit all of the
207+
requested number of bytes. Consider using the function
208+
`asio::async_write` if you need to ensure that the requested amount of
209+
data is write before the asynchronous operation completes.
210+
211+
@note This function does not guarantee that all of the consumed data is
212+
written to the underlying stream. For this reason one or more calls to
213+
`async_write_some` must be followed by a call to `async_close` to put the
214+
serializer into the `done` state and to write all data remaining in the
215+
serializer to the underlying stream.
216+
217+
@par Per-Operation Cancellation
218+
219+
This asynchronous operation supports cancellation for the following
220+
net::cancellation_type values:
221+
222+
@li @c net::cancellation_type::terminal
223+
@li @c net::cancellation_type::partial
224+
@li @c net::cancellation_type::total
225+
226+
if they are also supported by the underlying stream's @c async_write_some
227+
operation.
228+
*/
229+
template<
230+
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(system::error_code))
231+
CompletionToken>
232+
BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
233+
CompletionToken,
234+
void(system::error_code))
235+
async_close(CompletionToken&& handler);
236+
151237
private:
152238
AsyncWriteStream& stream_;
153239
http_proto::serializer& sr_;
240+
http_proto::serializer::stream& srs_;
154241
};
155242

156243
} // beast2

0 commit comments

Comments
 (0)