Debian Codegraph @

third_party/rust/neqo-http3/src/features/extended_connect/webtransport_streams.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::{cell::RefCell, rc::Rc};
8
9 use neqo_common::Encoder;
10 use neqo_transport::{Connection, RecvStreamStats, SendStreamStats, StreamId};
11
12 use super::WebTransportSession;
13 use crate::{
14 CloseType, Http3StreamInfo, Http3StreamType, ReceiveOutput, RecvStream, RecvStreamEvents, Res,
15 SendStream, SendStreamEvents, Stream,
16 };
17
18 pub const WEBTRANSPORT_UNI_STREAM: u64 = 0x54;
19 pub const WEBTRANSPORT_STREAM: u64 = 0x41;
20
21 #[derive(Debug)]
22 pub struct WebTransportRecvStream {
23 stream_id: StreamId,
24 events: Box<dyn RecvStreamEvents>,
25 session: Rc<RefCell<WebTransportSession>>,
26 session_id: StreamId,
27 fin: bool,
28 }
29
30 impl WebTransportRecvStream {
31 pub fn new(
32 stream_id: StreamId,
33 session_id: StreamId,
34 events: Box<dyn RecvStreamEvents>,
35 session: Rc<RefCell<WebTransportSession>>,
36 ) -> Self {
37 Self {
38 stream_id,
39 events,
40 session_id,
41 session,
42 fin: false,
43 }
44 }
45
46 fn get_info(&self) -> Http3StreamInfo {
47 Http3StreamInfo::new(self.stream_id, self.stream_type())
48 }
49 }
50
51 impl Stream for WebTransportRecvStream {
52 fn stream_type(&self) -> Http3StreamType {
53 Http3StreamType::WebTransport(self.session_id)
54 }
55 }
56
57 impl RecvStream for WebTransportRecvStream {
58 fn receive(&mut self, _conn: &mut Connection) -> Res<(ReceiveOutput, bool)> {
59 self.events.data_readable(self.get_info());
60 Ok((ReceiveOutput::NoOutput, false))
61 }
62
63 fn reset(&mut self, close_type: CloseType) -> Res<()> {
64 if !matches!(close_type, CloseType::ResetApp(_)) {
65 self.events.recv_closed(self.get_info(), close_type);
66 }
67 self.session.borrow_mut().remove_recv_stream(self.stream_id);
68 Ok(())
69 }
70
71 fn read_data(&mut self, conn: &mut Connection, buf: &mut [u8]) -> Res<(usize, bool)> {
72 let (amount, fin) = conn.stream_recv(self.stream_id, buf)?;
73 self.fin = fin;
74 if fin {
75 self.session.borrow_mut().remove_recv_stream(self.stream_id);
76 }
77 Ok((amount, fin))
78 }
79
80 fn stats(&mut self, conn: &mut Connection) -> Res<RecvStreamStats> {
81 const TYPE_LEN_UNI: usize = Encoder::varint_len(WEBTRANSPORT_UNI_STREAM);
82 const TYPE_LEN_BIDI: usize = Encoder::varint_len(WEBTRANSPORT_STREAM);
83
84 let stream_header_size = if self.stream_id.is_server_initiated() {
85 let id_len = if self.stream_id.is_uni() {
86 TYPE_LEN_UNI
87 } else {
88 TYPE_LEN_BIDI
89 };
90 (id_len + Encoder::varint_len(self.session_id.as_u64())) as u64
91 } else {
92 0
93 };
94
95 let stats = conn.recv_stream_stats(self.stream_id)?;
96 if stream_header_size == 0 {
97 return Ok(stats);
98 }
99
100 let subtract_non_app_bytes =
101 |count: u64| -> u64 { count.saturating_sub(stream_header_size) };
102
103 let bytes_received = subtract_non_app_bytes(stats.bytes_received());
104 let bytes_read = subtract_non_app_bytes(stats.bytes_read());
105
106 Ok(RecvStreamStats::new(bytes_received, bytes_read))
107 }
108 }
109
110 #[derive(Debug, PartialEq)]
111 enum WebTransportSenderStreamState {
112 SendingInit { buf: Vec<u8>, fin: bool },
113 SendingData,
114 Done,
115 }
116
117 #[derive(Debug)]
118 pub struct WebTransportSendStream {
119 stream_id: StreamId,
120 state: WebTransportSenderStreamState,
121 events: Box<dyn SendStreamEvents>,
122 session: Rc<RefCell<WebTransportSession>>,
123 session_id: StreamId,
124 }
125
126 impl WebTransportSendStream {
127 pub fn new(
128 stream_id: StreamId,
129 session_id: StreamId,
130 events: Box<dyn SendStreamEvents>,
131 session: Rc<RefCell<WebTransportSession>>,
132 local: bool,
133 ) -> Self {
134 Self {
135 stream_id,
136 state: if local {
137 let mut d = Encoder::default();
138 if stream_id.is_uni() {
139 d.encode_varint(WEBTRANSPORT_UNI_STREAM);
140 } else {
141 d.encode_varint(WEBTRANSPORT_STREAM);
142 }
143 d.encode_varint(session_id.as_u64());
144 WebTransportSenderStreamState::SendingInit {
145 buf: d.into(),
146 fin: false,
147 }
148 } else {
149 WebTransportSenderStreamState::SendingData
150 },
151 events,
152 session_id,
153 session,
154 }
155 }
156
157 fn set_done(&mut self, close_type: CloseType) {
158 self.state = WebTransportSenderStreamState::Done;
159 self.events.send_closed(self.get_info(), close_type);
160 self.session.borrow_mut().remove_send_stream(self.stream_id);
161 }
162
163 fn get_info(&self) -> Http3StreamInfo {
164 Http3StreamInfo::new(self.stream_id, self.stream_type())
165 }
166 }
167
168 impl Stream for WebTransportSendStream {
169 fn stream_type(&self) -> Http3StreamType {
170 Http3StreamType::WebTransport(self.session_id)
171 }
172 }
173
174 impl SendStream for WebTransportSendStream {
175 fn send(&mut self, conn: &mut Connection) -> Res<()> {
176 if let WebTransportSenderStreamState::SendingInit { ref mut buf, fin } = self.state {
177 let sent = conn.stream_send(self.stream_id, &buf[..])?;
178 if sent == buf.len() {
179 if fin {
180 conn.stream_close_send(self.stream_id)?;
181 self.set_done(CloseType::Done);
182 } else {
183 self.state = WebTransportSenderStreamState::SendingData;
184 }
185 } else {
186 let b = buf.split_off(sent);
187 *buf = b;
188 }
189 }
190 Ok(())
191 }
192
193 fn has_data_to_send(&self) -> bool {
194 matches!(
195 self.state,
196 WebTransportSenderStreamState::SendingInit { .. }
197 )
198 }
199
200 fn stream_writable(&self) {
201 self.events.data_writable(self.get_info());
202 }
203
204 fn done(&self) -> bool {
205 self.state == WebTransportSenderStreamState::Done
206 }
207
208 fn send_data(&mut self, conn: &mut Connection, buf: &[u8]) -> Res<usize> {
209 self.send(conn)?;
210 if self.state == WebTransportSenderStreamState::SendingData {
211 let sent = conn.stream_send(self.stream_id, buf)?;
212 Ok(sent)
213 } else {
214 Ok(0)
215 }
216 }
217
218 fn handle_stop_sending(&mut self, close_type: CloseType) {
219 self.set_done(close_type);
220 }
221
222 fn close(&mut self, conn: &mut Connection) -> Res<()> {
223 if let WebTransportSenderStreamState::SendingInit { ref mut fin, .. } = self.state {
224 *fin = true;
225 } else {
226 self.state = WebTransportSenderStreamState::Done;
227 conn.stream_close_send(self.stream_id)?;
228 self.set_done(CloseType::Done);
229 }
230 Ok(())
231 }
232
233 fn stats(&mut self, conn: &mut Connection) -> Res<SendStreamStats> {
234 const TYPE_LEN_UNI: usize = Encoder::varint_len(WEBTRANSPORT_UNI_STREAM);
235 const TYPE_LEN_BIDI: usize = Encoder::varint_len(WEBTRANSPORT_STREAM);
236
237 let stream_header_size = if self.stream_id.is_client_initiated() {
238 let id_len = if self.stream_id.is_uni() {
239 TYPE_LEN_UNI
240 } else {
241 TYPE_LEN_BIDI
242 };
243 (id_len + Encoder::varint_len(self.session_id.as_u64())) as u64
244 } else {
245 0
246 };
247
248 let stats = conn.send_stream_stats(self.stream_id)?;
249 if stream_header_size == 0 {
250 return Ok(stats);
251 }
252
253 let subtract_non_app_bytes =
254 |count: u64| -> u64 { count.saturating_sub(stream_header_size) };
255
256 let bytes_written = subtract_non_app_bytes(stats.bytes_written());
257 let bytes_sent = subtract_non_app_bytes(stats.bytes_sent());
258 let bytes_acked = subtract_non_app_bytes(stats.bytes_acked());
259 Ok(SendStreamStats::new(bytes_written, bytes_sent, bytes_acked))
260 }
261 }