Skip to main content

web_transport_ws/
session.rs

1use qmux::tungstenite;
2
3/// A WebTransport session over WebSocket.
4///
5/// # Deprecated
6///
7/// Use [`qmux::Session`] with [`qmux::ws::accept`] / [`qmux::ws::connect`] instead.
8#[deprecated(note = "use qmux::Session with qmux::ws::accept/connect instead")]
9#[derive(Clone)]
10pub struct Session(qmux::Session);
11
12#[allow(deprecated)]
13impl Session {
14    /// Wrap a pre-upgraded WebSocket as a server-side session.
15    pub fn accept<T>(ws: T, protocol: Option<String>) -> Self
16    where
17        T: futures::Stream<Item = Result<tungstenite::Message, tungstenite::Error>>
18            + futures::Sink<tungstenite::Message, Error = tungstenite::Error>
19            + Unpin
20            + Send
21            + 'static,
22    {
23        Self(qmux::ws::accept(ws, protocol.as_deref()))
24    }
25
26    /// Wrap a pre-upgraded WebSocket as a client-side session.
27    pub fn connect<T>(ws: T, protocol: Option<String>) -> Self
28    where
29        T: futures::Stream<Item = Result<tungstenite::Message, tungstenite::Error>>
30            + futures::Sink<tungstenite::Message, Error = tungstenite::Error>
31            + Unpin
32            + Send
33            + 'static,
34    {
35        Self(qmux::ws::connect(ws, protocol.as_deref()))
36    }
37
38    /// Get the inner qmux::Session.
39    pub fn into_inner(self) -> qmux::Session {
40        self.0
41    }
42}
43
44#[allow(deprecated)]
45impl From<Session> for qmux::Session {
46    fn from(s: Session) -> Self {
47        s.0
48    }
49}
50
51#[allow(deprecated)]
52impl From<qmux::Session> for Session {
53    fn from(s: qmux::Session) -> Self {
54        Self(s)
55    }
56}
57
58#[allow(deprecated)]
59impl web_transport_trait::Session for Session {
60    type SendStream = qmux::SendStream;
61    type RecvStream = qmux::RecvStream;
62    type Error = qmux::Error;
63
64    async fn accept_uni(&self) -> Result<Self::RecvStream, Self::Error> {
65        self.0.accept_uni().await
66    }
67
68    async fn accept_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
69        self.0.accept_bi().await
70    }
71
72    async fn open_uni(&self) -> Result<Self::SendStream, Self::Error> {
73        self.0.open_uni().await
74    }
75
76    async fn open_bi(&self) -> Result<(Self::SendStream, Self::RecvStream), Self::Error> {
77        self.0.open_bi().await
78    }
79
80    fn close(&self, code: u32, reason: &str) {
81        self.0.close(code, reason)
82    }
83
84    async fn closed(&self) -> Self::Error {
85        self.0.closed().await
86    }
87
88    fn protocol(&self) -> Option<&str> {
89        self.0.protocol()
90    }
91
92    fn send_datagram(&self, payload: bytes::Bytes) -> Result<(), Self::Error> {
93        self.0.send_datagram(payload)
94    }
95
96    async fn recv_datagram(&self) -> Result<bytes::Bytes, Self::Error> {
97        self.0.recv_datagram().await
98    }
99
100    fn max_datagram_size(&self) -> usize {
101        self.0.max_datagram_size()
102    }
103}