Skip to main content

open_net/api/ws/
incoming.rs

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