passcod_web_transport_quinn/
send.rs

1use std::{
2    io,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use bytes::Bytes;
8
9use crate::{ClosedStream, StoppedError, WriteError};
10
11/// A stream that can be used to send bytes. See [`quinn::SendStream`].
12///
13/// This wrapper is mainly needed for error codes, which is unfortunate.
14/// WebTransport uses u32 error codes and they're mapped in a reserved HTTP/3 error space.
15#[derive(Debug)]
16pub struct SendStream {
17    stream: quinn::SendStream,
18}
19
20impl SendStream {
21    pub(crate) fn new(stream: quinn::SendStream) -> Self {
22        Self { stream }
23    }
24
25    /// Abruptly reset the stream with the provided error code. See [`quinn::SendStream::reset`].
26    /// This is a u32 with WebTransport because we share the error space with HTTP/3.
27    pub fn reset(&mut self, code: u32) -> Result<(), ClosedStream> {
28        let code = web_transport_proto::error_to_http3(code);
29        let code = quinn::VarInt::try_from(code).unwrap();
30        self.stream.reset(code).map_err(Into::into)
31    }
32
33    /// Wait until the stream has been stopped and return the error code. See [`quinn::SendStream::stopped`].
34    /// Unlike Quinn, this returns None if the code is not a valid WebTransport error code.
35    pub async fn stopped(&mut self) -> Result<Option<u32>, StoppedError> {
36        Ok(match self.stream.stopped().await? {
37            Some(code) => web_transport_proto::error_from_http3(code.into_inner()),
38            None => None,
39        })
40    }
41
42    // Unfortunately, we have to wrap WriteError for a bunch of functions.
43
44    /// Write some data to the stream, returning the size written. See [`quinn::SendStream::write`].
45    pub async fn write(&mut self, buf: &[u8]) -> Result<usize, WriteError> {
46        self.stream.write(buf).await.map_err(Into::into)
47    }
48
49    /// Write all of the data to the stream. See [`quinn::SendStream::write_all`].
50    pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteError> {
51        self.stream.write_all(buf).await.map_err(Into::into)
52    }
53
54    /// Write chunks of data to the stream. See [`quinn::SendStream::write_chunks`].
55    pub async fn write_chunks(
56        &mut self,
57        bufs: &mut [Bytes],
58    ) -> Result<quinn_proto::Written, WriteError> {
59        self.stream.write_chunks(bufs).await.map_err(Into::into)
60    }
61
62    /// Write a chunk of data to the stream. See [`quinn::SendStream::write_chunk`].
63    pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError> {
64        self.stream.write_chunk(buf).await.map_err(Into::into)
65    }
66
67    /// Write all of the chunks of data to the stream. See [`quinn::SendStream::write_all_chunks`].
68    pub async fn write_all_chunks(&mut self, bufs: &mut [Bytes]) -> Result<(), WriteError> {
69        self.stream.write_all_chunks(bufs).await.map_err(Into::into)
70    }
71
72    /// Wait until all of the data has been written to the stream. See [`quinn::SendStream::finish`].
73    pub fn finish(&mut self) -> Result<(), ClosedStream> {
74        self.stream.finish().map_err(Into::into)
75    }
76
77    pub fn set_priority(&self, order: i32) -> Result<(), ClosedStream> {
78        self.stream.set_priority(order).map_err(Into::into)
79    }
80
81    pub fn priority(&self) -> Result<i32, ClosedStream> {
82        self.stream.priority().map_err(Into::into)
83    }
84}
85
86impl tokio::io::AsyncWrite for SendStream {
87    fn poll_write(
88        mut self: Pin<&mut Self>,
89        cx: &mut Context<'_>,
90        buf: &[u8],
91    ) -> Poll<io::Result<usize>> {
92        // We have to use this syntax because quinn added its own poll_write method.
93        tokio::io::AsyncWrite::poll_write(Pin::new(&mut self.stream), cx, buf)
94    }
95
96    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
97        Pin::new(&mut self.stream).poll_flush(cx)
98    }
99
100    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
101        Pin::new(&mut self.stream).poll_shutdown(cx)
102    }
103}