1use std::{future::Future, pin::Pin, sync::Arc};
2
3use crate::{Error, coding, ietf, lite};
4
5pub const VERSIONS: [coding::Version; 3] = [
9 lite::Version::Draft02.coding(),
10 lite::Version::Draft01.coding(),
11 ietf::Version::Draft14.coding(),
12];
13
14pub const ALPNS: [&str; 2] = [lite::ALPN, ietf::ALPN];
16
17pub struct Session {
23 session: Arc<dyn SessionInner>,
24}
25
26impl Session {
27 pub(super) fn new<S: web_transport_trait::Session>(session: S) -> Self {
28 Self {
29 session: Arc::new(session),
30 }
31 }
32
33 pub fn close(self, err: Error) {
35 self.session.close(err.to_code(), err.to_string().as_ref());
36 }
37
38 pub async fn closed(&self) -> Result<(), Error> {
41 let err = self.session.closed().await;
42 Err(Error::Transport(err))
43 }
44}
45
46trait SessionInner: Send + Sync {
48 fn close(&self, code: u32, reason: &str);
49 fn closed(&self) -> Pin<Box<dyn Future<Output = Arc<dyn crate::error::SendSyncError>> + Send + '_>>;
50}
51
52impl<S: web_transport_trait::Session> SessionInner for S {
53 fn close(&self, code: u32, reason: &str) {
54 S::close(self, code, reason);
55 }
56
57 fn closed(&self) -> Pin<Box<dyn Future<Output = Arc<dyn crate::error::SendSyncError>> + Send + '_>> {
58 Box::pin(async move { Arc::new(S::closed(self).await) as Arc<dyn crate::error::SendSyncError> })
59 }
60}