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, SessionError, 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    ///
35    /// Unlike Quinn, this returns None if the code is not a valid WebTransport error code.
36    /// Also unlike Quinn, this returns a SessionError, not a StoppedError, because 0-RTT is not supported.
37    pub async fn stopped(&mut self) -> Result<Option<u32>, SessionError> {
38        match self.stream.stopped().await {
39            Ok(Some(code)) => Ok(web_transport_proto::error_from_http3(code.into_inner())),
40            Ok(None) => Ok(None),
41            Err(quinn::StoppedError::ConnectionLost(e)) => Err(e.into()),
42            Err(quinn::StoppedError::ZeroRttRejected) => unreachable!("0-RTT not supported"),
43        }
44    }
45
46    // Unfortunately, we have to wrap WriteError for a bunch of functions.
47
48    /// Write some data to the stream, returning the size written. See [`quinn::SendStream::write`].
49    pub async fn write(&mut self, buf: &[u8]) -> Result<usize, WriteError> {
50        self.stream.write(buf).await.map_err(Into::into)
51    }
52
53    /// Write all of the data to the stream. See [`quinn::SendStream::write_all`].
54    pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteError> {
55        self.stream.write_all(buf).await.map_err(Into::into)
56    }
57
58    /// Write chunks of data to the stream. See [`quinn::SendStream::write_chunks`].
59    pub async fn write_chunks(
60        &mut self,
61        bufs: &mut [Bytes],
62    ) -> Result<quinn_proto::Written, WriteError> {
63        self.stream.write_chunks(bufs).await.map_err(Into::into)
64    }
65
66    /// Write a chunk of data to the stream. See [`quinn::SendStream::write_chunk`].
67    pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError> {
68        self.stream.write_chunk(buf).await.map_err(Into::into)
69    }
70
71    /// Write all of the chunks of data to the stream. See [`quinn::SendStream::write_all_chunks`].
72    pub async fn write_all_chunks(&mut self, bufs: &mut [Bytes]) -> Result<(), WriteError> {
73        self.stream.write_all_chunks(bufs).await.map_err(Into::into)
74    }
75
76    /// Wait until all of the data has been written to the stream. See [`quinn::SendStream::finish`].
77    pub fn finish(&mut self) -> Result<(), ClosedStream> {
78        self.stream.finish().map_err(Into::into)
79    }
80
81    pub fn set_priority(&self, order: i32) -> Result<(), ClosedStream> {
82        self.stream.set_priority(order).map_err(Into::into)
83    }
84
85    pub fn priority(&self) -> Result<i32, ClosedStream> {
86        self.stream.priority().map_err(Into::into)
87    }
88}
89
90impl tokio::io::AsyncWrite for SendStream {
91    fn poll_write(
92        mut self: Pin<&mut Self>,
93        cx: &mut Context<'_>,
94        buf: &[u8],
95    ) -> Poll<io::Result<usize>> {
96        // We have to use this syntax because quinn added its own poll_write method.
97        tokio::io::AsyncWrite::poll_write(Pin::new(&mut self.stream), cx, buf)
98    }
99
100    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
101        Pin::new(&mut self.stream).poll_flush(cx)
102    }
103
104    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
105        Pin::new(&mut self.stream).poll_shutdown(cx)
106    }
107}