Debian Codegraph @

third_party/rust/neqo-http3/src/control_stream_local.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::{
8 collections::{HashMap, VecDeque},
9 fmt::{self, Display, Formatter},
10 };
11
12 use neqo_common::{qtrace, Encoder};
13 use neqo_transport::{Connection, StreamId, StreamType};
14
15 use crate::{frames::HFrame, BufferedStream, Error, Http3StreamType, RecvStream, Res};
16
17 pub const HTTP3_UNI_STREAM_TYPE_CONTROL: u64 = 0x0;
18
19 /// The local control stream, responsible for encoding frames and sending them
20 #[derive(Debug)]
21 pub struct ControlStreamLocal {
22 stream: BufferedStream,
23 /// `stream_id`s of outstanding request streams
24 outstanding_priority_update: VecDeque<StreamId>,
25 }
26
27 impl Display for ControlStreamLocal {
28 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
29 write!(f, "Local control stream {:?}", self.stream)
30 }
31 }
32
33 impl ControlStreamLocal {
34 pub fn new() -> Self {
35 Self {
36 stream: BufferedStream::default(),
37 outstanding_priority_update: VecDeque::new(),
38 }
39 }
40
41 /// Add a new frame that needs to be send.
42 pub fn queue_frame(&mut self, f: &HFrame) {
43 let mut enc = Encoder::default();
44 f.encode(&mut enc);
45 self.stream.buffer(enc.as_ref());
46 }
47
48 pub fn queue_update_priority(&mut self, stream_id: StreamId) {
49 self.outstanding_priority_update.push_back(stream_id);
50 }
51
52 /// Send control data if available.
53 pub fn send(
54 &mut self,
55 conn: &mut Connection,
56 recv_conn: &mut HashMap<StreamId, Box<dyn RecvStream>>,
57 ) -> Res<()> {
58 self.stream.send_buffer(conn)?;
59 self.send_priority_update(conn, recv_conn)
60 }
61
62 fn send_priority_update(
63 &mut self,
64 conn: &mut Connection,
65 recv_conn: &mut HashMap<StreamId, Box<dyn RecvStream>>,
66 ) -> Res<()> {
67 // send all necessary priority updates
68 while let Some(update_id) = self.outstanding_priority_update.pop_front() {
69 let Some(update_stream) = recv_conn.get_mut(&update_id) else {
70 continue;
71 };
72
73 // can assert and unwrap here, because priority updates can only be added to
74 // HttpStreams in [Http3Connection::queue_update_priority}
75 debug_assert!(matches!(
76 update_stream.stream_type(),
77 Http3StreamType::Http | Http3StreamType::Push
78 ));
79 let stream = update_stream.http_stream().ok_or(Error::Internal)?;
80
81 // in case multiple priority_updates were issued, ignore now irrelevant
82 if let Some(hframe) = stream.priority_update_frame() {
83 let mut enc = Encoder::new();
84 hframe.encode(&mut enc);
85 if self.stream.send_atomic(conn, enc.as_ref())? {
86 stream.priority_update_sent()?;
87 } else {
88 self.outstanding_priority_update.push_front(update_id);
89 break;
90 }
91 }
92 }
93 Ok(())
94 }
95
96 /// Create a control stream.
97 pub fn create(&mut self, conn: &mut Connection) -> Res<()> {
98 qtrace!("[{self}] Create a control stream");
99 self.stream.init(conn.stream_create(StreamType::UniDi)?);
100 self.stream
101 .buffer(&[u8::try_from(HTTP3_UNI_STREAM_TYPE_CONTROL).map_err(|_| Error::Internal)?]);
102 Ok(())
103 }
104
105 #[must_use]
106 pub fn stream_id(&self) -> Option<StreamId> {
107 (&self.stream).into()
108 }
109 }