open_net/api/ws/
incoming.rs1use 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#[derive(Clone)]
12pub enum IncomingPayload {
13 Message(
15 Message,
17 ),
18 Ping(
20 Bytes,
22 ),
23 Pong(
25 Bytes,
27 ),
28 Close(
30 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#[derive(Clone, Copy, Debug, Eq, PartialEq)]
54pub(crate) struct IncomingOrigin {
55 client_id: ClientId,
57 session_id: SessionId,
59 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#[derive(Clone, Debug)]
79pub struct IncomingMessage {
80 origin: IncomingOrigin,
82 payload: Arc<IncomingPayload>,
84 received_at: SystemTime,
86 dispatch: Option<IncomingDispatch>,
88}
89
90#[derive(Clone, Debug)]
92pub(crate) struct IncomingDispatch {
93 pub(crate) pending: Weak<crate::module::ws_client::native_pending::NativePending>,
95 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 pub fn payload(&self) -> &IncomingPayload {
127 self.payload.as_ref()
128 }
129
130 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 pub fn received_at(&self) -> SystemTime {
140 self.received_at
141 }
142
143 pub fn client_id(&self) -> ClientId {
145 self.origin.client_id
146 }
147
148 pub fn session_id(&self) -> SessionId {
150 self.origin.session_id
151 }
152
153 pub fn connection_id(&self) -> ConnectionId {
155 self.origin.connection_id
156 }
157}
158
159#[cfg(test)]
160#[path = "incoming_tests.rs"]
161mod tests;