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
//! Advanced WebSocket functionality with dual interaction patterns.
//!
//! This module provides a robust WebSocket client implementation with support for:
//!
//! - **Request-Response Pattern**: Send requests and await correlated responses with
//! automatic timeout handling
//! - **Subscription Pattern**: Subscribe to topics and receive updates via broadcast channels
//! - **Auto-Reconnection**: Exponential backoff with jitter for reliable connections
//! - **Lock-Free Concurrency**: High-performance stores using `scc::HashMap`
//! - **Protocol Abstraction**: Exchange-agnostic via the [`ProtocolHandler`] trait
//!
//! # Architecture
//!
//! The WebSocket system uses a single-owner connection loop with a lightweight driver:
//!
//! ```text
//! ┌──────────────────┐ Commands ┌────────────────────────┐
//! │ ConnectionHandle │───────────────▶ │ Connection Driver │
//! │ (Clone) │ │ (reconnect/backoff) │
//! └────────┬─────────┘ └─────────┬──────────────┘
//! │ Events │
//! ▼ ▼
//! ┌──────────────────┐ ┌──────────────────┐
//! │ ConnectionStream │◀──────────────────────│ Connection Task │
//! │ (Stream<Event>) │ │ (select! loop) │
//! └──────────────────┘ └───────┬──────────┘
//! ▼
//! PendingRequestStore / SubscriptionStore
//! ```
//!
//! # Quick Start
//!
//! ## Request-Response Example
//!
//! ```rust,ignore
//! use hpx_transport::websocket::{Connection, WsConfig};
//! use hpx_transport::websocket::handlers::GenericJsonHandler;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure the WebSocket connection
//! let config = WsConfig::new("wss://api.exchange.com/ws")
//! .ping_interval(std::time::Duration::from_secs(30))
//! .request_timeout(std::time::Duration::from_secs(10));
//!
//! // Create a protocol handler
//! let handler = GenericJsonHandler::new();
//!
//! // Connect and split into handle + stream
//! let connection = Connection::connect_stream(config, handler).await?;
//! let (handle, mut _stream) = connection.split();
//!
//! // Send a request and await response
//! let response: serde_json::Value = handle.request(&serde_json::json!({
//! "method": "getOrderBook",
//! "params": {"symbol": "BTCUSD"}
//! })).await?;
//!
//! println!("Response: {:?}", response);
//! Ok(())
//! }
//! ```
//!
//! ## Subscription Example
//!
//! ```rust,ignore
//! use hpx_transport::websocket::{Connection, WsConfig};
//! use hpx_transport::websocket::handlers::GenericJsonHandler;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = WsConfig::new("wss://api.exchange.com/ws");
//! let handler = GenericJsonHandler::new();
//! let connection = Connection::connect_stream(config, handler).await?;
//! let (handle, mut _stream) = connection.split();
//!
//! // Subscribe to a topic
//! let mut guard = handle.subscribe("orderbook.BTC").await?;
//!
//! // Receive updates
//! while let Some(msg) = guard.recv().await {
//! println!("Update: {:?}", msg);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! # Creating a Custom Protocol Handler
//!
//! Implement [`ProtocolHandler`] for your exchange's specific protocol:
//!
//! ```rust
//! use hpx_transport::websocket::{MessageKind, ProtocolHandler, RequestId, Topic, WsMessage};
//!
//! struct MyExchangeHandler;
//!
//! impl ProtocolHandler for MyExchangeHandler {
//! fn classify_message(&self, message: &str) -> MessageKind {
//! // Parse and classify the message
//! if message.contains("\"type\":\"response\"") {
//! MessageKind::Response
//! } else if message.contains("\"type\":\"update\"") {
//! MessageKind::Update
//! } else {
//! MessageKind::Unknown
//! }
//! }
//!
//! fn extract_request_id(&self, message: &str) -> Option<RequestId> {
//! // Extract ID from response
//! None // Simplified
//! }
//!
//! fn extract_topic(&self, message: &str) -> Option<Topic> {
//! // Extract topic from update
//! None // Simplified
//! }
//!
//! fn build_subscribe(&self, topics: &[Topic], request_id: RequestId) -> WsMessage {
//! WsMessage::text(format!(r#"{{"op":"subscribe","id":"{}"}}"#, request_id))
//! }
//!
//! fn build_unsubscribe(&self, topics: &[Topic], request_id: RequestId) -> WsMessage {
//! WsMessage::text(format!(r#"{{"op":"unsubscribe","id":"{}"}}"#, request_id))
//! }
//! }
//! ```
//!
//! # Configuration
//!
//! Use [`WsConfig`] to customize connection behavior:
//!
//! | Setting | Default | Description |
//! |---------|---------|-------------|
//! | `ping_interval` | 30s | Interval between heartbeat pings |
//! | `pong_timeout` | 10s | Max wait for pong response |
//! | `request_timeout` | 30s | Default request-response timeout |
//! | `reconnect_initial_delay` | 1s | Initial reconnection delay |
//! | `reconnect_max_delay` | 60s | Maximum reconnection delay |
//! | `max_pending_requests` | 1000 | Maximum concurrent pending requests |
//!
//! # Thread Safety
//!
//! All types in this module are designed for concurrent use:
//!
//! - [`ConnectionHandle`] is `Clone` and can be shared across tasks
//! - [`PendingRequestStore`] uses lock-free `scc::HashMap`
//! - [`SubscriptionStore`] uses lock-free `scc::HashMap`
//!
//! # Error Handling
//!
//! WebSocket operations return [`TransportResult<T>`](crate::error::TransportResult).
//! Common error cases:
//!
//! - [`TransportError::RequestTimeout`](crate::error::TransportError::RequestTimeout) - Request timed out
//! - [`TransportError::ConnectionClosed`](crate::error::TransportError::ConnectionClosed) - Connection lost
//! - [`TransportError::CapacityExceeded`](crate::error::TransportError::CapacityExceeded) - Too many pending requests
//!
//! # Module Structure
//!
//! - `config`: WebSocket connection configuration
//! - `types`: Core type definitions (RequestId, Topic, MessageKind)
//! - `protocol`: Protocol handler trait for exchange abstraction
//! - [`handlers`]: Ready-to-use protocol handler implementations
//! - `connection`: Single-task connection driver/task implementation
//! - `ws_client`: High-level client wrapper
//! - `pending`: Lock-free pending request management
//! - `subscription`: Lock-free subscription management
// Re-export config types
pub use WsConfig;
pub use ;
// Re-export state management stores
pub use PendingRequestStore;
// Re-export protocol types
pub use ;
pub use SubscriptionStore;
// Re-export core types
pub use ;
pub use WsClient;