hyperlane_plugin_websocket/struct.rs
1use super::*;
2
3/// Stack-backed `fmt::Write` cursor for zero-allocation broadcast key formatting.
4pub(crate) struct StackKeyWriter<'a> {
5 /// Destination stack byte buffer.
6 pub(crate) buf: &'a mut [u8],
7 /// Current number of written UTF-8 bytes.
8 pub(crate) len: usize,
9}
10
11/// Represents a WebSocket instance.
12///
13/// This struct manages broadcast capabilities and holds the internal broadcast map
14/// responsible for handling message distribution to various WebSocket connections.
15#[derive(Clone, Data, Debug, Default)]
16pub struct WebSocket {
17 /// The internal broadcast map.
18 ///
19 /// This map is used for managing WebSocket message distribution.
20 pub(crate) broadcast_map: BroadcastMap<Vec<u8>>,
21}
22
23/// Process-wide [`WebSocketPlugin`] singleton holding a shared [`WebSocket`] room hub.
24#[derive(Clone, Debug, Default)]
25pub struct WebSocketPlugin {
26 /// Shared WebSocket broadcast hub.
27 pub(crate) websocket: WebSocket,
28}
29
30/// Configuration for a WebSocket connection.
31///
32/// This struct encapsulates all necessary parameters for setting up and managing
33/// a WebSocket connection, including context, buffer sizes, capacity, broadcast type,
34/// and hook handlers for different lifecycle events.
35///
36/// # Type Parameters
37///
38/// - `B`: The type used for broadcast keys, which must implement `BroadcastTypeTrait`.
39#[derive(Data)]
40pub struct WebSocketConfig<'a, B: BroadcastTypeTrait> {
41 /// The underlying stream for this WebSocket connection.
42 #[set(skip)]
43 pub(crate) stream: &'a mut Stream,
44 /// The Hyperlane context.
45 ///
46 /// This context is associated with this WebSocket connection.
47 #[set(skip)]
48 pub(crate) context: &'a mut Context,
49 /// The capacity.
50 ///
51 /// This is the capacity of the broadcast sender channel.
52 #[get(type(copy))]
53 #[set(skip)]
54 pub(crate) capacity: usize,
55 /// The broadcast type.
56 ///
57 /// This defines the type of broadcast this WebSocket connection will participate in
58 /// (point-to-point or point-to-group).
59 #[set(skip)]
60 pub(crate) broadcast_type: BroadcastType<B>,
61 /// The connected hook handler.
62 ///
63 /// This hook is executed when the WebSocket connection is established.
64 #[set(skip)]
65 pub(crate) connected_hook: ServerHookHandler,
66 /// The request hook handler.
67 ///
68 /// This hook is executed when a new request is received on the WebSocket.
69 #[set(skip)]
70 pub(crate) request_hook: ServerHookHandler,
71 /// The sended hook handler.
72 ///
73 /// This hook is executed after a message has been successfully sent over the WebSocket.
74 #[set(skip)]
75 pub(crate) sended_hook: ServerHookHandler,
76 /// The closed hook handler.
77 ///
78 /// This hook is executed when the WebSocket connection is closed.
79 #[set(skip)]
80 pub(crate) closed_hook: ServerHookHandler,
81}