wisp_mux/extensions/
udp.rs

1//! UDP protocol extension.
2//!
3//! # Example
4//! ```
5//! let (mux, fut) = ServerMux::new(
6//!     rx,
7//!     tx,
8//!     128,
9//!     Some(&[Box::new(UdpProtocolExtensionBuilder)])
10//! );
11//! ```
12//! See [the docs](https://github.com/MercuryWorkshop/wisp-protocol/blob/v2/protocol.md#0x01---udp)
13use async_trait::async_trait;
14use bytes::Bytes;
15
16use crate::{
17	ws::{LockedWebSocketWrite, WebSocketRead},
18	WispError,
19};
20
21use super::{AnyProtocolExtension, ProtocolExtension, ProtocolExtensionBuilder};
22
23#[derive(Debug)]
24/// UDP protocol extension.
25pub struct UdpProtocolExtension;
26
27impl UdpProtocolExtension {
28	/// UDP protocol extension ID.
29	pub const ID: u8 = 0x01;
30}
31
32#[async_trait]
33impl ProtocolExtension for UdpProtocolExtension {
34	fn get_id(&self) -> u8 {
35		Self::ID
36	}
37
38	fn get_supported_packets(&self) -> &'static [u8] {
39		&[]
40	}
41
42	fn encode(&self) -> Bytes {
43		Bytes::new()
44	}
45
46	async fn handle_handshake(
47		&mut self,
48		_: &mut dyn WebSocketRead,
49		_: &LockedWebSocketWrite,
50	) -> Result<(), WispError> {
51		Ok(())
52	}
53
54	async fn handle_packet(
55		&mut self,
56		_: Bytes,
57		_: &mut dyn WebSocketRead,
58		_: &LockedWebSocketWrite,
59	) -> Result<(), WispError> {
60		Ok(())
61	}
62
63	fn box_clone(&self) -> Box<dyn ProtocolExtension + Sync + Send> {
64		Box::new(Self)
65	}
66}
67
68impl From<UdpProtocolExtension> for AnyProtocolExtension {
69	fn from(value: UdpProtocolExtension) -> Self {
70		AnyProtocolExtension(Box::new(value))
71	}
72}
73
74/// UDP protocol extension builder.
75pub struct UdpProtocolExtensionBuilder;
76
77impl ProtocolExtensionBuilder for UdpProtocolExtensionBuilder {
78	fn get_id(&self) -> u8 {
79		UdpProtocolExtension::ID
80	}
81
82	fn build_from_bytes(
83		&self,
84		_: Bytes,
85		_: crate::Role,
86	) -> Result<AnyProtocolExtension, WispError> {
87		Ok(UdpProtocolExtension.into())
88	}
89
90	fn build_to_extension(&self, _: crate::Role) -> AnyProtocolExtension {
91		UdpProtocolExtension.into()
92	}
93}