1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! http2 connection flow control.
15 
16 use ylong_http::h2::{Frame, H2Error};
17 
18 use crate::util::h2::buffer::window::RecvWindow;
19 use crate::util::h2::buffer::SendWindow;
20 
21 pub(crate) struct FlowControl {
22     recv_window: RecvWindow,
23     send_window: SendWindow,
24 }
25 
26 impl FlowControl {
new(conn_recv_window: u32, conn_send_window: u32) -> Self27     pub(crate) fn new(conn_recv_window: u32, conn_send_window: u32) -> Self {
28         FlowControl {
29             recv_window: RecvWindow::new(conn_recv_window as i32),
30             send_window: SendWindow::new(conn_send_window as i32),
31         }
32     }
33 
check_conn_recv_window_update(&mut self) -> Option<Frame>34     pub(crate) fn check_conn_recv_window_update(&mut self) -> Option<Frame> {
35         self.recv_window.check_window_update(0)
36     }
37 
setup_recv_window(&mut self, size: u32)38     pub(crate) fn setup_recv_window(&mut self, size: u32) {
39         let setup = size;
40         let actual = self.recv_window.actual_size() as u32;
41         if setup > actual {
42             let extra = setup - actual;
43             self.recv_window.increase_actual(extra);
44         } else {
45             let extra = actual - setup;
46             self.recv_window.reduce_actual(extra);
47         }
48     }
49 
increase_send_size(&mut self, size: u32) -> Result<(), H2Error>50     pub(crate) fn increase_send_size(&mut self, size: u32) -> Result<(), H2Error> {
51         self.send_window.increase_size(size)
52     }
53 
send_size_available(&self) -> usize54     pub(crate) fn send_size_available(&self) -> usize {
55         self.send_window.size_available() as usize
56     }
57 
recv_notification_size_available(&self) -> u3258     pub(crate) fn recv_notification_size_available(&self) -> u32 {
59         self.recv_window.notification_available()
60     }
61 
send_data(&mut self, size: u32)62     pub(crate) fn send_data(&mut self, size: u32) {
63         self.send_window.send_data(size)
64     }
65 
recv_data(&mut self, size: u32)66     pub(crate) fn recv_data(&mut self, size: u32) {
67         self.recv_window.recv_data(size)
68     }
69 }
70