Skip to main content

open_net/api/ws/
incoming.rs

1use super::{ClientId, ConnectionId, Message, PeerClose, SessionId};
2use bytes::Bytes;
3use std::sync::Arc;
4use std::sync::Weak;
5use std::time::SystemTime;
6
7/// Received business message or protocol-control payload.
8#[derive(Clone, Debug)]
9pub enum IncomingPayload {
10    /// Text or binary business message.
11    Message(
12        /// Owned business body.
13        Message,
14    ),
15    /// Ping control frame sent by the peer.
16    Ping(
17        /// Raw bytes carried by the Ping frame.
18        Bytes,
19    ),
20    /// Pong control frame sent by the peer.
21    Pong(
22        /// Raw bytes carried by the Pong frame.
23        Bytes,
24    ),
25    /// Close control frame sent by the peer.
26    Close(
27        /// Decoded close code and reason.
28        PeerClose,
29    ),
30}
31
32/// Source identity assigned by the receiving connection; owns no session resources.
33#[derive(Clone, Copy, Debug, Eq, PartialEq)]
34pub(crate) struct IncomingOrigin {
35    /// Client identity that received the item.
36    client_id: ClientId,
37    /// Logical session identity that received the item.
38    session_id: SessionId,
39    /// Physical connection that received the item.
40    connection_id: ConnectionId,
41}
42
43impl IncomingOrigin {
44    pub(crate) fn new(
45        client_id: ClientId,
46        session_id: SessionId,
47        connection_id: ConnectionId,
48    ) -> Self {
49        Self {
50            client_id,
51            session_id,
52            connection_id,
53        }
54    }
55}
56
57/// Immutable inbound item; clones share one payload allocation.
58#[derive(Clone, Debug)]
59pub struct IncomingMessage {
60    /// Client, session, and physical connection that produced the item.
61    origin: IncomingOrigin,
62    /// Business or control payload shared by all clones.
63    payload: Arc<IncomingPayload>,
64    /// System time recorded when the SDK received the item.
65    received_at: SystemTime,
66    /// Response-dispatch identity, or `None` before request dispatch binding.
67    dispatch: Option<IncomingDispatch>,
68}
69
70/// Response-dispatch identity attached to an inbound item for source and validity checks.
71#[derive(Clone, Debug)]
72pub(crate) struct IncomingDispatch {
73    /// Weak reference to the request table; does not extend table or session lifetime.
74    pub(crate) pending: Weak<crate::module::ws_client::native_pending::NativePending>,
75    /// Identifier allocated by that table for this inbound dispatch.
76    pub(crate) id: u64,
77}
78
79impl IncomingMessage {
80    pub(crate) fn new(
81        origin: IncomingOrigin,
82        payload: IncomingPayload,
83        received_at: SystemTime,
84    ) -> Self {
85        Self {
86            origin,
87            payload: Arc::new(payload),
88            received_at,
89            dispatch: None,
90        }
91    }
92
93    pub(crate) fn bind_dispatch(
94        &mut self,
95        pending: Weak<crate::module::ws_client::native_pending::NativePending>,
96        id: u64,
97    ) {
98        self.dispatch = Some(IncomingDispatch { pending, id });
99    }
100
101    pub(crate) fn dispatch(&self) -> Option<&IncomingDispatch> {
102        self.dispatch.as_ref()
103    }
104
105    /// Borrows the complete inbound payload, including control frames.
106    pub fn payload(&self) -> &IncomingPayload {
107        self.payload.as_ref()
108    }
109
110    /// Returns the business message, or `None` for control frames.
111    pub fn message(&self) -> Option<&Message> {
112        match self.payload() {
113            IncomingPayload::Message(message) => Some(message),
114            IncomingPayload::Ping(_) | IncomingPayload::Pong(_) | IncomingPayload::Close(_) => None,
115        }
116    }
117
118    /// Returns the SDK receive timestamp.
119    pub fn received_at(&self) -> SystemTime {
120        self.received_at
121    }
122
123    /// Returns the originating client identity.
124    pub fn client_id(&self) -> ClientId {
125        self.origin.client_id
126    }
127
128    /// Returns the originating logical session identity.
129    pub fn session_id(&self) -> SessionId {
130        self.origin.session_id
131    }
132
133    /// Returns the originating physical connection identity.
134    pub fn connection_id(&self) -> ConnectionId {
135        self.origin.connection_id
136    }
137}
138
139#[cfg(test)]
140#[path = "incoming_tests.rs"]
141mod tests;