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
#![allow(dead_code)]
#![allow(unused_imports)]
use crate::builder::Builder;
use crate::endpoints::Endpoints;
use crate::websocket::{ConnectionContext, WebSocketHandler, WebSocketMessage, WebSocketSession};
use std::collections::HashMap;
use async_tungstenite::{
tokio::accept_hdr_async_with_config,
tungstenite::{http::Request, Error},
WebSocketStream,
};
use async_tungstenite::tokio::TokioAdapter;
use async_tungstenite::tungstenite::http::{Response, StatusCode};
use async_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
use async_tungstenite::tungstenite::protocol::frame::coding::CloseCode::Status;
use async_tungstenite::tungstenite::protocol::CloseFrame;
use futures::future::BoxFuture;
use futures::StreamExt;
use futures::{SinkExt, Stream, TryFutureExt};
use std::borrow::Borrow;
use std::future::Future;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::AsyncRead;
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
use tokio::sync::{mpsc, RwLock};
use tracing_futures::Instrument;
use uuid::Uuid;
pub mod builder;
pub mod endpoints;
pub mod websocket;
pub type WebSocketSessions = Arc<RwLock<HashMap<Uuid, WebSocketSession>>>;
#[derive(Clone, Debug)]
pub struct Server {
endpoints: Endpoints,
pub sessions: WebSocketSessions,
}
impl Server {
pub(crate) fn new(endpoints: Endpoints) -> Self {
Self {
endpoints,
sessions: WebSocketSessions::default(),
}
}
async fn handle_new_session(
&mut self,
socket: WebSocketStream<TokioAdapter<TcpStream>>,
ctx: ConnectionContext,
) {
let (mut ws_session_tx, mut ws_session_rx) = socket.split();
let (tx, mut rx) = mpsc::unbounded_channel::<WebSocketMessage>();
let (server_tx, server_rx) = mpsc::unbounded_channel::<(Uuid, WebSocketMessage)>();
let ws_server2 = self.clone();
websocket::register_recv_ws_message_handling(ws_server2, server_rx).await;
tokio::spawn(async move {
while let Some(result) = rx.recv().await {
tracing::trace!("Sending message to websocket connection: {:?}", result);
ws_session_tx.send(result).await.unwrap();
}
});
let session = WebSocketSession::new(ctx, tx);
let session_id = session.id();
self.add_session(session).await;
let server_tx2 = server_tx.clone();
let f = async move {
while let Some(result) = ws_session_rx.next().await {
match result {
Ok(msg) => {
match server_tx2.send((session_id, msg)) {
Ok(_) => {}
Err(e) => {
tracing::error!("ERROR SENDING TO server_tx2 channel: {}", e)
}
};
}
Err(e) => {
tracing::error!("ws_session_rx.next() error: {}", e);
return;
}
}
}
tracing::trace!("we are leaving the gibson - channel dropped");
};
tokio::task::spawn(f);
}
async fn handle_connection(
&mut self,
peer: SocketAddr,
stream: TcpStream,
) -> async_tungstenite::tungstenite::Result<()> {
type HttpResponse = async_tungstenite::tungstenite::http::Response<()>;
let (tx, rx) = tokio::sync::oneshot::channel();
let callback = move |request: &Request<()>, mut response: HttpResponse| {
let header_options = request.headers().clone();
let path = request.uri().path().to_owned();
let query = match request.uri().query() {
Some(s) => Some(s.to_string()),
_ => Some("".to_string()),
};
if tx.send((header_options, path, query)).is_err() {
tracing::error!("Unable to upgrade connection");
let status = response.status_mut();
*status = StatusCode::from_u16(500).unwrap();
}
Ok(response)
};
let mut ws_stream = accept_hdr_async_with_config(stream, callback, None)
.await
.expect("failed");
let conn_data = match rx.await {
Ok(d) => d,
_ => panic!("Unable to receive connection context for upgrade"),
};
if !self.endpoints.contains_path(&conn_data.1).await {
let err_message = format!(
"Unable to find Web socket handler for path: {}",
conn_data.1
);
tracing::trace!("{}", err_message);
let frame = CloseFrame {
code: CloseCode::Policy,
reason: Default::default(),
};
let _ = ws_stream.close(Some(frame)).await;
return Err(Error::Http(
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(None)
.unwrap(),
));
}
let ctx = ConnectionContext::new(
Some(peer),
conn_data.0,
conn_data.2.unwrap(),
conn_data.1.to_string(),
);
self.handle_new_session(ws_stream, ctx).await;
Ok(())
}
async fn accept_connection(&mut self, peer: SocketAddr, stream: TcpStream) {
if let Err(e) = self.handle_connection(peer, stream).await {
match e {
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
err => tracing::error!("Error processing connection: {}", err),
}
}
}
pub async fn bind(&mut self, addrs: impl ToSocketAddrs) -> std::io::Result<()> {
let listener = TcpListener::bind(addrs).await?;
while let Ok((stream, _sock_addr)) = listener.accept().await {
match stream.peer_addr() {
Ok(addr) => {
tracing::info!("connected stream's peer address: {}", addr);
self.accept_connection(addr, stream).await
}
Err(_) => {
tracing::warn!("connected streams should have a peer address");
}
};
}
Ok(())
}
#[tracing::instrument(level = "trace")]
async fn add_session(&mut self, session: WebSocketSession) {
tracing::trace!("adding session: {:?}", session.id());
let session2 = session.clone();
let len: usize = {
let mut lock = self.sessions.write().await;
lock.insert(session.id(), session);
lock.len()
};
tracing::trace!("total sessions: {}", len);
self.trigger_on_open(&session2).await;
}
#[tracing::instrument(level = "trace")]
async fn remove_session(&self, session_id: Uuid) {
let s = self.sessions.write().await.remove(session_id.borrow());
drop(s);
tracing::debug!(
"Current total active sessions: {}",
self.sessions.read().await.len()
);
}
#[tracing::instrument(level = "trace")]
pub async fn trigger_on_open(&mut self, session: &WebSocketSession) {
self.endpoints.on_open(session).await;
}
#[tracing::instrument(level = "trace")]
pub async fn recv(&mut self, session_id: Uuid, message: WebSocketMessage) {
let session = {
let lock = self.sessions.read().await;
match lock.get(session_id.borrow()) {
Some(v) => v.clone(),
None => return,
}
};
if message.is_close() {
tracing::trace!(
"received message {:?} from session id: {:?}",
message,
session_id
);
self.endpoints.on_close(&session, message).await;
self.remove_session(session_id).await;
} else if message.is_text() || message.is_binary() {
tracing::trace!(
"received message {:?} from session id: {:?}",
message,
session_id
);
self.endpoints.on_message(&session, message).await;
} else if message.is_ping() || message.is_pong() {
tracing::trace!(
"received message {:?} from session id: {:?}",
message,
session_id
);
} else {
tracing::error!("What kind of message is that?!");
unimplemented!("What kind of message is that?!");
}
}
}
pub fn builder() -> Builder {
builder::Builder::new()
}