1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! WebSocket stream implementation
use crate::{Message, WebSocketError, WsHeartbeatConfig};
use futures_util::{
stream::{SplitSink, SplitStream},
SinkExt, Stream, StreamExt,
};
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc;
use tokio_tungstenite::WebSocketStream as TungsteniteStream;
/// Type alias for the upgraded connection
type UpgradedConnection = TungsteniteStream<TokioIo<Upgraded>>;
/// Internal implementation of the WebSocket stream
#[allow(clippy::large_enum_variant)]
enum StreamImpl {
/// Direct connection (no heartbeat/management)
Direct(UpgradedConnection),
/// Managed connection (heartbeat/cleanup running in background task)
Managed {
tx: mpsc::Sender<Message>,
rx: mpsc::Receiver<Result<Message, WebSocketError>>,
},
}
/// A WebSocket stream
pub struct WebSocketStream {
inner: StreamImpl,
}
impl WebSocketStream {
/// Create a new direct WebSocket stream
pub(crate) fn new(inner: UpgradedConnection) -> Self {
Self {
inner: StreamImpl::Direct(inner),
}
}
/// Create a new managed WebSocket stream with heartbeat
pub(crate) fn new_managed(inner: UpgradedConnection, config: WsHeartbeatConfig) -> Self {
let (mut sender, mut receiver) = inner.split();
let (user_tx, mut internal_rx) = mpsc::channel::<Message>(32);
let (internal_tx, user_rx) = mpsc::channel::<Result<Message, WebSocketError>>(32);
// Spawn management task
tokio::spawn(async move {
let mut heartbeat_interval = tokio::time::interval(config.interval);
// First tick finishes immediately
heartbeat_interval.tick().await;
// For pong tracking, we can just track last activity or strictly check pongs.
// Simplified: we rely on TCP checks and ping writing success.
// If we want to enforce timeout, we need to track "last pong".
// Tungstenite handles Pong responses to our Pings automatically IF we poll the stream.
// But we are polling the stream in the select loop below.
// Note: Tungstenite returns Pongs as messages. We should filter them out mostly,
// or pass them if the user wants them?
// Usually heartbeat Pongs are an implementation detail.
let mut last_heartbeat = tokio::time::Instant::now();
let mut timeout_check = tokio::time::interval(config.timeout);
loop {
tokio::select! {
// 1. Receive message from socket
msg = receiver.next() => {
match msg {
Some(Ok(msg)) => {
last_heartbeat = tokio::time::Instant::now();
if msg.is_pong() {
// Received a pong (response to our ping)
continue;
}
if msg.is_ping() {
// Received a ping (from client)
// Tungstenite might have auto-replied if we used the right callback,
// but default poll_next does reply to pings by queueing a pong.
// We need to ensure that queued pong is sent.
// However, we are in a split stream.
// `receiver` is Stream. `sender` is Sink.
// Tungstenite's split separates them.
// The `receiver` will NOT automatically write to `sender`.
// WE must handle Ping replies if split?
// `tokio-tungstenite` docs: "You must handle Pings manually when split?"
// No, the `tungstenite` protocol handler is shared? No.
// If we receive a Ping, we should send a Pong.
let _ = sender.send(Message::Pong(msg.into_data()).into()).await;
continue;
}
// Forward other messages to user
if internal_tx.send(Ok(Message::from(msg))).await.is_err() {
break; // User dropped receiver
}
}
Some(Err(e)) => {
let _ = internal_tx.send(Err(WebSocketError::from(e))).await;
break;
}
None => break, // Connection closed
}
}
// 2. Receive message from user to send
msg = internal_rx.recv() => {
match msg {
Some(msg) => {
if sender.send(msg.into()).await.is_err() {
break; // Connection closed
}
}
None => break, // User dropped sender
}
}
// 3. Send Ping
_ = heartbeat_interval.tick() => {
if sender.send(Message::Ping(vec![]).into()).await.is_err() {
break;
}
}
// 4. Check timeout
_ = timeout_check.tick() => {
if last_heartbeat.elapsed() > config.interval + config.timeout {
// Timeout
break;
// This drops 'sender', closing the connection
}
}
}
}
// Loop break drops sender/receiver, closing connection
});
Self {
inner: StreamImpl::Managed {
tx: user_tx,
rx: user_rx,
},
}
}
/// Split the stream into sender and receiver halves
pub fn split(self) -> (WebSocketSender, WebSocketReceiver) {
match self.inner {
StreamImpl::Direct(inner) => {
let (sink, stream) = inner.split();
(
WebSocketSender {
inner: SenderImpl::Direct(sink),
},
WebSocketReceiver {
inner: ReceiverImpl::Direct(stream),
},
)
}
StreamImpl::Managed { tx, rx } => (
WebSocketSender {
inner: SenderImpl::Managed(tx),
},
WebSocketReceiver {
inner: ReceiverImpl::Managed(rx),
},
),
}
}
}
// Implement helper methods directly on WebSocketStream for convenience
impl WebSocketStream {
/// Send a message
pub async fn send(&mut self, msg: Message) -> Result<(), WebSocketError> {
match &mut self.inner {
StreamImpl::Direct(s) => s.send(msg.into()).await.map_err(WebSocketError::from),
StreamImpl::Managed { tx, .. } => tx
.send(msg)
.await
.map_err(|_| WebSocketError::ConnectionClosed),
}
}
/// Receive the next message
pub async fn recv(&mut self) -> Option<Result<Message, WebSocketError>> {
match &mut self.inner {
StreamImpl::Direct(s) => s
.next()
.await
.map(|r| r.map(Message::from).map_err(WebSocketError::from)),
StreamImpl::Managed { rx, .. } => rx.recv().await,
}
}
/// Send a text message
pub async fn send_text(&mut self, text: impl Into<String>) -> Result<(), WebSocketError> {
self.send(Message::text(text)).await
}
/// Send a binary message
pub async fn send_binary(&mut self, data: impl Into<Vec<u8>>) -> Result<(), WebSocketError> {
self.send(Message::binary(data)).await
}
/// Send a JSON message
pub async fn send_json<T: serde::Serialize>(
&mut self,
value: &T,
) -> Result<(), WebSocketError> {
self.send(Message::json(value)?).await
}
}
// Inner implementations for Sender/Receiver
enum SenderImpl {
Direct(SplitSink<UpgradedConnection, tungstenite::Message>),
Managed(mpsc::Sender<Message>),
}
/// Sender half of a WebSocket stream
pub struct WebSocketSender {
inner: SenderImpl,
}
impl WebSocketSender {
/// Send a message
pub async fn send(&mut self, msg: Message) -> Result<(), WebSocketError> {
match &mut self.inner {
SenderImpl::Direct(s) => s.send(msg.into()).await.map_err(WebSocketError::from),
SenderImpl::Managed(s) => s
.send(msg)
.await
.map_err(|_| WebSocketError::ConnectionClosed),
}
}
/// Send a text message
pub async fn send_text(&mut self, text: impl Into<String>) -> Result<(), WebSocketError> {
self.send(Message::text(text)).await
}
/// Send a binary message
pub async fn send_binary(&mut self, data: impl Into<Vec<u8>>) -> Result<(), WebSocketError> {
self.send(Message::binary(data)).await
}
/// Send a JSON message
pub async fn send_json<T: serde::Serialize>(
&mut self,
value: &T,
) -> Result<(), WebSocketError> {
self.send(Message::json(value)?).await
}
/// Close the sender
pub async fn close(mut self) -> Result<(), WebSocketError> {
match &mut self.inner {
SenderImpl::Direct(s) => s.close().await.map_err(WebSocketError::from),
SenderImpl::Managed(_) => {
// Drop sender to close channel, explicitly nothing else to do
Ok(())
}
}
}
}
enum ReceiverImpl {
Direct(SplitStream<UpgradedConnection>),
Managed(mpsc::Receiver<Result<Message, WebSocketError>>),
}
/// Receiver half of a WebSocket stream
pub struct WebSocketReceiver {
inner: ReceiverImpl,
}
impl WebSocketReceiver {
/// Receive the next message
pub async fn recv(&mut self) -> Option<Result<Message, WebSocketError>> {
match &mut self.inner {
ReceiverImpl::Direct(s) => s
.next()
.await
.map(|r| r.map(Message::from).map_err(WebSocketError::from)),
ReceiverImpl::Managed(s) => s.recv().await,
}
}
}
impl Stream for WebSocketReceiver {
type Item = Result<Message, WebSocketError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match &mut self.inner {
ReceiverImpl::Direct(s) => match Pin::new(s).poll_next(cx) {
Poll::Ready(Some(Ok(msg))) => Poll::Ready(Some(Ok(Message::from(msg)))),
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(WebSocketError::from(e)))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
},
ReceiverImpl::Managed(s) => s.poll_recv(cx),
}
}
}