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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use std::collections::hash_map::Entry;
use std::mem;
use thiserror::Error;
use tracing::debug;
use super::{Retransmits, ShouldTransmit, StreamHalf, StreamId, StreamsState, UnknownStream};
use crate::connection::assembler::{Assembler, Chunk, IllegalOrderedRead};
use crate::{frame, Dir, TransportError, VarInt};
#[derive(Debug, Default)]
pub(super) struct Recv {
state: RecvState,
pub(super) assembler: Assembler,
sent_max_stream_data: u64,
pub(super) end: u64,
pub(super) stopped: bool,
}
impl Recv {
pub(super) fn new(initial_max_data: u64) -> Self {
Self {
state: RecvState::default(),
assembler: Assembler::new(),
sent_max_stream_data: initial_max_data,
end: 0,
stopped: false,
}
}
pub(super) fn ingest(
&mut self,
frame: frame::Stream,
payload_len: usize,
received: u64,
max_data: u64,
) -> Result<(u64, bool), TransportError> {
let end = frame.offset + frame.data.len() as u64;
if end >= 2u64.pow(62) {
return Err(TransportError::FLOW_CONTROL_ERROR(
"maximum stream offset too large",
));
}
if let Some(final_offset) = self.final_offset() {
if end > final_offset || (frame.fin && end != final_offset) {
debug!(end, final_offset, "final size error");
return Err(TransportError::FINAL_SIZE_ERROR(""));
}
}
let new_bytes = self.credit_consumed_by(end, received, max_data)?;
if frame.fin && !self.stopped {
if let RecvState::Recv { ref mut size } = self.state {
*size = Some(end);
}
}
self.end = self.end.max(end);
if !self.stopped {
self.assembler.insert(frame.offset, frame.data, payload_len);
} else {
self.assembler.set_bytes_read(end);
}
Ok((new_bytes, frame.fin && self.stopped))
}
pub(super) fn stop(&mut self) -> Result<(u64, ShouldTransmit), UnknownStream> {
if self.stopped {
return Err(UnknownStream { _private: () });
}
self.stopped = true;
self.assembler.clear();
let read_credits = self.end - self.assembler.bytes_read();
Ok((read_credits, ShouldTransmit(self.is_receiving())))
}
pub(super) fn max_stream_data(&mut self, stream_receive_window: u64) -> (u64, ShouldTransmit) {
let max_stream_data = self.assembler.bytes_read() + stream_receive_window;
let diff = max_stream_data - self.sent_max_stream_data;
let transmit = self.receiving_unknown_size() && diff >= (stream_receive_window / 8);
(max_stream_data, ShouldTransmit(transmit))
}
pub fn record_sent_max_stream_data(&mut self, sent_value: u64) {
if sent_value > self.sent_max_stream_data {
self.sent_max_stream_data = sent_value;
}
}
pub(super) fn receiving_unknown_size(&self) -> bool {
matches!(self.state, RecvState::Recv { size: None })
}
pub(super) fn is_receiving(&self) -> bool {
matches!(self.state, RecvState::Recv { .. })
}
fn final_offset(&self) -> Option<u64> {
match self.state {
RecvState::Recv { size } => size,
RecvState::ResetRecvd { size, .. } => Some(size),
}
}
pub(super) fn reset(
&mut self,
error_code: VarInt,
final_offset: VarInt,
received: u64,
max_data: u64,
) -> Result<bool, TransportError> {
if let Some(offset) = self.final_offset() {
if offset != final_offset.into() {
return Err(TransportError::FINAL_SIZE_ERROR("inconsistent value"));
}
} else if self.end > final_offset.into() {
return Err(TransportError::FINAL_SIZE_ERROR(
"lower than high water mark",
));
}
self.credit_consumed_by(final_offset.into(), received, max_data)?;
if matches!(self.state, RecvState::ResetRecvd { .. }) {
return Ok(false);
}
self.state = RecvState::ResetRecvd {
size: final_offset.into(),
error_code,
};
self.assembler.clear();
Ok(true)
}
fn credit_consumed_by(
&self,
offset: u64,
received: u64,
max_data: u64,
) -> Result<u64, TransportError> {
let prev_end = self.end;
let new_bytes = offset.saturating_sub(prev_end);
if offset > self.sent_max_stream_data || received + new_bytes > max_data {
debug!(
received,
new_bytes,
max_data,
offset,
stream_max_data = self.sent_max_stream_data,
"flow control error"
);
return Err(TransportError::FLOW_CONTROL_ERROR(""));
}
Ok(new_bytes)
}
}
pub struct Chunks<'a> {
id: StreamId,
ordered: bool,
streams: &'a mut StreamsState,
pending: &'a mut Retransmits,
state: ChunksState,
read: u64,
}
impl<'a> Chunks<'a> {
pub(super) fn new(
id: StreamId,
ordered: bool,
streams: &'a mut StreamsState,
pending: &'a mut Retransmits,
) -> Result<Self, ReadableError> {
let entry = match streams.recv.entry(id) {
Entry::Occupied(entry) => entry,
Entry::Vacant(_) => return Err(ReadableError::UnknownStream),
};
let mut recv = match entry.get().stopped {
true => return Err(ReadableError::UnknownStream),
false => entry.remove(),
};
recv.assembler.ensure_ordering(ordered)?;
Ok(Self {
id,
ordered,
streams,
pending,
state: ChunksState::Readable(recv),
read: 0,
})
}
pub fn next(&mut self, max_length: usize) -> Result<Option<Chunk>, ReadError> {
let rs = match self.state {
ChunksState::Readable(ref mut rs) => rs,
ChunksState::Reset(error_code) => {
return Err(ReadError::Reset(error_code));
}
ChunksState::Finished => {
return Ok(None);
}
ChunksState::Finalized => panic!("must not call next() after finalize()"),
};
if let Some(chunk) = rs.assembler.read(max_length, self.ordered) {
self.read += chunk.bytes.len() as u64;
return Ok(Some(chunk));
}
match rs.state {
RecvState::ResetRecvd { error_code, .. } => {
debug_assert_eq!(self.read, 0, "reset streams have empty buffers");
self.streams.stream_freed(self.id, StreamHalf::Recv);
self.state = ChunksState::Reset(error_code);
Err(ReadError::Reset(error_code))
}
RecvState::Recv { size } => {
if size == Some(rs.end) && rs.assembler.bytes_read() == rs.end {
self.streams.stream_freed(self.id, StreamHalf::Recv);
self.state = ChunksState::Finished;
Ok(None)
} else {
Err(ReadError::Blocked)
}
}
}
}
pub fn finalize(mut self) -> ShouldTransmit {
self.finalize_inner(false)
}
fn finalize_inner(&mut self, drop: bool) -> ShouldTransmit {
let state = mem::replace(&mut self.state, ChunksState::Finalized);
debug_assert!(
!drop || matches!(state, ChunksState::Finalized),
"finalize must be called before drop"
);
if let ChunksState::Finalized = state {
return ShouldTransmit(false);
}
let mut should_transmit = false;
if matches!(state, ChunksState::Finished | ChunksState::Reset(_))
&& self.streams.side != self.id.initiator()
{
match self.id.dir() {
Dir::Uni => self.pending.max_uni_stream_id = true,
Dir::Bi => self.pending.max_bi_stream_id = true,
}
should_transmit = true;
}
if let ChunksState::Readable(mut rs) = state {
let (_, max_stream_data) = rs.max_stream_data(self.streams.stream_receive_window);
should_transmit |= max_stream_data.0;
if max_stream_data.0 {
self.pending.max_stream_data.insert(self.id);
}
self.streams.recv.insert(self.id, rs);
}
let max_data = self.streams.add_read_credits(self.read);
self.pending.max_data |= max_data.0;
should_transmit |= max_data.0;
ShouldTransmit(should_transmit)
}
}
impl<'a> Drop for Chunks<'a> {
fn drop(&mut self) {
let _ = self.finalize_inner(true);
}
}
enum ChunksState {
Readable(Recv),
Reset(VarInt),
Finished,
Finalized,
}
#[derive(Debug, Error, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum ReadError {
#[error("blocked")]
Blocked,
#[error("reset by peer: code {0}")]
Reset(VarInt),
}
#[derive(Debug, Error, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum ReadableError {
#[error("unknown stream")]
UnknownStream,
#[error("ordered read after unordered read")]
IllegalOrderedRead,
}
impl From<IllegalOrderedRead> for ReadableError {
fn from(_: IllegalOrderedRead) -> Self {
ReadableError::IllegalOrderedRead
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum RecvState {
Recv { size: Option<u64> },
ResetRecvd { size: u64, error_code: VarInt },
}
impl Default for RecvState {
fn default() -> Self {
RecvState::Recv { size: None }
}
}