Skip to main content

udp/hook/
impl.rs

1use super::*;
2
3/// Provides a default implementation for `ServerControlHook`.
4impl Default for ServerControlHook {
5    /// Creates a default `ServerControlHook` with no-op hooks.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - A new instance with default no-op hooks.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            wait_hook: Arc::new(|| Box::pin(async {})),
14            shutdown_hook: Arc::new(|| Box::pin(async {})),
15        }
16    }
17}
18
19/// Implementation of methods for `ServerControlHook`.
20impl ServerControlHook {
21    /// Sets the wait hook.
22    ///
23    /// # Arguments
24    ///
25    /// - `hook` - The wait hook to set.
26    pub(crate) fn set_wait_hook(
27        &mut self,
28        hook: Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync>,
29    ) {
30        self.wait_hook = hook;
31    }
32
33    /// Sets the shutdown hook.
34    ///
35    /// # Arguments
36    ///
37    /// - `hook` - The shutdown hook to set.
38    pub(crate) fn set_shutdown_hook(
39        &mut self,
40        hook: Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync>,
41    ) {
42        self.shutdown_hook = hook;
43    }
44
45    /// Gets the wait hook.
46    ///
47    /// # Returns
48    ///
49    /// - `Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync>` - The wait hook.
50    pub fn get_wait_hook(&self) -> Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync> {
51        self.wait_hook.clone()
52    }
53
54    /// Gets the shutdown hook.
55    ///
56    /// # Returns
57    ///
58    /// - `Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync>` - The shutdown hook.
59    pub fn get_shutdown_hook(&self) -> Arc<dyn Fn() -> SendableAsyncTask<()> + Send + Sync> {
60        self.shutdown_hook.clone()
61    }
62
63    /// Waits for the server to finish.
64    pub async fn wait(&self) {
65        (self.get_wait_hook())().await;
66    }
67
68    /// Shuts down the server.
69    pub async fn shutdown(&self) {
70        (self.get_shutdown_hook())().await;
71    }
72}
73
74/// Implementation of methods for `HandlerState`.
75impl HandlerState {
76    /// Creates a new HandlerState instance.
77    ///
78    /// # Arguments
79    ///
80    /// - `socket` - The network socket.
81    ///
82    /// # Returns
83    ///
84    /// - `Self` - The newly created handler state.
85    #[inline(always)]
86    pub(crate) fn new(socket: ArcRwLockUdpSocket) -> Self {
87        Self { socket }
88    }
89
90    /// Gets the socket.
91    ///
92    /// # Returns
93    ///
94    /// - `ArcRwLockUdpSocket` - The network socket.
95    pub(crate) fn get_socket(&self) -> ArcRwLockUdpSocket {
96        self.socket.clone()
97    }
98}
99
100/// Implementation of `ServerHook` for `DefaultServerHook`.
101impl ServerHook for DefaultServerHook {
102    /// Creates a new instance of `DefaultServerHook`.
103    ///
104    /// # Arguments
105    ///
106    /// - `_ctx` - The request context (unused).
107    ///
108    /// # Returns
109    ///
110    /// A future that resolves to a new `DefaultServerHook` instance.
111    async fn new(_ctx: &Context) -> Self {
112        Self
113    }
114
115    /// Executes the default hook's processing logic (no-op).
116    ///
117    /// # Arguments
118    ///
119    /// - `_ctx` - The request context (unused).
120    async fn handle(self, _ctx: &Context) {}
121}