rwebtransport 0.2.2 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +523 -140
- package/Cargo.toml +5 -5
- package/README.md +8 -4
- package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
- package/crates/native/Cargo.toml +2 -0
- package/crates/native/src/config.rs +69 -8
- package/crates/native/src/driver.rs +253 -90
- package/crates/native/src/lib.rs +80 -57
- package/crates/native/src/parser_memory.rs +203 -0
- package/crates/native/src/server.rs +215 -183
- package/crates/native/src/session.rs +1341 -243
- package/crates/native/src/test_support.rs +119 -0
- package/crates/wtcore/Cargo.toml +1 -0
- package/crates/wtcore/src/h3.rs +250 -46
- package/dist/index.js +218 -75
- package/dist/index.js.map +7 -1
- package/dist/index.mjs +214 -69
- package/dist/index.mjs.map +7 -1
- package/dist/native.d.ts +10 -4
- package/dist/server.d.ts +1 -0
- package/package.json +84 -78
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/build-linux-prebuilt.sh +46 -0
- package/scripts/build-ts.js +41 -0
- package/scripts/build.js +10 -3
- package/vendor/quiche/AGENTS.md +10 -0
- package/vendor/quiche/Cargo.lock +239 -248
- package/vendor/quiche/Cargo.toml +23 -10
- package/vendor/quiche/examples/Makefile +4 -4
- package/vendor/quiche/examples/client.rs +10 -28
- package/vendor/quiche/examples/http3-client.rs +20 -34
- package/vendor/quiche/examples/http3-server.rs +47 -78
- package/vendor/quiche/examples/server.rs +19 -49
- package/vendor/quiche/include/quiche.h +35 -4
- package/vendor/quiche/src/build.rs +17 -0
- package/vendor/quiche/src/cid.rs +129 -61
- package/vendor/quiche/src/crypto/boringssl.rs +98 -86
- package/vendor/quiche/src/crypto/mod.rs +64 -77
- package/vendor/quiche/src/dgram.rs +2 -3
- package/vendor/quiche/src/error.rs +22 -20
- package/vendor/quiche/src/ffi.rs +558 -280
- package/vendor/quiche/src/flowcontrol.rs +0 -7
- package/vendor/quiche/src/frame.rs +134 -135
- package/vendor/quiche/src/h3/ffi.rs +73 -60
- package/vendor/quiche/src/h3/frame.rs +70 -90
- package/vendor/quiche/src/h3/mod.rs +1024 -593
- package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
- package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
- package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
- package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
- package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
- package/vendor/quiche/src/h3/stream.rs +1009 -194
- package/vendor/quiche/src/lib.rs +1361 -1195
- package/vendor/quiche/src/minmax.rs +2 -6
- package/vendor/quiche/src/packet.rs +420 -552
- package/vendor/quiche/src/path.rs +202 -72
- package/vendor/quiche/src/pmtud.rs +18 -16
- package/vendor/quiche/src/ranges.rs +184 -82
- package/vendor/quiche/src/recovery/AGENTS.md +5 -2
- package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
- package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
- package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
- package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
- package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
- package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
- package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
- package/vendor/quiche/src/recovery/mod.rs +414 -173
- package/vendor/quiche/src/recovery/rtt.rs +8 -5
- package/vendor/quiche/src/stream/mod.rs +864 -133
- package/vendor/quiche/src/stream/recv_buf.rs +476 -172
- package/vendor/quiche/src/stream/send_buf.rs +343 -81
- package/vendor/quiche/src/test_utils.rs +78 -64
- package/vendor/quiche/src/tests.rs +2559 -1335
- package/vendor/quiche/src/tls/boringssl.rs +60 -63
- package/vendor/quiche/src/tls/mod.rs +146 -193
- package/vendor/quiche/src/transport_params.rs +87 -103
|
@@ -28,6 +28,8 @@ use std::cmp;
|
|
|
28
28
|
|
|
29
29
|
use std::collections::BTreeMap;
|
|
30
30
|
use std::collections::VecDeque;
|
|
31
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
32
|
+
use std::sync::Arc;
|
|
31
33
|
|
|
32
34
|
use std::time::Duration;
|
|
33
35
|
use std::time::Instant;
|
|
@@ -41,6 +43,48 @@ use crate::flowcontrol;
|
|
|
41
43
|
|
|
42
44
|
use crate::range_buf::RangeBuf;
|
|
43
45
|
|
|
46
|
+
/// Allocation budget shared by every stream on a connection, optionally with
|
|
47
|
+
/// a parent budget shared by the listener.
|
|
48
|
+
#[derive(Debug)]
|
|
49
|
+
pub(crate) struct ReceiveMemory {
|
|
50
|
+
limit: usize,
|
|
51
|
+
used: AtomicUsize,
|
|
52
|
+
parent: Option<Arc<Self>>,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
impl ReceiveMemory {
|
|
56
|
+
pub(crate) fn new(limit: usize, parent: Option<Arc<Self>>) -> Arc<Self> {
|
|
57
|
+
Arc::new(Self {
|
|
58
|
+
limit,
|
|
59
|
+
used: AtomicUsize::new(0),
|
|
60
|
+
parent,
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#[allow(deprecated)] // Keep the vendored crate's Rust 1.88 compatibility.
|
|
65
|
+
fn reserve(&self, bytes: usize) -> Result<()> {
|
|
66
|
+
self.used
|
|
67
|
+
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |used| {
|
|
68
|
+
used.checked_add(bytes).filter(|&next| next <= self.limit)
|
|
69
|
+
})
|
|
70
|
+
.map_err(|_| Error::StreamMemoryLimit)?;
|
|
71
|
+
if let Some(parent) = &self.parent {
|
|
72
|
+
if let Err(e) = parent.reserve(bytes) {
|
|
73
|
+
self.used.fetch_sub(bytes, Ordering::Relaxed);
|
|
74
|
+
return Err(e);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
Ok(())
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn release(&self, bytes: usize) {
|
|
81
|
+
self.used.fetch_sub(bytes, Ordering::Relaxed);
|
|
82
|
+
if let Some(parent) = &self.parent {
|
|
83
|
+
parent.release(bytes);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
44
88
|
/// Receive-side stream buffer.
|
|
45
89
|
///
|
|
46
90
|
/// Stream data received by the peer is buffered in a list of data chunks
|
|
@@ -52,6 +96,9 @@ pub struct RecvBuf {
|
|
|
52
96
|
/// the application, ordered by offset.
|
|
53
97
|
data: BTreeMap<u64, RangeBuf>,
|
|
54
98
|
|
|
99
|
+
memory: Option<Arc<ReceiveMemory>>,
|
|
100
|
+
reserved: usize,
|
|
101
|
+
|
|
55
102
|
/// The lowest data offset that has yet to be read by the application.
|
|
56
103
|
off: u64,
|
|
57
104
|
|
|
@@ -66,30 +113,70 @@ pub struct RecvBuf {
|
|
|
66
113
|
|
|
67
114
|
/// The error code received via RESET_STREAM.
|
|
68
115
|
error: Option<u64>,
|
|
116
|
+
/// After a reset, the receiver still owes the application this prefix.
|
|
117
|
+
reliable_size: u64,
|
|
118
|
+
reset_pending: bool,
|
|
69
119
|
|
|
70
120
|
/// Whether incoming data is validated but not buffered.
|
|
71
121
|
drain: bool,
|
|
72
122
|
}
|
|
73
123
|
|
|
124
|
+
impl Drop for RecvBuf {
|
|
125
|
+
fn drop(&mut self) {
|
|
126
|
+
self.clear();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
74
130
|
impl RecvBuf {
|
|
75
131
|
/// Creates a new receive buffer.
|
|
76
132
|
pub fn new(max_data: u64, initial_window: u64, max_window: u64) -> RecvBuf {
|
|
77
|
-
RecvBuf
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
133
|
+
let mut recv = RecvBuf::default();
|
|
134
|
+
recv.flow_control = flowcontrol::FlowControl::new(max_data, initial_window, max_window);
|
|
135
|
+
recv
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
pub(crate) fn set_memory_budget(&mut self, budget: Option<Arc<ReceiveMemory>>) {
|
|
139
|
+
if self.memory.is_none() {
|
|
140
|
+
self.memory = budget;
|
|
84
141
|
}
|
|
85
142
|
}
|
|
86
143
|
|
|
144
|
+
fn allocation_cost(buf: &RangeBuf) -> usize {
|
|
145
|
+
// Charge the backing allocation even when the retained slice is small.
|
|
146
|
+
// Include space for the range, tree entry and allocation bookkeeping.
|
|
147
|
+
buf.data.as_ref().len() + 192
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn insert(&mut self, buf: RangeBuf) -> Result<()> {
|
|
151
|
+
if let Some(memory) = &self.memory {
|
|
152
|
+
let cost = Self::allocation_cost(&buf);
|
|
153
|
+
memory.reserve(cost)?;
|
|
154
|
+
self.reserved += cost;
|
|
155
|
+
}
|
|
156
|
+
if let Some(previous) = self.data.insert(buf.max_off(), buf) {
|
|
157
|
+
self.release(Self::allocation_cost(&previous));
|
|
158
|
+
}
|
|
159
|
+
Ok(())
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
fn release(&mut self, bytes: usize) {
|
|
163
|
+
if let Some(memory) = &self.memory {
|
|
164
|
+
self.reserved -= bytes;
|
|
165
|
+
memory.release(bytes);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fn clear(&mut self) {
|
|
170
|
+
self.data.clear();
|
|
171
|
+
self.release(self.reserved);
|
|
172
|
+
}
|
|
173
|
+
|
|
87
174
|
/// Inserts the given chunk of data in the buffer.
|
|
88
175
|
///
|
|
89
176
|
/// This also takes care of enforcing stream flow control limits, as well
|
|
90
177
|
/// as handling incoming data that overlaps data that is already in the
|
|
91
178
|
/// buffer.
|
|
92
|
-
pub fn write(&mut self, buf: RangeBuf) -> Result<()> {
|
|
179
|
+
pub fn write(&mut self, mut buf: RangeBuf) -> Result<()> {
|
|
93
180
|
if buf.max_off() > self.max_data() {
|
|
94
181
|
return Err(Error::FlowControl);
|
|
95
182
|
}
|
|
@@ -111,32 +198,51 @@ impl RecvBuf {
|
|
|
111
198
|
return Err(Error::FinalSize);
|
|
112
199
|
}
|
|
113
200
|
|
|
201
|
+
if self.error.is_some() {
|
|
202
|
+
if self.drain || buf.off() >= self.reliable_size {
|
|
203
|
+
return Ok(());
|
|
204
|
+
}
|
|
205
|
+
if buf.max_off() > self.reliable_size {
|
|
206
|
+
let _ = buf.split_off((self.reliable_size - buf.off()) as usize);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
114
210
|
// We already saved the final offset, so there's nothing else we
|
|
115
211
|
// need to keep from the RangeBuf if it's empty.
|
|
116
212
|
if self.fin_off.is_some() && buf.is_empty() {
|
|
117
213
|
return Ok(());
|
|
118
214
|
}
|
|
119
215
|
|
|
120
|
-
|
|
216
|
+
let new_fin = buf.fin() && self.fin_off.is_none();
|
|
217
|
+
|
|
218
|
+
if new_fin {
|
|
121
219
|
self.fin_off = Some(buf.max_off());
|
|
122
220
|
}
|
|
123
221
|
|
|
124
|
-
// No need to store empty buffer that doesn't carry the fin flag
|
|
222
|
+
// No need to store empty buffer that doesn't carry the fin flag, but
|
|
223
|
+
// its offset still advances the largest received offset used by flow
|
|
224
|
+
// control (RFC 9000 Section 19.8).
|
|
125
225
|
if !buf.fin() && buf.is_empty() {
|
|
226
|
+
self.len = cmp::max(self.len, buf.max_off());
|
|
227
|
+
|
|
228
|
+
if self.drain {
|
|
229
|
+
// we are not storing any data, off == len
|
|
230
|
+
self.off = self.len;
|
|
231
|
+
}
|
|
232
|
+
|
|
126
233
|
return Ok(());
|
|
127
234
|
}
|
|
128
235
|
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
// By this point all spurious empty buffers should have already been
|
|
136
|
-
// discarded, so allowing empty buffers here should be safe.
|
|
137
|
-
if !buf.is_empty() {
|
|
236
|
+
// Discard consumed data unless it carries the first FIN. Such data is
|
|
237
|
+
// replaced with an empty buffer at the final size, so the application
|
|
238
|
+
// reads the FIN without receiving duplicate data and the queued buffer
|
|
239
|
+
// does not keep the duplicate payload alive.
|
|
240
|
+
if self.off >= buf.max_off() && !buf.is_empty() {
|
|
241
|
+
if !new_fin {
|
|
138
242
|
return Ok(());
|
|
139
243
|
}
|
|
244
|
+
|
|
245
|
+
buf = RangeBuf::from(b"", buf.max_off(), true);
|
|
140
246
|
}
|
|
141
247
|
|
|
142
248
|
let mut tmp_bufs = VecDeque::with_capacity(2);
|
|
@@ -179,8 +285,7 @@ impl RecvBuf {
|
|
|
179
285
|
|
|
180
286
|
// New buffer's end overlaps existing buffer.
|
|
181
287
|
if off < b.off() && buf.max_off() > b.off() {
|
|
182
|
-
tmp_bufs
|
|
183
|
-
.push_back(buf.split_off((b.off() - off) as usize));
|
|
288
|
+
tmp_bufs.push_back(buf.split_off((b.off() - off) as usize));
|
|
184
289
|
}
|
|
185
290
|
}
|
|
186
291
|
}
|
|
@@ -188,7 +293,7 @@ impl RecvBuf {
|
|
|
188
293
|
self.len = cmp::max(self.len, buf.max_off());
|
|
189
294
|
|
|
190
295
|
if !self.drain {
|
|
191
|
-
self.
|
|
296
|
+
self.insert(buf)?;
|
|
192
297
|
} else {
|
|
193
298
|
// we are not storing any data, off == len
|
|
194
299
|
self.off = self.len;
|
|
@@ -228,7 +333,8 @@ impl RecvBuf {
|
|
|
228
333
|
/// On success the amount of data read or discarded, and a flag indicating
|
|
229
334
|
/// if there is no more data in the buffer, are returned as a tuple.
|
|
230
335
|
pub fn emit_or_discard<B: bytes::BufMut>(
|
|
231
|
-
&mut self,
|
|
336
|
+
&mut self,
|
|
337
|
+
mut action: RecvAction<B>,
|
|
232
338
|
) -> Result<(usize, bool)> {
|
|
233
339
|
let mut len = 0;
|
|
234
340
|
let mut cap = match &action {
|
|
@@ -242,12 +348,16 @@ impl RecvBuf {
|
|
|
242
348
|
|
|
243
349
|
// The stream was reset, so clear its data and return the error code
|
|
244
350
|
// instead.
|
|
245
|
-
if let Some(e) = self.error {
|
|
246
|
-
self.
|
|
351
|
+
if let Some(e) = self.error.filter(|_| self.is_fin()) {
|
|
352
|
+
self.clear();
|
|
353
|
+
self.reset_pending = false;
|
|
247
354
|
return Err(Error::StreamReset(e));
|
|
248
355
|
}
|
|
356
|
+
if self.error.is_some() {
|
|
357
|
+
cap = cap.min((self.reliable_size - self.off) as usize);
|
|
358
|
+
}
|
|
249
359
|
|
|
250
|
-
while
|
|
360
|
+
while self.ready() && (self.error.is_none() || self.off < self.reliable_size) {
|
|
251
361
|
let mut entry = match self.data.first_entry() {
|
|
252
362
|
Some(entry) => entry,
|
|
253
363
|
None => break,
|
|
@@ -255,6 +365,10 @@ impl RecvBuf {
|
|
|
255
365
|
|
|
256
366
|
let buf = entry.get_mut();
|
|
257
367
|
|
|
368
|
+
if cap == 0 && !buf.is_empty() {
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
|
|
258
372
|
let buf_len = cmp::min(buf.len(), cap);
|
|
259
373
|
|
|
260
374
|
// Only copy data if we're emitting, not discarding.
|
|
@@ -263,10 +377,7 @@ impl RecvBuf {
|
|
|
263
377
|
// impls are allowed to grow the buffer, so we
|
|
264
378
|
// check here that we still have at least
|
|
265
379
|
// `cap` bytes, but we can't require equality
|
|
266
|
-
debug_assert!(
|
|
267
|
-
cap <= out.remaining_mut(),
|
|
268
|
-
"We updated `cap` incorrectly"
|
|
269
|
-
);
|
|
380
|
+
debug_assert!(cap <= out.remaining_mut(), "We updated `cap` incorrectly");
|
|
270
381
|
out.put_slice(&buf[..buf_len])
|
|
271
382
|
}
|
|
272
383
|
|
|
@@ -282,19 +393,47 @@ impl RecvBuf {
|
|
|
282
393
|
break;
|
|
283
394
|
}
|
|
284
395
|
|
|
285
|
-
entry.remove();
|
|
396
|
+
let removed = entry.remove();
|
|
397
|
+
self.release(Self::allocation_cost(&removed));
|
|
286
398
|
}
|
|
287
399
|
|
|
288
400
|
// Update consumed bytes for flow control.
|
|
289
401
|
self.flow_control.add_consumed(len as u64);
|
|
290
402
|
|
|
291
|
-
|
|
403
|
+
if self.error.is_some() && self.off == self.reliable_size {
|
|
404
|
+
self.off = self.len;
|
|
405
|
+
self.clear();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
Ok((len, self.is_fin() && self.error.is_none()))
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/// The final size supplied by FIN or RESET_STREAM, if known.
|
|
412
|
+
pub fn final_size(&self) -> Option<u64> {
|
|
413
|
+
self.fin_off
|
|
292
414
|
}
|
|
293
415
|
|
|
294
416
|
/// Resets the stream at the given offset.
|
|
295
|
-
pub fn reset(
|
|
296
|
-
|
|
417
|
+
pub fn reset(&mut self, error_code: u64, final_size: u64) -> Result<RecvBufResetReturn> {
|
|
418
|
+
self.reset_at(error_code, final_size, 0)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
pub fn reset_error(&self) -> Option<u64> {
|
|
422
|
+
self.error
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/// Signal the reset after delivering the reliable prefix, releasing credit
|
|
426
|
+
/// for the tail immediately.
|
|
427
|
+
pub fn reset_at(
|
|
428
|
+
&mut self,
|
|
429
|
+
error_code: u64,
|
|
430
|
+
final_size: u64,
|
|
431
|
+
reliable_size: u64,
|
|
297
432
|
) -> Result<RecvBufResetReturn> {
|
|
433
|
+
if reliable_size > final_size {
|
|
434
|
+
return Err(Error::InvalidFrame);
|
|
435
|
+
}
|
|
436
|
+
|
|
298
437
|
// Stream's size is already known, forbid changing it.
|
|
299
438
|
if let Some(fin_off) = self.fin_off {
|
|
300
439
|
if fin_off != final_size {
|
|
@@ -307,29 +446,39 @@ impl RecvBuf {
|
|
|
307
446
|
return Err(Error::FinalSize);
|
|
308
447
|
}
|
|
309
448
|
|
|
310
|
-
if self.
|
|
311
|
-
|
|
449
|
+
if final_size > self.max_data() {
|
|
450
|
+
return Err(Error::FlowControl);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if self.error.is_some() && self.is_fin() {
|
|
312
454
|
return Ok(RecvBufResetReturn::zero());
|
|
313
455
|
}
|
|
314
456
|
|
|
315
|
-
|
|
316
|
-
|
|
457
|
+
let notify = !self.drain && (!self.is_fin() || self.ready());
|
|
458
|
+
let previous_limit = if self.error.is_some() {
|
|
459
|
+
self.reliable_size
|
|
460
|
+
} else {
|
|
461
|
+
final_size
|
|
462
|
+
};
|
|
463
|
+
let limit = if self.drain {
|
|
464
|
+
self.off
|
|
465
|
+
} else {
|
|
466
|
+
reliable_size.min(previous_limit).max(self.off)
|
|
467
|
+
};
|
|
317
468
|
let result = RecvBufResetReturn {
|
|
318
469
|
max_data_delta: final_size - self.len,
|
|
319
|
-
consumed_flowcontrol:
|
|
470
|
+
consumed_flowcontrol: previous_limit - limit,
|
|
320
471
|
};
|
|
321
472
|
|
|
322
473
|
self.error = Some(error_code);
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
self.
|
|
326
|
-
|
|
327
|
-
self.
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
let buf = RangeBuf::from(b"", final_size, true);
|
|
332
|
-
self.write(buf)?;
|
|
474
|
+
self.fin_off = Some(final_size);
|
|
475
|
+
self.len = final_size;
|
|
476
|
+
self.reliable_size = limit;
|
|
477
|
+
self.reset_pending = notify;
|
|
478
|
+
if self.off == limit {
|
|
479
|
+
self.off = final_size;
|
|
480
|
+
self.clear();
|
|
481
|
+
}
|
|
333
482
|
|
|
334
483
|
Ok(result)
|
|
335
484
|
}
|
|
@@ -369,9 +518,14 @@ impl RecvBuf {
|
|
|
369
518
|
|
|
370
519
|
self.drain = true;
|
|
371
520
|
|
|
372
|
-
self.
|
|
521
|
+
self.clear();
|
|
373
522
|
|
|
374
|
-
let consumed = self.
|
|
523
|
+
let consumed = if self.error.is_some() {
|
|
524
|
+
self.reliable_size.saturating_sub(self.off)
|
|
525
|
+
} else {
|
|
526
|
+
self.max_off() - self.off
|
|
527
|
+
};
|
|
528
|
+
self.reset_pending = false;
|
|
375
529
|
self.off = self.max_off();
|
|
376
530
|
|
|
377
531
|
Ok(consumed)
|
|
@@ -411,6 +565,9 @@ impl RecvBuf {
|
|
|
411
565
|
|
|
412
566
|
/// Returns true if the stream has data to be read.
|
|
413
567
|
pub fn ready(&self) -> bool {
|
|
568
|
+
if self.error.is_some() && self.is_fin() {
|
|
569
|
+
return self.reset_pending;
|
|
570
|
+
}
|
|
414
571
|
let (_, buf) = match self.data.first_key_value() {
|
|
415
572
|
Some(v) => v,
|
|
416
573
|
None => return false,
|
|
@@ -419,6 +576,41 @@ impl RecvBuf {
|
|
|
419
576
|
buf.off() == self.off
|
|
420
577
|
}
|
|
421
578
|
|
|
579
|
+
/// Returns the number of bytes that can be read contiguously from the
|
|
580
|
+
/// current read offset, up to `max_len`.
|
|
581
|
+
///
|
|
582
|
+
/// Data buffered behind a gap (received out of order) is not counted, so
|
|
583
|
+
/// this never reports bytes that are not yet readable. The cost is
|
|
584
|
+
/// proportional to the number of contiguous buffered chunks at the front
|
|
585
|
+
/// of the buffer, up to `max_len`; no data is copied.
|
|
586
|
+
pub fn readable_len(&self, max_len: usize) -> usize {
|
|
587
|
+
let max_len = if self.error.is_some() {
|
|
588
|
+
max_len.min(self.reliable_size.saturating_sub(self.off) as usize)
|
|
589
|
+
} else {
|
|
590
|
+
max_len
|
|
591
|
+
};
|
|
592
|
+
let mut contiguous = 0usize;
|
|
593
|
+
let mut next_off = self.off;
|
|
594
|
+
|
|
595
|
+
// `data` is ordered by offset, so walk from the front and stop at the
|
|
596
|
+
// first gap (a chunk that does not start where the contiguous run so
|
|
597
|
+
// far leaves off).
|
|
598
|
+
for buf in self.data.values() {
|
|
599
|
+
if buf.off() != next_off {
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
contiguous = contiguous.saturating_add(buf.len()).min(max_len);
|
|
604
|
+
next_off = buf.max_off();
|
|
605
|
+
|
|
606
|
+
if contiguous == max_len {
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
contiguous
|
|
612
|
+
}
|
|
613
|
+
|
|
422
614
|
#[cfg(test)]
|
|
423
615
|
pub(crate) fn flow_control_for_tests(&self) -> &flowcontrol::FlowControl {
|
|
424
616
|
&self.flow_control
|
|
@@ -428,10 +620,67 @@ impl RecvBuf {
|
|
|
428
620
|
#[cfg(test)]
|
|
429
621
|
mod tests {
|
|
430
622
|
use super::*;
|
|
431
|
-
|
|
623
|
+
|
|
624
|
+
/// The default size of the receiver stream flow control window.
|
|
625
|
+
const DEFAULT_STREAM_WINDOW: u64 = 32 * 1024;
|
|
432
626
|
use bytes::BufMut as _;
|
|
433
627
|
use rstest::rstest;
|
|
434
628
|
|
|
629
|
+
#[test]
|
|
630
|
+
fn reliable_reset_delivers_prefix_before_error() {
|
|
631
|
+
let mut recv = RecvBuf::new(32, 32, 32);
|
|
632
|
+
let reset = recv.reset_at(42, 10, 3).unwrap();
|
|
633
|
+
assert_eq!(reset.max_data_delta, 10);
|
|
634
|
+
assert_eq!(reset.consumed_flowcontrol, 7);
|
|
635
|
+
assert!(!recv.ready());
|
|
636
|
+
assert_eq!(recv.reset_at(42, 10, 5), Ok(RecvBufResetReturn::zero()));
|
|
637
|
+
recv.write(RangeBuf::from(b"abcde", 0, false)).unwrap();
|
|
638
|
+
assert_eq!(recv.readable_len(10), 3);
|
|
639
|
+
let mut data = [0; 10];
|
|
640
|
+
assert_eq!(recv.emit(&mut data), Ok((3, false)));
|
|
641
|
+
assert_eq!(&data[..3], b"abc");
|
|
642
|
+
assert_eq!(recv.emit(&mut data), Err(Error::StreamReset(42)));
|
|
643
|
+
assert_eq!(recv.emit(&mut data), Err(Error::Done));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
#[test]
|
|
647
|
+
fn reducing_reliable_size_returns_tail_credit_once() {
|
|
648
|
+
let mut recv = RecvBuf::new(32, 32, 32);
|
|
649
|
+
recv.write(RangeBuf::from(b"abcdef", 0, false)).unwrap();
|
|
650
|
+
assert_eq!(recv.reset_at(1, 10, 6).unwrap().consumed_flowcontrol, 4);
|
|
651
|
+
assert_eq!(recv.emit(&mut [0; 2]), Ok((2, false)));
|
|
652
|
+
assert_eq!(recv.reset_at(1, 10, 3).unwrap().consumed_flowcontrol, 3);
|
|
653
|
+
assert_eq!(recv.shutdown(), Ok(1));
|
|
654
|
+
assert_eq!(recv.reset(1, 10), Ok(RecvBufResetReturn::zero()));
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
#[test]
|
|
658
|
+
fn receive_memory_releases_on_read_shutdown_and_drop() {
|
|
659
|
+
let shared = ReceiveMemory::new(450, None);
|
|
660
|
+
let budget = ReceiveMemory::new(350, Some(shared.clone()));
|
|
661
|
+
let mut first = RecvBuf::new(512, 512, 512);
|
|
662
|
+
let mut second = RecvBuf::new(512, 512, 512);
|
|
663
|
+
first.set_memory_budget(Some(budget.clone()));
|
|
664
|
+
second.set_memory_budget(Some(ReceiveMemory::new(350, Some(shared.clone()))));
|
|
665
|
+
|
|
666
|
+
assert_eq!(first.write(RangeBuf::from(&[0; 64], 0, false)), Ok(()));
|
|
667
|
+
assert_eq!(
|
|
668
|
+
second.write(RangeBuf::from(&[0; 64], 0, false)),
|
|
669
|
+
Err(Error::StreamMemoryLimit)
|
|
670
|
+
);
|
|
671
|
+
assert_eq!(first.emit(&mut [0; 32]), Ok((32, false)));
|
|
672
|
+
assert_eq!(shared.used.load(Ordering::Relaxed), 256);
|
|
673
|
+
assert_eq!(first.emit(&mut [0; 32]), Ok((32, false)));
|
|
674
|
+
assert_eq!(shared.used.load(Ordering::Relaxed), 0);
|
|
675
|
+
assert_eq!(second.write(RangeBuf::from(&[0; 64], 0, false)), Ok(()));
|
|
676
|
+
assert_eq!(second.shutdown(), Ok(64));
|
|
677
|
+
assert_eq!(shared.used.load(Ordering::Relaxed), 0);
|
|
678
|
+
assert_eq!(first.write(RangeBuf::from(&[0; 64], 64, false)), Ok(()));
|
|
679
|
+
drop(first);
|
|
680
|
+
assert_eq!(budget.used.load(Ordering::Relaxed), 0);
|
|
681
|
+
assert_eq!(shared.used.load(Ordering::Relaxed), 0);
|
|
682
|
+
}
|
|
683
|
+
|
|
435
684
|
// Helper function for testing either buffer emit or discard.
|
|
436
685
|
//
|
|
437
686
|
// The `emit` parameter controls whether data is emitted or discarded from
|
|
@@ -450,8 +699,12 @@ mod tests {
|
|
|
450
699
|
// assert is performed against the bytes that were read out of the buffer,
|
|
451
700
|
// to ensure caller expectations are met.
|
|
452
701
|
fn assert_emit_discard(
|
|
453
|
-
recv: &mut RecvBuf,
|
|
454
|
-
|
|
702
|
+
recv: &mut RecvBuf,
|
|
703
|
+
emit: bool,
|
|
704
|
+
target_len: usize,
|
|
705
|
+
result_len: usize,
|
|
706
|
+
is_fin: bool,
|
|
707
|
+
test_bytes: Option<&[u8]>,
|
|
455
708
|
) {
|
|
456
709
|
let mut buf = Vec::<u8>::with_capacity(512).limit(target_len);
|
|
457
710
|
let action = if emit {
|
|
@@ -489,8 +742,7 @@ mod tests {
|
|
|
489
742
|
|
|
490
743
|
#[rstest]
|
|
491
744
|
fn empty_read(#[values(true, false)] emit: bool) {
|
|
492
|
-
let mut recv =
|
|
493
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
745
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
494
746
|
assert_eq!(recv.len, 0);
|
|
495
747
|
|
|
496
748
|
assert_emit_discard_done(&mut recv, emit);
|
|
@@ -498,8 +750,7 @@ mod tests {
|
|
|
498
750
|
|
|
499
751
|
#[rstest]
|
|
500
752
|
fn empty_stream_frame(#[values(true, false)] emit: bool) {
|
|
501
|
-
let mut recv =
|
|
502
|
-
RecvBuf::new(15, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
753
|
+
let mut recv = RecvBuf::new(15, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
503
754
|
assert_eq!(recv.len, 0);
|
|
504
755
|
|
|
505
756
|
let buf = RangeBuf::from(b"hello", 0, false);
|
|
@@ -510,10 +761,10 @@ mod tests {
|
|
|
510
761
|
|
|
511
762
|
assert_emit_discard(&mut recv, emit, 32, 5, false, None);
|
|
512
763
|
|
|
513
|
-
// Don't store non-fin empty buffer.
|
|
764
|
+
// Don't store non-fin empty buffer, but track its offset.
|
|
514
765
|
let buf = RangeBuf::from(b"", 10, false);
|
|
515
766
|
assert!(recv.write(buf).is_ok());
|
|
516
|
-
assert_eq!(recv.len,
|
|
767
|
+
assert_eq!(recv.len, 10);
|
|
517
768
|
assert_eq!(recv.off, 5);
|
|
518
769
|
assert_eq!(recv.data.len(), 0);
|
|
519
770
|
|
|
@@ -521,40 +772,50 @@ mod tests {
|
|
|
521
772
|
let buf = RangeBuf::from(b"", 16, false);
|
|
522
773
|
assert_eq!(recv.write(buf), Err(Error::FlowControl));
|
|
523
774
|
|
|
524
|
-
//
|
|
775
|
+
// A final size below the advanced largest offset is an error
|
|
776
|
+
// (RFC 9000 Section 4.5).
|
|
525
777
|
let buf = RangeBuf::from(b"", 5, true);
|
|
778
|
+
assert_eq!(recv.write(buf), Err(Error::FinalSize));
|
|
779
|
+
|
|
780
|
+
// Store fin empty buffer at the largest received offset.
|
|
781
|
+
let buf = RangeBuf::from(b"", 10, true);
|
|
526
782
|
assert!(recv.write(buf).is_ok());
|
|
527
|
-
assert_eq!(recv.len,
|
|
783
|
+
assert_eq!(recv.len, 10);
|
|
528
784
|
assert_eq!(recv.off, 5);
|
|
529
785
|
assert_eq!(recv.data.len(), 1);
|
|
530
786
|
|
|
531
787
|
// Don't store additional fin empty buffers.
|
|
532
|
-
let buf = RangeBuf::from(b"",
|
|
788
|
+
let buf = RangeBuf::from(b"", 10, true);
|
|
533
789
|
assert!(recv.write(buf).is_ok());
|
|
534
|
-
assert_eq!(recv.len,
|
|
790
|
+
assert_eq!(recv.len, 10);
|
|
535
791
|
assert_eq!(recv.off, 5);
|
|
536
792
|
assert_eq!(recv.data.len(), 1);
|
|
537
793
|
|
|
538
|
-
//
|
|
539
|
-
let buf = RangeBuf::from(b"aa",
|
|
794
|
+
// Accept another fin buffer with the same final size.
|
|
795
|
+
let buf = RangeBuf::from(b"aa", 8, true);
|
|
540
796
|
assert!(recv.write(buf).is_ok());
|
|
541
|
-
assert_eq!(recv.len,
|
|
797
|
+
assert_eq!(recv.len, 10);
|
|
542
798
|
assert_eq!(recv.off, 5);
|
|
543
799
|
assert_eq!(recv.data.len(), 1);
|
|
544
800
|
|
|
801
|
+
// A fin buffer whose end disagrees with the known final size errors.
|
|
802
|
+
let buf = RangeBuf::from(b"aa", 3, true);
|
|
803
|
+
assert_eq!(recv.write(buf), Err(Error::FinalSize));
|
|
804
|
+
|
|
545
805
|
// Validate final size with fin empty buffers.
|
|
546
|
-
let buf = RangeBuf::from(b"",
|
|
806
|
+
let buf = RangeBuf::from(b"", 11, true);
|
|
547
807
|
assert_eq!(recv.write(buf), Err(Error::FinalSize));
|
|
548
|
-
let buf = RangeBuf::from(b"",
|
|
808
|
+
let buf = RangeBuf::from(b"", 9, true);
|
|
549
809
|
assert_eq!(recv.write(buf), Err(Error::FinalSize));
|
|
550
810
|
|
|
551
|
-
|
|
811
|
+
// The range (5..10) was never received, so the stream cannot reach
|
|
812
|
+
// its fin and nothing further is readable.
|
|
813
|
+
assert_emit_discard_done(&mut recv, emit);
|
|
552
814
|
}
|
|
553
815
|
|
|
554
816
|
#[rstest]
|
|
555
817
|
fn ordered_read(#[values(true, false)] emit: bool) {
|
|
556
|
-
let mut recv =
|
|
557
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
818
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
558
819
|
assert_eq!(recv.len, 0);
|
|
559
820
|
|
|
560
821
|
let first = RangeBuf::from(b"hello", 0, false);
|
|
@@ -577,25 +838,36 @@ mod tests {
|
|
|
577
838
|
assert_eq!(recv.len, 19);
|
|
578
839
|
assert_eq!(recv.off, 0);
|
|
579
840
|
|
|
580
|
-
assert_emit_discard(
|
|
581
|
-
&mut recv,
|
|
582
|
-
emit,
|
|
583
|
-
32,
|
|
584
|
-
19,
|
|
585
|
-
true,
|
|
586
|
-
Some(b"helloworldsomething"),
|
|
587
|
-
);
|
|
841
|
+
assert_emit_discard(&mut recv, emit, 32, 19, true, Some(b"helloworldsomething"));
|
|
588
842
|
assert_eq!(recv.len, 19);
|
|
589
843
|
assert_eq!(recv.off, 19);
|
|
590
844
|
|
|
591
845
|
assert_emit_discard_done(&mut recv, emit);
|
|
592
846
|
}
|
|
593
847
|
|
|
848
|
+
#[test]
|
|
849
|
+
/// `readable_len` counts only contiguous in-order data, up to its limit.
|
|
850
|
+
fn readable_len() {
|
|
851
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
852
|
+
|
|
853
|
+
// Empty buffer: nothing readable.
|
|
854
|
+
assert_eq!(recv.readable_len(64 * 1024), 0);
|
|
855
|
+
|
|
856
|
+
// Data buffered behind a gap is not readable.
|
|
857
|
+
assert!(recv.write(RangeBuf::from(b"hello", 0, false)).is_ok());
|
|
858
|
+
assert!(recv.write(RangeBuf::from(b"something", 10, false)).is_ok());
|
|
859
|
+
assert_eq!(recv.readable_len(64 * 1024), 5);
|
|
860
|
+
|
|
861
|
+
// Filling the gap makes the full range readable, bounded by the limit.
|
|
862
|
+
assert!(recv.write(RangeBuf::from(b"world", 5, false)).is_ok());
|
|
863
|
+
assert_eq!(recv.readable_len(64 * 1024), 19);
|
|
864
|
+
assert_eq!(recv.readable_len(10), 10);
|
|
865
|
+
}
|
|
866
|
+
|
|
594
867
|
/// Test shutdown behavior
|
|
595
868
|
#[rstest]
|
|
596
869
|
fn shutdown(#[values(true, false)] emit: bool) {
|
|
597
|
-
let mut recv =
|
|
598
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
870
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
599
871
|
assert_eq!(recv.len, 0);
|
|
600
872
|
|
|
601
873
|
let first = RangeBuf::from(b"hello", 0, false);
|
|
@@ -645,10 +917,104 @@ mod tests {
|
|
|
645
917
|
assert_emit_discard_done(&mut recv, emit);
|
|
646
918
|
}
|
|
647
919
|
|
|
920
|
+
/// An empty non-fin buffer advances the largest received offset, which the
|
|
921
|
+
/// connection charges to flow control on arrival, so a draining stream must
|
|
922
|
+
/// consume it too and a later reset must not credit it again.
|
|
923
|
+
#[test]
|
|
924
|
+
fn shutdown_empty_stream_frame() {
|
|
925
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
926
|
+
|
|
927
|
+
assert!(recv.write(RangeBuf::from(b"hello", 0, false)).is_ok());
|
|
928
|
+
assert_eq!(recv.shutdown(), Ok(5));
|
|
929
|
+
|
|
930
|
+
assert!(recv.write(RangeBuf::from(b"", 10, false)).is_ok());
|
|
931
|
+
assert_eq!(recv.len, 10);
|
|
932
|
+
assert_eq!(recv.off, 10);
|
|
933
|
+
assert_eq!(recv.data.len(), 0);
|
|
934
|
+
|
|
935
|
+
assert_eq!(
|
|
936
|
+
recv.reset(42, 10),
|
|
937
|
+
Ok(RecvBufResetReturn {
|
|
938
|
+
max_data_delta: 0,
|
|
939
|
+
consumed_flowcontrol: 0,
|
|
940
|
+
})
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
#[rstest]
|
|
945
|
+
fn reset_after_fin(
|
|
946
|
+
#[values("unread", "partial", "gap", "empty", "consumed", "draining")] state: &str,
|
|
947
|
+
) {
|
|
948
|
+
let mut recv = RecvBuf::new(15, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
949
|
+
let mut out = [0; 5];
|
|
950
|
+
let (data, offset) = if state == "gap" {
|
|
951
|
+
(b"llo".as_slice(), 2)
|
|
952
|
+
} else {
|
|
953
|
+
(b"hello".as_slice(), 0)
|
|
954
|
+
};
|
|
955
|
+
recv.write(RangeBuf::from(data, offset, state != "empty"))
|
|
956
|
+
.unwrap();
|
|
957
|
+
|
|
958
|
+
match state {
|
|
959
|
+
"partial" => assert_eq!(recv.emit(&mut out[..2]), Ok((2, false))),
|
|
960
|
+
"consumed" => assert_eq!(recv.emit(&mut out), Ok((5, true))),
|
|
961
|
+
"empty" => {
|
|
962
|
+
assert_eq!(recv.emit(&mut out), Ok((5, false)));
|
|
963
|
+
recv.write(RangeBuf::from(b"", 5, true)).unwrap();
|
|
964
|
+
}
|
|
965
|
+
"draining" => assert_eq!(recv.shutdown(), Ok(5)),
|
|
966
|
+
_ => (),
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
let consumed = 5 - recv.off_front();
|
|
970
|
+
assert_eq!(
|
|
971
|
+
recv.reset(42, 5),
|
|
972
|
+
Ok(RecvBufResetReturn {
|
|
973
|
+
max_data_delta: 0,
|
|
974
|
+
consumed_flowcontrol: consumed,
|
|
975
|
+
})
|
|
976
|
+
);
|
|
977
|
+
assert!(recv.is_fin());
|
|
978
|
+
assert_eq!(recv.max_off(), 5);
|
|
979
|
+
assert_eq!(recv.ready(), state != "consumed" && state != "draining");
|
|
980
|
+
if recv.ready() {
|
|
981
|
+
assert_eq!(recv.emit(&mut out), Err(Error::StreamReset(42)));
|
|
982
|
+
}
|
|
983
|
+
assert_eq!(recv.emit(&mut out), Err(Error::Done));
|
|
984
|
+
assert_eq!(recv.reset(42, 5), Ok(RecvBufResetReturn::zero()));
|
|
985
|
+
recv.write(RangeBuf::from(b"hello", 0, true)).unwrap();
|
|
986
|
+
assert!(!recv.ready());
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
#[rstest]
|
|
990
|
+
fn duplicate_data_first_fin(#[values(true, false)] drain: bool) {
|
|
991
|
+
let mut recv = RecvBuf::new(15, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
992
|
+
let mut out = [0; 5];
|
|
993
|
+
recv.write(RangeBuf::from(b"hello", 0, false)).unwrap();
|
|
994
|
+
assert_eq!(recv.emit(&mut out), Ok((5, false)));
|
|
995
|
+
if drain {
|
|
996
|
+
assert_eq!(recv.shutdown(), Ok(0));
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
recv.write(RangeBuf::from(b"hello", 0, false)).unwrap();
|
|
1000
|
+
assert!(!recv.ready());
|
|
1001
|
+
recv.write(RangeBuf::from(b"hello", 0, true)).unwrap();
|
|
1002
|
+
assert!(recv.is_fin());
|
|
1003
|
+
assert_eq!(recv.ready(), !drain);
|
|
1004
|
+
assert!(recv.data.values().all(|buf| buf.data.as_ref().is_empty()));
|
|
1005
|
+
if !drain {
|
|
1006
|
+
assert_eq!(recv.emit(&mut out), Ok((0, true)));
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
recv.write(RangeBuf::from(b"hello", 0, true)).unwrap();
|
|
1010
|
+
recv.write(RangeBuf::from(b"", 5, true)).unwrap();
|
|
1011
|
+
assert!(!recv.ready());
|
|
1012
|
+
assert_eq!(recv.emit(&mut out), Err(Error::Done));
|
|
1013
|
+
}
|
|
1014
|
+
|
|
648
1015
|
#[rstest]
|
|
649
1016
|
fn split_read(#[values(true, false)] emit: bool) {
|
|
650
|
-
let mut recv =
|
|
651
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1017
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
652
1018
|
assert_eq!(recv.len, 0);
|
|
653
1019
|
|
|
654
1020
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -677,8 +1043,7 @@ mod tests {
|
|
|
677
1043
|
|
|
678
1044
|
#[test]
|
|
679
1045
|
fn split_read_incremental_buf() {
|
|
680
|
-
let mut recv =
|
|
681
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1046
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
682
1047
|
assert_eq!(recv.len, 0);
|
|
683
1048
|
|
|
684
1049
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -725,8 +1090,7 @@ mod tests {
|
|
|
725
1090
|
|
|
726
1091
|
#[rstest]
|
|
727
1092
|
fn incomplete_read(#[values(true, false)] emit: bool) {
|
|
728
|
-
let mut recv =
|
|
729
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1093
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
730
1094
|
assert_eq!(recv.len, 0);
|
|
731
1095
|
|
|
732
1096
|
let mut buf = [0u8; 32];
|
|
@@ -751,22 +1115,14 @@ mod tests {
|
|
|
751
1115
|
assert_eq!(recv.len, 19);
|
|
752
1116
|
assert_eq!(recv.off, 0);
|
|
753
1117
|
|
|
754
|
-
assert_emit_discard(
|
|
755
|
-
&mut recv,
|
|
756
|
-
emit,
|
|
757
|
-
32,
|
|
758
|
-
19,
|
|
759
|
-
true,
|
|
760
|
-
Some(b"somethinghelloworld"),
|
|
761
|
-
);
|
|
1118
|
+
assert_emit_discard(&mut recv, emit, 32, 19, true, Some(b"somethinghelloworld"));
|
|
762
1119
|
assert_eq!(recv.len, 19);
|
|
763
1120
|
assert_eq!(recv.off, 19);
|
|
764
1121
|
}
|
|
765
1122
|
|
|
766
1123
|
#[rstest]
|
|
767
1124
|
fn zero_len_read(#[values(true, false)] emit: bool) {
|
|
768
|
-
let mut recv =
|
|
769
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1125
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
770
1126
|
assert_eq!(recv.len, 0);
|
|
771
1127
|
|
|
772
1128
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -789,8 +1145,7 @@ mod tests {
|
|
|
789
1145
|
|
|
790
1146
|
#[rstest]
|
|
791
1147
|
fn past_read(#[values(true, false)] emit: bool) {
|
|
792
|
-
let mut recv =
|
|
793
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1148
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
794
1149
|
assert_eq!(recv.len, 0);
|
|
795
1150
|
|
|
796
1151
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -817,15 +1172,15 @@ mod tests {
|
|
|
817
1172
|
assert!(recv.write(fourth).is_ok());
|
|
818
1173
|
assert_eq!(recv.len, 9);
|
|
819
1174
|
assert_eq!(recv.off, 9);
|
|
820
|
-
assert_eq!(recv.data.len(),
|
|
1175
|
+
assert_eq!(recv.data.len(), 1);
|
|
821
1176
|
|
|
1177
|
+
assert_emit_discard(&mut recv, emit, 32, 0, true, Some(b""));
|
|
822
1178
|
assert_emit_discard_done(&mut recv, emit);
|
|
823
1179
|
}
|
|
824
1180
|
|
|
825
1181
|
#[rstest]
|
|
826
1182
|
fn fully_overlapping_read(#[values(true, false)] emit: bool) {
|
|
827
|
-
let mut recv =
|
|
828
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1183
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
829
1184
|
assert_eq!(recv.len, 0);
|
|
830
1185
|
|
|
831
1186
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -851,8 +1206,7 @@ mod tests {
|
|
|
851
1206
|
|
|
852
1207
|
#[rstest]
|
|
853
1208
|
fn fully_overlapping_read2(#[values(true, false)] emit: bool) {
|
|
854
|
-
let mut recv =
|
|
855
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1209
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
856
1210
|
assert_eq!(recv.len, 0);
|
|
857
1211
|
|
|
858
1212
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -878,8 +1232,7 @@ mod tests {
|
|
|
878
1232
|
|
|
879
1233
|
#[rstest]
|
|
880
1234
|
fn fully_overlapping_read3(#[values(true, false)] emit: bool) {
|
|
881
|
-
let mut recv =
|
|
882
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1235
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
883
1236
|
assert_eq!(recv.len, 0);
|
|
884
1237
|
|
|
885
1238
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -905,8 +1258,7 @@ mod tests {
|
|
|
905
1258
|
|
|
906
1259
|
#[rstest]
|
|
907
1260
|
fn fully_overlapping_read_multi(#[values(true, false)] emit: bool) {
|
|
908
|
-
let mut recv =
|
|
909
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1261
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
910
1262
|
assert_eq!(recv.len, 0);
|
|
911
1263
|
|
|
912
1264
|
let first = RangeBuf::from(b"somethingsomething", 0, false);
|
|
@@ -928,14 +1280,7 @@ mod tests {
|
|
|
928
1280
|
assert_eq!(recv.off, 0);
|
|
929
1281
|
assert_eq!(recv.data.len(), 5);
|
|
930
1282
|
|
|
931
|
-
assert_emit_discard(
|
|
932
|
-
&mut recv,
|
|
933
|
-
emit,
|
|
934
|
-
32,
|
|
935
|
-
18,
|
|
936
|
-
false,
|
|
937
|
-
Some(b"somhellogsomhellog"),
|
|
938
|
-
);
|
|
1283
|
+
assert_emit_discard(&mut recv, emit, 32, 18, false, Some(b"somhellogsomhellog"));
|
|
939
1284
|
assert_eq!(recv.len, 18);
|
|
940
1285
|
assert_eq!(recv.off, 18);
|
|
941
1286
|
assert_eq!(recv.data.len(), 0);
|
|
@@ -945,8 +1290,7 @@ mod tests {
|
|
|
945
1290
|
|
|
946
1291
|
#[rstest]
|
|
947
1292
|
fn overlapping_start_read(#[values(true, false)] emit: bool) {
|
|
948
|
-
let mut recv =
|
|
949
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1293
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
950
1294
|
assert_eq!(recv.len, 0);
|
|
951
1295
|
|
|
952
1296
|
let first = RangeBuf::from(b"something", 0, false);
|
|
@@ -962,14 +1306,7 @@ mod tests {
|
|
|
962
1306
|
assert_eq!(recv.off, 0);
|
|
963
1307
|
assert_eq!(recv.data.len(), 2);
|
|
964
1308
|
|
|
965
|
-
assert_emit_discard(
|
|
966
|
-
&mut recv,
|
|
967
|
-
emit,
|
|
968
|
-
32,
|
|
969
|
-
13,
|
|
970
|
-
true,
|
|
971
|
-
Some(b"somethingello"),
|
|
972
|
-
);
|
|
1309
|
+
assert_emit_discard(&mut recv, emit, 32, 13, true, Some(b"somethingello"));
|
|
973
1310
|
|
|
974
1311
|
assert_eq!(recv.len, 13);
|
|
975
1312
|
assert_eq!(recv.off, 13);
|
|
@@ -979,8 +1316,7 @@ mod tests {
|
|
|
979
1316
|
|
|
980
1317
|
#[rstest]
|
|
981
1318
|
fn overlapping_end_read(#[values(true, false)] emit: bool) {
|
|
982
|
-
let mut recv =
|
|
983
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1319
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
984
1320
|
assert_eq!(recv.len, 0);
|
|
985
1321
|
|
|
986
1322
|
let first = RangeBuf::from(b"hello", 0, false);
|
|
@@ -1005,8 +1341,7 @@ mod tests {
|
|
|
1005
1341
|
|
|
1006
1342
|
#[rstest]
|
|
1007
1343
|
fn overlapping_end_twice_read(#[values(true, false)] emit: bool) {
|
|
1008
|
-
let mut recv =
|
|
1009
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1344
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1010
1345
|
assert_eq!(recv.len, 0);
|
|
1011
1346
|
|
|
1012
1347
|
let first = RangeBuf::from(b"he", 0, false);
|
|
@@ -1042,11 +1377,8 @@ mod tests {
|
|
|
1042
1377
|
}
|
|
1043
1378
|
|
|
1044
1379
|
#[rstest]
|
|
1045
|
-
fn overlapping_end_twice_and_contained_read(
|
|
1046
|
-
|
|
1047
|
-
) {
|
|
1048
|
-
let mut recv =
|
|
1049
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1380
|
+
fn overlapping_end_twice_and_contained_read(#[values(true, false)] emit: bool) {
|
|
1381
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1050
1382
|
assert_eq!(recv.len, 0);
|
|
1051
1383
|
|
|
1052
1384
|
let first = RangeBuf::from(b"hellow", 0, false);
|
|
@@ -1074,14 +1406,7 @@ mod tests {
|
|
|
1074
1406
|
assert_eq!(recv.off, 0);
|
|
1075
1407
|
assert_eq!(recv.data.len(), 5);
|
|
1076
1408
|
|
|
1077
|
-
assert_emit_discard(
|
|
1078
|
-
&mut recv,
|
|
1079
|
-
emit,
|
|
1080
|
-
32,
|
|
1081
|
-
16,
|
|
1082
|
-
true,
|
|
1083
|
-
Some(b"helloworldbarfoo"),
|
|
1084
|
-
);
|
|
1409
|
+
assert_emit_discard(&mut recv, emit, 32, 16, true, Some(b"helloworldbarfoo"));
|
|
1085
1410
|
assert_eq!(recv.len, 16);
|
|
1086
1411
|
assert_eq!(recv.off, 16);
|
|
1087
1412
|
|
|
@@ -1089,11 +1414,8 @@ mod tests {
|
|
|
1089
1414
|
}
|
|
1090
1415
|
|
|
1091
1416
|
#[rstest]
|
|
1092
|
-
fn partially_multi_overlapping_reordered_read(
|
|
1093
|
-
|
|
1094
|
-
) {
|
|
1095
|
-
let mut recv =
|
|
1096
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1417
|
+
fn partially_multi_overlapping_reordered_read(#[values(true, false)] emit: bool) {
|
|
1418
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1097
1419
|
assert_eq!(recv.len, 0);
|
|
1098
1420
|
|
|
1099
1421
|
let first = RangeBuf::from(b"hello", 8, false);
|
|
@@ -1115,14 +1437,7 @@ mod tests {
|
|
|
1115
1437
|
assert_eq!(recv.off, 0);
|
|
1116
1438
|
assert_eq!(recv.data.len(), 3);
|
|
1117
1439
|
|
|
1118
|
-
assert_emit_discard(
|
|
1119
|
-
&mut recv,
|
|
1120
|
-
emit,
|
|
1121
|
-
32,
|
|
1122
|
-
15,
|
|
1123
|
-
true,
|
|
1124
|
-
Some(b"somethinhelloar"),
|
|
1125
|
-
);
|
|
1440
|
+
assert_emit_discard(&mut recv, emit, 32, 15, true, Some(b"somethinhelloar"));
|
|
1126
1441
|
assert_eq!(recv.len, 15);
|
|
1127
1442
|
assert_eq!(recv.off, 15);
|
|
1128
1443
|
assert_eq!(recv.data.len(), 0);
|
|
@@ -1131,11 +1446,8 @@ mod tests {
|
|
|
1131
1446
|
}
|
|
1132
1447
|
|
|
1133
1448
|
#[rstest]
|
|
1134
|
-
fn partially_multi_overlapping_reordered_read2(
|
|
1135
|
-
|
|
1136
|
-
) {
|
|
1137
|
-
let mut recv =
|
|
1138
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1449
|
+
fn partially_multi_overlapping_reordered_read2(#[values(true, false)] emit: bool) {
|
|
1450
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1139
1451
|
assert_eq!(recv.len, 0);
|
|
1140
1452
|
|
|
1141
1453
|
let first = RangeBuf::from(b"aaa", 0, false);
|
|
@@ -1175,14 +1487,7 @@ mod tests {
|
|
|
1175
1487
|
assert_eq!(recv.off, 0);
|
|
1176
1488
|
assert_eq!(recv.data.len(), 6);
|
|
1177
1489
|
|
|
1178
|
-
assert_emit_discard(
|
|
1179
|
-
&mut recv,
|
|
1180
|
-
emit,
|
|
1181
|
-
32,
|
|
1182
|
-
14,
|
|
1183
|
-
false,
|
|
1184
|
-
Some(b"aabbbcdddeefff"),
|
|
1185
|
-
);
|
|
1490
|
+
assert_emit_discard(&mut recv, emit, 32, 14, false, Some(b"aabbbcdddeefff"));
|
|
1186
1491
|
assert_eq!(recv.len, 14);
|
|
1187
1492
|
assert_eq!(recv.off, 14);
|
|
1188
1493
|
assert_eq!(recv.data.len(), 0);
|
|
@@ -1192,8 +1497,7 @@ mod tests {
|
|
|
1192
1497
|
|
|
1193
1498
|
#[test]
|
|
1194
1499
|
fn mixed_read_actions() {
|
|
1195
|
-
let mut recv =
|
|
1196
|
-
RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1500
|
+
let mut recv = RecvBuf::new(u64::MAX, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1197
1501
|
assert_eq!(recv.len, 0);
|
|
1198
1502
|
|
|
1199
1503
|
let first = RangeBuf::from(b"hello", 0, false);
|