Debian Codegraph @

third_party/rust/neqo-http3/src/buffered_send_stream.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::fmt::{self, Display, Formatter};
8
9 use neqo_transport::{Connection, StreamId};
10
11 use crate::{qlog, Res};
12
13 #[derive(Debug, PartialEq, Eq, Default)]
14 pub enum BufferedStream {
15 #[default]
16 Uninitialized,
17 Initialized {
18 stream_id: StreamId,
19 buf: Vec<u8>,
20 },
21 }
22
23 impl Display for BufferedStream {
24 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
25 write!(f, "BufferedStream {:?}", Option::<StreamId>::from(self))
26 }
27 }
28
29 impl BufferedStream {
30 #[must_use]
31 pub const fn new(stream_id: StreamId) -> Self {
32 Self::Initialized {
33 stream_id,
34 buf: Vec::new(),
35 }
36 }
37
38 /// # Panics
39 ///
40 /// If the `BufferedStream` is initialized more than once, it will panic.
41 pub fn init(&mut self, stream_id: StreamId) {
42 debug_assert!(&Self::Uninitialized == self);
43 *self = Self::Initialized {
44 stream_id,
45 buf: Vec::new(),
46 };
47 }
48
49 /// # Panics
50 ///
51 /// This function cannot be called before the `BufferedStream` is initialized.
52 pub fn buffer(&mut self, to_buf: &[u8]) {
53 if let Self::Initialized { buf, .. } = self {
54 buf.extend_from_slice(to_buf);
55 } else {
56 debug_assert!(false, "Do not buffer date before the stream is initialized");
57 }
58 }
59
60 /// # Errors
61 ///
62 /// Returns `neqo_transport` errors.
63 pub fn send_buffer(&mut self, conn: &mut Connection) -> Res<usize> {
64 let Self::Initialized { stream_id, buf } = self else {
65 return Ok(0);
66 };
67 if buf.is_empty() {
68 return Ok(0);
69 }
70 let sent = conn.stream_send(*stream_id, &buf[..])?;
71 if sent == 0 {
72 return Ok(0);
73 } else if sent == buf.len() {
74 buf.clear();
75 } else {
76 let b = buf.split_off(sent);
77 *buf = b;
78 }
79 qlog::h3_data_moved_down(conn.qlog_mut(), *stream_id, sent);
80 Ok(sent)
81 }
82
83 /// # Errors
84 ///
85 /// Returns `neqo_transport` errors.
86 pub fn send_atomic(&mut self, conn: &mut Connection, to_send: &[u8]) -> Res<bool> {
87 // First try to send anything that is in the buffer.
88 self.send_buffer(conn)?;
89 let Self::Initialized { stream_id, buf } = self else {
90 return Ok(false);
91 };
92 if !buf.is_empty() {
93 return Ok(false);
94 }
95 let res = conn.stream_send_atomic(*stream_id, to_send)?;
96 if res {
97 qlog::h3_data_moved_down(conn.qlog_mut(), *stream_id, to_send.len());
98 }
99 Ok(res)
100 }
101
102 #[must_use]
103 pub fn has_buffered_data(&self) -> bool {
104 if let Self::Initialized { buf, .. } = self {
105 !buf.is_empty()
106 } else {
107 false
108 }
109 }
110 }
111
112 impl From<&BufferedStream> for Option<StreamId> {
113 fn from(stream: &BufferedStream) -> Self {
114 if let BufferedStream::Initialized { stream_id, .. } = stream {
115 Some(*stream_id)
116 } else {
117 None
118 }
119 }
120 }