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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use s2n_quic_core::varint::VarInt;
pub mod blocking;
pub mod non_blocking;
pub use crate::msg::segment::MAX_TOTAL;
/// Flow credits acquired by an application request
#[derive(Debug)]
pub struct Credits {
/// The offset at which to write the stream bytes
pub offset: VarInt,
/// The number of bytes which an application must write after acquisition
pub len: usize,
/// The total number of bytes the application buffer is willing to transmit
pub initial_len: usize,
/// Indicates if the stream is being finalized
pub is_fin: bool,
}
/// An application request for flow credits
#[derive(Clone, Copy, Debug)]
pub struct Request {
/// The number of bytes in the application buffer
pub len: usize,
/// The total number of bytes the application buffer is willing to transmit
pub initial_len: usize,
/// Indicates if the request is finalizing a stream
pub is_fin: bool,
}
impl Request {
/// Clamps the request with the given number of credits
#[inline]
pub fn clamp(&mut self, credits: u64) {
let len = self.len.min(credits.min(MAX_TOTAL as _) as usize);
// if we didn't acquire the entire len, then clear the `is_fin` flag
if self.len != len {
self.is_fin = false;
}
// update the len based on the provided credits
self.len = len;
}
/// Constructs a response with the acquired offset
#[inline]
pub fn response(self, offset: VarInt) -> Credits {
Credits {
offset,
len: self.len,
initial_len: self.initial_len,
is_fin: self.is_fin,
}
}
}