Skip to main content

wsio_server/namespace/
mod.rs

1use std::sync::Arc;
2
3use anyhow::Result;
4use arc_swap::ArcSwap;
5use futures_util::{
6    SinkExt,
7    StreamExt,
8};
9use http::{
10    HeaderMap,
11    Uri,
12};
13use hyper::upgrade::{
14    OnUpgrade,
15    Upgraded,
16};
17use hyper_util::rt::TokioIo;
18use kikiutils::{
19    atomic::enum_cell::AtomicEnumCell,
20    types::fx_collections::FxDashMap,
21};
22use num_enum::{
23    IntoPrimitive,
24    TryFromPrimitive,
25};
26use roaring::RoaringTreemap;
27use serde::Serialize;
28use tokio::{
29    join,
30    select,
31    spawn,
32    sync::Mutex,
33    task::JoinSet,
34    time::timeout,
35};
36use tokio_tungstenite::{
37    WebSocketStream,
38    tungstenite::{
39        Message,
40        protocol::Role,
41    },
42};
43
44pub(crate) mod builder;
45mod config;
46pub mod operators;
47
48use self::{
49    config::WsIoServerNamespaceConfig,
50    operators::broadcast::WsIoServerNamespaceBroadcastOperator,
51};
52use crate::{
53    WsIoServer,
54    connection::WsIoServerConnection,
55    core::packet::WsIoPacket,
56    runtime::{
57        WsIoServerRuntime,
58        WsIoServerRuntimeStatus,
59    },
60};
61
62// Enums
63#[repr(u8)]
64#[derive(Debug, Eq, IntoPrimitive, PartialEq, TryFromPrimitive)]
65enum NamespaceStatus {
66    Running,
67    Stopped,
68    Stopping,
69}
70
71// Structs
72#[derive(Debug)]
73pub struct WsIoServerNamespace {
74    pub(crate) config: WsIoServerNamespaceConfig,
75    connection_ids: ArcSwap<RoaringTreemap>,
76    connections: FxDashMap<u64, Arc<WsIoServerConnection>>,
77    connection_task_set: Mutex<JoinSet<()>>,
78    rooms: FxDashMap<String, RoaringTreemap>,
79    runtime: Arc<WsIoServerRuntime>,
80    status: AtomicEnumCell<NamespaceStatus>,
81}
82
83impl WsIoServerNamespace {
84    fn new(config: WsIoServerNamespaceConfig, runtime: Arc<WsIoServerRuntime>) -> Arc<Self> {
85        Arc::new(Self {
86            config,
87            connection_ids: ArcSwap::new(Arc::new(RoaringTreemap::new())),
88            connections: FxDashMap::default(),
89            connection_task_set: Mutex::new(JoinSet::new()),
90            rooms: FxDashMap::default(),
91            runtime,
92            status: AtomicEnumCell::new(NamespaceStatus::Running),
93        })
94    }
95
96    // Private methods
97    async fn handle_upgraded_request(
98        self: &Arc<Self>,
99        headers: HeaderMap,
100        request_uri: Uri,
101        upgraded: Upgraded,
102    ) -> Result<()> {
103        #[cfg(feature = "tracing")]
104        tracing::debug!(
105            namespace = self.config.path,
106            request_path = request_uri.path(),
107            "handling upgraded WebSocket request"
108        );
109
110        // Create ws stream
111        let mut ws_stream =
112            WebSocketStream::from_raw_socket(TokioIo::new(upgraded), Role::Server, Some(self.config.websocket_config))
113                .await;
114
115        // Check runtime and namespace status
116        if !self.runtime.status.is(WsIoServerRuntimeStatus::Running) || !self.status.is(NamespaceStatus::Running) {
117            #[cfg(feature = "tracing")]
118            tracing::debug!(
119                namespace = self.config.path,
120                runtime_status = ?self.runtime.status.get(),
121                namespace_status = ?self.status.get(),
122                "rejecting upgraded request because server or namespace is not running"
123            );
124
125            ws_stream
126                .send((*self.encode_packet_to_message(&WsIoPacket::new_disconnect())?).clone())
127                .await?;
128
129            let _ = ws_stream.close(None).await;
130            return Ok(());
131        }
132
133        // Create connection
134        let (connection, mut message_rx) = WsIoServerConnection::new(headers, self.clone(), request_uri);
135
136        #[cfg(feature = "tracing")]
137        tracing::debug!(
138            namespace = self.config.path,
139            connection_id = connection.id(),
140            "accepted WebSocket connection"
141        );
142
143        // Split ws stream and spawn read and write tasks
144        let (mut ws_stream_writer, mut ws_stream_reader) = ws_stream.split();
145        let connection_clone = connection.clone();
146        let mut read_ws_stream_task = spawn(async move {
147            while let Some(message) = ws_stream_reader.next().await {
148                if match message {
149                    Ok(Message::Binary(bytes)) => {
150                        // Treat any single-byte binary frame as a client heartbeat and ignore it
151                        if bytes.len() == 1 {
152                            continue;
153                        }
154
155                        connection_clone.handle_incoming_packet(&bytes).await
156                    },
157                    Ok(Message::Close(_)) => {
158                        #[cfg(feature = "tracing")]
159                        tracing::debug!(connection_id = connection_clone.id(), "server read task received close frame");
160                        break;
161                    },
162                    Err(_err) => {
163                        #[cfg(feature = "tracing")]
164                        tracing::debug!(connection_id = connection_clone.id(), error = %_err, "server read task failed");
165                        break;
166                    },
167                    Ok(Message::Text(text)) => connection_clone.handle_incoming_packet(text.as_bytes()).await,
168                    _ => Ok(()),
169                }
170                .is_err()
171                {
172                    #[cfg(feature = "tracing")]
173                    tracing::debug!(
174                        connection_id = connection_clone.id(),
175                        "server read task stopped after packet handling error"
176                    );
177
178                    break;
179                }
180            }
181        });
182
183        let mut write_ws_stream_task = spawn(async move {
184            while let Some(message) = message_rx.recv().await {
185                let message = (*message).clone();
186                let is_close = matches!(message, Message::Close(_));
187                if ws_stream_writer.send(message).await.is_err() {
188                    #[cfg(feature = "tracing")]
189                    tracing::debug!("server write task failed to send message");
190                    break;
191                }
192
193                if is_close {
194                    #[cfg(feature = "tracing")]
195                    tracing::debug!("server write task sent close frame");
196                    let _ = ws_stream_writer.close().await;
197                    break;
198                }
199            }
200        });
201
202        // Try to init connection
203        match connection.init().await {
204            Ok(_) => {
205                #[cfg(feature = "tracing")]
206                tracing::debug!(connection_id = connection.id(), "server connection initialized");
207                // Wait for either read or write task to finish
208                select! {
209                    _ = &mut read_ws_stream_task => {
210                        #[cfg(feature = "tracing")]
211                        tracing::debug!(connection_id = connection.id(), "server read task finished; aborting write task");
212                        write_ws_stream_task.abort();
213                    },
214                    _ = &mut write_ws_stream_task => {
215                        #[cfg(feature = "tracing")]
216                        tracing::debug!(connection_id = connection.id(), "server write task finished; aborting read task");
217                        read_ws_stream_task.abort();
218                    },
219                }
220            },
221            Err(_err) => {
222                #[cfg(feature = "tracing")]
223                tracing::debug!(connection_id = connection.id(), error = %_err, "server connection initialization failed");
224                // Close connection
225                read_ws_stream_task.abort();
226                connection.close();
227                let _ = join!(read_ws_stream_task, write_ws_stream_task);
228            },
229        }
230
231        // Cleanup connection
232        connection.cleanup().await;
233
234        #[cfg(feature = "tracing")]
235        tracing::debug!(connection_id = connection.id(), "server connection stopped");
236        Ok(())
237    }
238
239    // Protected methods
240    #[inline]
241    pub(crate) fn add_connection_id_to_room(&self, room_name: &str, connection_id: u64) {
242        self.rooms
243            .entry(room_name.to_owned())
244            .or_default()
245            .insert(connection_id);
246    }
247
248    #[inline]
249    pub(crate) fn encode_packet_to_message(&self, packet: &WsIoPacket) -> Result<Arc<Message>> {
250        let bytes = self.config.packet_codec.encode(packet)?;
251        Ok(Arc::new(match self.config.packet_codec.is_text() {
252            // SAFETY: text packet codecs only produce valid UTF-8 payloads.
253            true => Message::Text(unsafe { String::from_utf8_unchecked(bytes).into() }),
254            false => Message::Binary(bytes.into()),
255        }))
256    }
257
258    pub(crate) async fn handle_on_upgrade_request(
259        self: &Arc<Self>,
260        headers: HeaderMap,
261        on_upgrade: OnUpgrade,
262        request_uri: Uri,
263    ) {
264        #[cfg(feature = "tracing")]
265        tracing::trace!(
266            namespace = self.config.path,
267            request_path = request_uri.path(),
268            "spawning WebSocket upgrade task"
269        );
270
271        let namespace = self.clone();
272        self.connection_task_set.lock().await.spawn(async move {
273            match timeout(namespace.config.http_request_upgrade_timeout, on_upgrade).await {
274                Ok(Ok(upgraded)) => {
275                    if let Err(_err) = namespace.handle_upgraded_request(headers, request_uri, upgraded).await {
276                        #[cfg(feature = "tracing")]
277                        tracing::debug!(namespace = namespace.config.path, error = %_err, "upgraded request handling failed");
278                    }
279                },
280                Ok(Err(_err)) => {
281                    #[cfg(feature = "tracing")]
282                    tracing::warn!(namespace = namespace.config.path, error = %_err, "HTTP upgrade failed");
283                },
284                Err(_err) => {
285                    #[cfg(feature = "tracing")]
286                    tracing::warn!(
287                        namespace = namespace.config.path,
288                        error = %_err,
289                        timeout_ms = namespace.config.http_request_upgrade_timeout.as_millis() as u64,
290                        "HTTP upgrade timed out"
291                    );
292                },
293            }
294        });
295    }
296
297    #[inline]
298    pub(crate) fn insert_connection(&self, connection: Arc<WsIoServerConnection>) {
299        self.connections.insert(connection.id(), connection.clone());
300        self.runtime.insert_connection_id(connection.id());
301        self.connection_ids.rcu(|old_connection_ids| {
302            let mut new_connection_ids = (**old_connection_ids).clone();
303            new_connection_ids.insert(connection.id());
304            new_connection_ids
305        });
306    }
307
308    #[inline]
309    pub(crate) fn remove_connection(&self, id: u64) {
310        self.connections.remove(&id);
311        self.runtime.remove_connection_id(id);
312        self.connection_ids.rcu(|old_connection_ids| {
313            let mut new_connection_ids = (**old_connection_ids).clone();
314            new_connection_ids.remove(id);
315            new_connection_ids
316        });
317    }
318
319    #[inline]
320    pub(crate) fn remove_connection_id_from_room(&self, room_name: &str, connection_id: u64) {
321        if let Some(mut entry) = self.rooms.get_mut(room_name) {
322            entry.remove(connection_id);
323        }
324
325        self.rooms.remove_if(room_name, |_, entry| entry.is_empty());
326    }
327
328    // Public methods
329    pub async fn close_all(self: &Arc<Self>) {
330        WsIoServerNamespaceBroadcastOperator::new(self.clone()).close().await;
331    }
332
333    #[inline]
334    pub fn connection_count(&self) -> usize {
335        self.connections.len()
336    }
337
338    pub async fn disconnect_all(self: &Arc<Self>) -> Result<()> {
339        WsIoServerNamespaceBroadcastOperator::new(self.clone())
340            .disconnect()
341            .await
342    }
343
344    pub async fn emit<D: Serialize>(self: &Arc<Self>, event: impl AsRef<str>, data: Option<&D>) -> Result<()> {
345        WsIoServerNamespaceBroadcastOperator::new(self.clone())
346            .emit(event, data)
347            .await
348    }
349
350    #[inline]
351    pub fn except(
352        self: &Arc<Self>,
353        room_names: impl IntoIterator<Item = impl Into<String>>,
354    ) -> WsIoServerNamespaceBroadcastOperator {
355        WsIoServerNamespaceBroadcastOperator::new(self.clone()).except(room_names)
356    }
357
358    #[inline]
359    pub fn path(&self) -> &str {
360        &self.config.path
361    }
362
363    #[inline]
364    pub fn server(&self) -> WsIoServer {
365        WsIoServer(self.runtime.clone())
366    }
367
368    pub async fn shutdown(self: &Arc<Self>) {
369        match self.status.get() {
370            NamespaceStatus::Stopped => return,
371            NamespaceStatus::Running => {
372                #[cfg(feature = "tracing")]
373                tracing::info!(namespace = self.config.path, "shutting down namespace");
374                self.status.store(NamespaceStatus::Stopping)
375            },
376            _ => unreachable!(),
377        }
378
379        self.close_all().await;
380        let mut connection_task_set = self.connection_task_set.lock().await;
381        while connection_task_set.join_next().await.is_some() {}
382
383        self.status.store(NamespaceStatus::Stopped);
384
385        #[cfg(feature = "tracing")]
386        tracing::info!(namespace = self.config.path, "namespace stopped");
387    }
388
389    #[inline]
390    pub fn to(
391        self: &Arc<Self>,
392        room_names: impl IntoIterator<Item = impl Into<String>>,
393    ) -> WsIoServerNamespaceBroadcastOperator {
394        WsIoServerNamespaceBroadcastOperator::new(self.clone()).to(room_names)
395    }
396}
397
398#[cfg(test)]
399mod tests {
400    use super::*;
401    use crate::WsIoServer;
402
403    fn create_test_namespace() -> Arc<WsIoServerNamespace> {
404        WsIoServer::builder()
405            .build()
406            .new_namespace_builder("/test")
407            .register()
408            .unwrap()
409    }
410
411    #[test]
412    fn test_namespace_new() {
413        let namespace = create_test_namespace();
414        assert_eq!(namespace.path(), "/test");
415        assert_eq!(namespace.connection_count(), 0);
416    }
417
418    #[test]
419    fn test_namespace_add_remove_connection_id_to_room() {
420        let namespace = create_test_namespace();
421        namespace.add_connection_id_to_room("room1", 1);
422        namespace.add_connection_id_to_room("room1", 2);
423        namespace.add_connection_id_to_room("room2", 3);
424
425        assert_eq!(namespace.rooms.get("room1").unwrap().len(), 2);
426        assert!(namespace.rooms.get("room1").unwrap().contains(1));
427        assert!(namespace.rooms.get("room1").unwrap().contains(2));
428        assert_eq!(namespace.rooms.get("room2").unwrap().len(), 1);
429
430        namespace.remove_connection_id_from_room("room1", 1);
431        assert_eq!(namespace.rooms.get("room1").unwrap().len(), 1);
432        assert!(namespace.rooms.get("room1").unwrap().contains(2));
433
434        namespace.remove_connection_id_from_room("room1", 2);
435        namespace.remove_connection_id_from_room("room2", 3);
436
437        assert!(!namespace.rooms.contains_key("room1"));
438        assert!(!namespace.rooms.contains_key("room2"));
439    }
440
441    #[test]
442    fn test_namespace_encode_packet_to_message() {
443        let namespace = create_test_namespace();
444        let packet = WsIoPacket::new_disconnect();
445        let message = namespace.encode_packet_to_message(&packet).unwrap();
446
447        assert!(matches!(&*message, Message::Text(_)));
448    }
449
450    #[tokio::test]
451    async fn test_namespace_shutdown_idempotent() {
452        let namespace = create_test_namespace();
453        namespace.clone().shutdown().await;
454        // Shutting down again should be safe
455        namespace.shutdown().await;
456    }
457
458    #[tokio::test]
459    async fn test_broadcast_operator_disconnect_with_no_connections() {
460        let namespace = create_test_namespace();
461        namespace.to(["room1"]).disconnect().await.unwrap();
462    }
463
464    #[tokio::test]
465    async fn test_broadcast_operator_emit_requires_running() {
466        let namespace = create_test_namespace();
467        // Shutdown to make status invalid
468        namespace.clone().shutdown().await;
469
470        let error = namespace
471            .to(["room1"])
472            .emit("event", Option::<&()>::None)
473            .await
474            .unwrap_err();
475
476        assert!(error.to_string().contains("invalid status"));
477    }
478}