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
use std::collections::HashMap;
use std::net::TcpStream;
use std::thread::{self, JoinHandle};
use crossbeam_channel::{unbounded, Receiver, Sender};
use tungstenite::stream::MaybeTlsStream;
use tungstenite::{Message, WebSocket};
use crate::error::{S9Result, S9WebSocketError};
use super::options::NonBlockingOptions;
use super::types::{WebSocketEvent, ControlMessage};
use super::types::{send_or_break, send_or_log};
use super::shared;
// ============================================================================
// S9AsyncNonBlockingWebSocketClient - Async client with channels
// ============================================================================
pub struct S9AsyncNonBlockingWebSocketClient {
socket: Option<WebSocket<MaybeTlsStream<TcpStream>>>,
options: NonBlockingOptions,
pub control_tx: Sender<ControlMessage>,
control_rx: Receiver<ControlMessage>,
event_tx: Sender<WebSocketEvent>,
pub event_rx: Receiver<WebSocketEvent>,
}
impl S9AsyncNonBlockingWebSocketClient {
/// Connects to a WebSocket server and prepares for async operation.
///
/// Creates a client ready to spawn a background thread via `run()`.
/// The connection supports both `ws://` and `wss://` protocols.
pub fn connect(uri: &str, options: NonBlockingOptions)-> S9Result<S9AsyncNonBlockingWebSocketClient> {
Self::connect_with_headers(uri, &HashMap::new(), options)
}
/// Connects to a WebSocket server with custom HTTP headers.
///
/// Allows setting custom headers (e.g., Authorization) during the WebSocket handshake.
pub fn connect_with_headers(uri: &str, headers: &HashMap<String, String>, options: NonBlockingOptions) -> S9Result<S9AsyncNonBlockingWebSocketClient> {
let (mut socket, _response) = shared::connect_socket(uri, headers)?;
shared::configure_non_blocking(&mut socket, &options)?;
let (control_tx, control_rx) = unbounded::<ControlMessage>();
let (event_tx, event_rx) = unbounded::<WebSocketEvent>();
Ok(S9AsyncNonBlockingWebSocketClient {
socket: Some(socket),
options,
control_tx,
control_rx,
event_tx,
event_rx
})
}
/// Returns a reference to the underlying WebSocket if it hasn't been moved to the event loop thread yet.
///
/// This provides low-level access to the tungstenite WebSocket for advanced use cases.
/// Note: This will return `None` after `run()` has been called, as the socket is moved to the event loop thread.
/// Use with caution as direct manipulation may interfere with the client's operation.
#[inline]
pub fn get_socket(&self) -> Option<&WebSocket<MaybeTlsStream<TcpStream>>> {
self.socket.as_ref()
}
/// Returns a mutable reference to the underlying WebSocket if it hasn't been moved to the event loop thread yet.
///
/// This provides low-level access to the tungstenite WebSocket for advanced use cases.
/// Note: This will return `None` after `run()` has been called, as the socket is moved to the event loop thread.
/// Use with caution as direct manipulation may interfere with the client's operation.
#[inline]
pub fn get_socket_mut(&mut self) -> Option<&mut WebSocket<MaybeTlsStream<TcpStream>>> {
self.socket.as_mut()
}
/// Spawns the background thread and starts processing WebSocket events.
///
/// Returns immediately with a `JoinHandle`. Send commands via `control_tx` and receive events via `event_rx`.
/// The socket is moved to the background thread and becomes unavailable for direct access.
#[inline]
pub fn run(&mut self) -> S9Result<JoinHandle<()>> {
// Take ownership of the socket to put it into the tread by replacing it with a dummy value
// This is safe because we'll never use the original socket again after spawning
let socket = self.socket.take();
let mut socket = match socket {
Some(s) => s,
None => {
tracing::error!("Socket just consumed");
return Err(S9WebSocketError::SocketUnavailable.into());
},
};
let control_rx = self.control_rx.clone();
let event_tx = self.event_tx.clone();
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Starting non-blocking event loop thread...");
}
let spin_wait_duration = self.options.shared.spin_wait_duration.clone();
let join_handle = thread::spawn(move || {
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Starting event loop");
}
// Send Activate event before entering the main loop
send_or_log!(event_tx, "WebSocketEvent::Activated", WebSocketEvent::Activated);
loop {
// 1. Check for control messages (non-blocking)
if let Ok(control_msg) = control_rx.try_recv() {
match shared::handle_control_message(control_msg, &mut socket) {
Ok(shared::ControlFlow::Continue) => {},
Ok(shared::ControlFlow::Break) => {
send_or_log!(event_tx, "WebSocketEvent::Quit on ControlMessage::ForceQuit", WebSocketEvent::Quit);
break;
},
Err(error) => {
send_or_break!(event_tx, "WebSocketEvent::Error on ControlMessage", WebSocketEvent::Error(error));
}
}
}
// 2. Try to read from socket (non-blocking)
match socket.read() {
Ok(msg) => {
match msg {
Message::Text(message) => {
shared::trace_on_text_message(&message);
send_or_break!(event_tx, "WebSocketEvent::TextMessage on Message::Text", WebSocketEvent::TextMessage(message.as_bytes().to_vec()));
},
Message::Binary(bytes) => {
shared::trace_on_binary_message(&bytes);
send_or_break!(event_tx, "WebSocketEvent::BinaryMessage on Message::Binary", WebSocketEvent::BinaryMessage(bytes.to_vec()));
},
Message::Ping(bytes) => {
shared::trace_on_ping_message(&bytes);
send_or_break!(event_tx, "WebSocketEvent::Ping on Message::Ping", WebSocketEvent::Ping(bytes.to_vec()));
},
Message::Pong(bytes) => {
shared::trace_on_pong_message(&bytes);
send_or_break!(event_tx, "WebSocketEvent::Pong on Message::Pong", WebSocketEvent::Pong(bytes.to_vec()));
},
Message::Close(close_frame) => {
shared::trace_on_close_frame(&close_frame);
let reason = close_frame.map(|cf| cf.to_string());
send_or_log!(event_tx, "WebSocketEvent::ConnectionClosed on Message::Close", WebSocketEvent::ConnectionClosed(reason));
send_or_log!(event_tx, "WebSocketEvent::Quit on Message::Close", WebSocketEvent::Quit);
break;
},
Message::Frame(_) => {
shared::trace_on_frame();
// No handling for frames until use case needs it
}
}
},
Err(error) => {
let (reason, should_break) = shared::handle_read_error(error);
if let Some(error_msg) = reason {
if should_break {
let (context, event) = {
if shared::is_connection_closed_error(&error_msg) {
("WebSocketEvent::ConnectionClosed on Error::ConnectionClosed", WebSocketEvent::ConnectionClosed(Some(error_msg)))
} else {
("WebSocketEvent::Error", WebSocketEvent::Error(error_msg))
}
};
send_or_log!(event_tx, context, event);
send_or_break!(event_tx, "WebSocketEvent::Quit", WebSocketEvent::Quit);
break;
}
}
}
};
// Optionally sleep to reduce CPU usage
if let Some(duration) = spin_wait_duration {
thread::sleep(duration);
}
}
});
Ok(join_handle)
}
}
impl Drop for S9AsyncNonBlockingWebSocketClient {
fn drop(&mut self) {
if let Some(socket) = &mut self.socket {
shared::close_websocket_with_logging(socket, "on Drop");
}
}
}