wisp_mux/extensions/
mod.rs1pub mod password;
3pub mod udp;
4
5use std::ops::{Deref, DerefMut};
6
7use async_trait::async_trait;
8use bytes::{BufMut, Bytes, BytesMut};
9
10use crate::{
11 ws::{LockedWebSocketWrite, WebSocketRead},
12 Role, WispError,
13};
14
15#[derive(Debug)]
17pub struct AnyProtocolExtension(Box<dyn ProtocolExtension + Sync + Send>);
18
19impl AnyProtocolExtension {
20 pub fn new<T: ProtocolExtension + Sync + Send + 'static>(extension: T) -> Self {
22 Self(Box::new(extension))
23 }
24}
25
26impl Deref for AnyProtocolExtension {
27 type Target = dyn ProtocolExtension;
28 fn deref(&self) -> &Self::Target {
29 self.0.deref()
30 }
31}
32
33impl DerefMut for AnyProtocolExtension {
34 fn deref_mut(&mut self) -> &mut Self::Target {
35 self.0.deref_mut()
36 }
37}
38
39impl Clone for AnyProtocolExtension {
40 fn clone(&self) -> Self {
41 Self(self.0.box_clone())
42 }
43}
44
45impl From<AnyProtocolExtension> for Bytes {
46 fn from(value: AnyProtocolExtension) -> Self {
47 let mut bytes = BytesMut::with_capacity(5);
48 let payload = value.encode();
49 bytes.put_u8(value.get_id());
50 bytes.put_u32_le(payload.len() as u32);
51 bytes.extend(payload);
52 bytes.freeze()
53 }
54}
55
56#[async_trait]
61pub trait ProtocolExtension: std::fmt::Debug {
62 fn get_id(&self) -> u8;
64 fn get_supported_packets(&self) -> &'static [u8];
68
69 fn encode(&self) -> Bytes;
71
72 async fn handle_handshake(
76 &mut self,
77 read: &mut dyn WebSocketRead,
78 write: &LockedWebSocketWrite,
79 ) -> Result<(), WispError>;
80
81 async fn handle_packet(
83 &mut self,
84 packet: Bytes,
85 read: &mut dyn WebSocketRead,
86 write: &LockedWebSocketWrite,
87 ) -> Result<(), WispError>;
88
89 fn box_clone(&self) -> Box<dyn ProtocolExtension + Sync + Send>;
91}
92
93pub trait ProtocolExtensionBuilder {
95 fn get_id(&self) -> u8;
99
100 fn build_from_bytes(&self, bytes: Bytes, role: Role)
102 -> Result<AnyProtocolExtension, WispError>;
103
104 fn build_to_extension(&self, role: Role) -> AnyProtocolExtension;
106}