wtx 0.50.0

A collection of different transport implementations and related tools focused primarily on web technologies.
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#![expect(clippy::infinite_loop, reason = "expected behavior of a server")]

use crate::{
  collections::Vector,
  executor::{Executor, Runtime as _},
  http::Router,
  misc::{TcpParams, Uri},
  rng::{CryptoRng, CryptoSeedableRng},
  stream::{StreamReader, StreamWriter, TcpListener},
  sync::Arc,
  tls::{TlsAcceptor, TlsConfig, TlsMode},
  web_socket::{WebSocket, WebSocketAcceptor, WsCompression},
};
use alloc::string::String;
use core::num::NonZeroUsize;

type LocalWebSocket<CO, EX, TM> = WebSocket<
  <CO as WsCompression<false>>::NegotiatedCompression,
  <EX as Executor>::TcpStream,
  TM,
  false,
>;

/// WebSocket Server Framework
#[derive(Debug)]
pub struct WebSocketServerFramework<CO, EC, EX, RC, RNG, TM> {
  compression: CO,
  error_cb: EC,
  executor: EX,
  local_runtime_cb: RC,
  local_runtimes: Option<NonZeroUsize>,
  rng: RNG,
  tcp_params: TcpParams,
  tls_config: Arc<TlsConfig<TM>>,
}

impl<ER, EX, RNG, TM>
  WebSocketServerFramework<
    (),
    fn(ER),
    EX,
    fn() -> Result<<EX as Executor>::LocalRuntime, ER>,
    RNG,
    TM,
  >
where
  ER: From<crate::Error>,
  EX: Executor,
{
  /// Taking aside the provided parameters, everything else is set to default values.
  #[inline]
  pub fn new(executor: EX, rng: RNG, tls_config: TlsConfig<TM>) -> crate::Result<Self> {
    let error_cb: fn(_) = |_| {};
    let local_runtime_cb: fn() -> _ = || Ok(EX::LocalRuntime::new()?);
    Ok(Self {
      compression: (),
      error_cb,
      executor,
      local_runtime_cb,
      local_runtimes: None,
      rng,
      tcp_params: TcpParams::default(),
      tls_config: tls_config.into(),
    })
  }
}

#[cfg(feature = "tokio")]
impl<ER, TM>
  WebSocketServerFramework<
    (),
    fn(ER),
    crate::executor::TokioExecutor,
    fn() -> Result<<crate::executor::TokioExecutor as Executor>::LocalRuntime, ER>,
    crate::rng::ChaCha20,
    TM,
  >
where
  ER: From<crate::Error>,
{
  /// Calls [`Self::new`] using the elements provided by the tokio project
  #[inline]
  pub fn tokio(tls_config: TlsConfig<TM>) -> crate::Result<Self> {
    Self::new(
      crate::executor::TokioExecutor::default(),
      crate::rng::ChaCha20::from_std_random()?,
      tls_config,
    )
  }
}

impl<CO, EC, EX, RC, RNG, TM> WebSocketServerFramework<CO, EC, EX, RC, RNG, TM> {
  /// Sets the compression algorithm.
  #[inline]
  pub fn set_compression<_C>(self, value: _C) -> WebSocketServerFramework<_C, EC, EX, RC, RNG, TM> {
    WebSocketServerFramework {
      compression: value,
      error_cb: self.error_cb,
      executor: self.executor,
      local_runtime_cb: self.local_runtime_cb,
      local_runtimes: self.local_runtimes,
      rng: self.rng,
      tcp_params: self.tcp_params,
      tls_config: self.tls_config,
    }
  }

  /// Sets the error callback function.
  #[inline]
  pub fn set_error_cb<_EC>(self, value: _EC) -> WebSocketServerFramework<CO, _EC, EX, RC, RNG, TM> {
    WebSocketServerFramework {
      compression: self.compression,
      error_cb: value,
      executor: self.executor,
      local_runtime_cb: self.local_runtime_cb,
      local_runtimes: self.local_runtimes,
      rng: self.rng,
      tcp_params: self.tcp_params,
      tls_config: self.tls_config,
    }
  }

  /// Allows the tweaking of the chosen runtime.
  ///
  /// Only works when calling [`Self::run_in_threads`].
  #[inline]
  pub fn set_local_runtime_cb<_RC>(
    self,
    value: _RC,
  ) -> WebSocketServerFramework<CO, EC, EX, _RC, RNG, TM> {
    WebSocketServerFramework {
      compression: self.compression,
      error_cb: self.error_cb,
      executor: self.executor,
      local_runtime_cb: value,
      local_runtimes: self.local_runtimes,
      rng: self.rng,
      tcp_params: self.tcp_params,
      tls_config: self.tls_config,
    }
  }

  /// The number of spawned runtimes. Shouldn't be greater than the number of available threads.
  ///
  /// Only works when calling [`Self::run_in_threads`]. If [`None`], defaults to the number of threads.
  #[inline]
  #[must_use]
  pub fn set_local_runtimes(mut self, value: Option<NonZeroUsize>) -> Self {
    self.local_runtimes = value;
    self
  }

  /// See [`TcpParams`].
  #[inline]
  #[must_use]
  pub fn set_tcp_params(mut self, value: TcpParams) -> Self {
    self.tcp_params = value;
    self
  }

  /// See [`TlsConfig`].
  #[inline]
  pub fn set_tls_config<_TM>(
    self,
    value: TlsConfig<_TM>,
  ) -> WebSocketServerFramework<CO, EC, EX, RC, RNG, _TM> {
    WebSocketServerFramework {
      compression: self.compression,
      error_cb: self.error_cb,
      executor: self.executor,
      local_runtime_cb: self.local_runtime_cb,
      local_runtimes: self.local_runtimes,
      rng: self.rng,
      tcp_params: self.tcp_params,
      tls_config: Arc::new(value),
    }
  }
}

impl<CO, EC, ER, EX, RC, RNG, TM> WebSocketServerFramework<CO, EC, EX, RC, RNG, TM>
where
  CO: Clone + WsCompression<false> + 'static,
  EC: Clone + Fn(ER) + 'static,
  ER: From<crate::Error> + 'static,
  EX: Clone + Executor + 'static,
  EX::TcpListener: 'static,
  EX::TcpStream: 'static,
  RC: Clone + Fn() -> Result<EX::LocalRuntime, ER> + 'static,
  RNG: CryptoRng + CryptoSeedableRng + 'static,
  TM: TlsMode + 'static,
{
  /// Starts the server distributing connections across multiple tasks.
  ///
  /// You must call this method from within an existing async environment. Preferably, a
  /// multi-thread environment.
  #[cfg(feature = "nightly")]
  #[inline]
  pub async fn run<WSR>(mut self, addr: &str, wsr: WSR) -> Result<(), ER>
  where
    CO: Send,
    CO::NegotiatedCompression: Send + Sync,
    EC: Send,
    ER: Send,
    EX: Send,
    EX::TcpListener: Send,
    EX::TcpStream: Send,
    RC: Send,
    RNG: Send,
    TM: Send + Sync,
    WSR: WebSocketRouter<CO, ER, EX, TM> + Send + Sync + 'static,
    WSR::call(..): Send,
    <EX::TcpListener as TcpListener>::accept(..): Send,
    <EX::TcpStream as StreamReader>::read(..): Send,
    <EX::TcpStream as StreamWriter>::write_all(..): Send,
    <EX::TcpStream as StreamWriter>::write_all_vectored(..): Send,
  {
    let uri = Uri::new(addr);
    let web_socket_router = Arc::new(wsr);
    let listener = EX::TcpListener::bind(uri.hostname_with_implied_port(), self.tcp_params).await?;
    let router = build_matcher(&*web_socket_router)?;
    loop {
      let Ok(cp) = conn_params::<CO, EC, EX, RNG, TM, WSR>(
        (&self.compression, &self.error_cb),
        (&mut self.rng, self.tcp_params, &self.tls_config),
        &listener,
        &router,
        &web_socket_router,
      )
      .await
      else {
        continue;
      };
      let _jh = self.executor.spawn(conn_fut(cp));
    }
  }

  /// Starts the server using a runtime-per-thread architecture.
  ///
  /// Contrary to the other methods, this method internally creates a runtime according to the
  /// specified `local_runtimes` value.
  ///
  /// You must call this method inside the main thread to allow the interruption of the remaining
  /// threads once an error arises.
  #[cfg(feature = "std")]
  #[inline]
  pub fn run_in_threads<WSR>(mut self, addr: &str, wsr: WSR) -> Result<(), ER>
  where
    CO: Send,
    EC: Send,
    ER: Send,
    EX: Send,
    RC: Send,
    RNG: Send,
    TM: Send + Sync,
    WSR: WebSocketRouter<CO, ER, EX, TM> + Send + Sync + 'static,
  {
    let runtimes = if let Some(elem) = self.local_runtimes {
      elem.get()
    } else {
      std::thread::available_parallelism().map_err(crate::Error::from)?.get()
    };
    let web_socket_router = Arc::new(wsr);
    let router = build_matcher(&*web_socket_router)?;
    let mut join_handles = Vector::<std::thread::JoinHandle<Result<(), ER>>>::new();
    for _ in 0..runtimes {
      let thread_comp = self.compression.clone();
      let thread_error_cb = self.error_cb.clone();
      let thread_executor = self.executor.clone();
      let thread_local_runtime_cb = self.local_runtime_cb.clone();
      let mut thread_rng = RNG::from_crypto_rng(&mut self.rng)?;
      let thread_router = router.clone();
      let thread_tcp_params = self.tcp_params;
      let thread_tls_config = self.tls_config.clone();
      let thread_uri = Uri::new(String::from(addr));
      let thread_web_socket_router = web_socket_router.clone();
      join_handles.push(std::thread::spawn(move || {
        thread_local_runtime_cb()?.block_on(async move {
          let hostname = thread_uri.hostname_with_implied_port();
          let listener = EX::TcpListener::bind(hostname, thread_tcp_params).await?;
          loop {
            let Ok(cp) = conn_params::<CO, EC, EX, RNG, TM, WSR>(
              (&thread_comp, &thread_error_cb),
              (&mut thread_rng, thread_tcp_params, &thread_tls_config),
              &listener,
              &thread_router,
              &thread_web_socket_router,
            )
            .await
            else {
              continue;
            };
            let _jh = thread_executor.spawn_local(conn_fut(cp));
          }
        })
      }))?;
    }
    for join_handle in join_handles {
      join_handle.join().map_err(crate::Error::from)??;
    }
    Ok(())
  }

  /// Starts the server handling all connections on the current thread.
  ///
  /// You must call this method from within an existing async environment.
  #[inline]
  pub async fn run_local<WSR>(mut self, addr: &str, wsr: WSR) -> Result<(), ER>
  where
    WSR: WebSocketRouter<CO, ER, EX, TM> + 'static,
  {
    let web_socket_router = Arc::new(wsr);
    let router = build_matcher(&*web_socket_router)?;
    let uri = Uri::new(addr);
    let listener = EX::TcpListener::bind(uri.hostname_with_implied_port(), self.tcp_params).await?;
    loop {
      let Ok(cp) = conn_params::<CO, EC, EX, RNG, TM, WSR>(
        (&self.compression, &self.error_cb),
        (&mut self.rng, self.tcp_params, &self.tls_config),
        &listener,
        &router,
        &web_socket_router,
      )
      .await
      else {
        continue;
      };
      let _jh = self.executor.spawn_local(conn_fut(cp));
    }
  }
}

/// Routes path according to the set of user-provided functions and paths.
pub trait WebSocketRouter<CO, ER, EX, TM>
where
  CO: WsCompression<false>,
  EX: Executor,
{
  /// Calls user-provided functions.
  fn call(
    &self,
    matcher: &Router<u8>,
    path: String,
    ws: LocalWebSocket<CO, EX, TM>,
  ) -> impl Future<Output = Result<(), ER>>;

  /// All user registered paths
  fn paths(&self) -> impl ExactSizeIterator<Item = &'static str>;
}

struct ConnParams<CO, EC, EX, RNG, TM, WSR>
where
  EX: Executor,
{
  compression: CO,
  error_cb: EC,
  rng: RNG,
  router: Arc<Router<u8>>,
  stream: EX::TcpStream,
  tls_config: Arc<TlsConfig<TM>>,
  web_socket_router: Arc<WSR>,
}

fn build_matcher<CO, ER, EX, TM, WSR>(web_socket_router: &WSR) -> crate::Result<Arc<Router<u8>>>
where
  CO: WsCompression<false>,
  EX: Executor,
  WSR: WebSocketRouter<CO, ER, EX, TM>,
{
  let mut matcher = Router::new();
  {
    let mut builder = matcher.builder();
    let mut idx: u8 = 0;
    for path in web_socket_router.paths() {
      let _ = builder.add(&path.try_into()?, idx)?;
      idx = idx.wrapping_add(1);
    }
  }
  Ok(Arc::new(matcher))
}

#[inline]
async fn conn_fut<CO, EC, ER, EX, RNG, TM, WSR>(conn_params: ConnParams<CO, EC, EX, RNG, TM, WSR>)
where
  CO: WsCompression<false>,
  EC: Fn(ER),
  ER: From<crate::Error>,
  EX: Executor,
  RNG: CryptoRng + CryptoSeedableRng,
  TM: TlsMode,
  WSR: WebSocketRouter<CO, ER, EX, TM>,
{
  let fun = async {
    let mut path = String::new();
    let web_socket = WebSocketAcceptor::default()
      .set_compression(conn_params.compression)
      .set_req(|req| {
        if let Some(elem) = req.path {
          path.push_str(elem);
        }
        crate::Result::Ok(true)
      })
      .accept(TlsAcceptor::new(&*conn_params.tls_config, conn_params.rng, conn_params.stream))
      .await?;
    conn_params.web_socket_router.call(&conn_params.router, path, web_socket).await?;
    Ok::<_, ER>(())
  };
  if let Err(err) = fun.await {
    (conn_params.error_cb)(err);
  }
}

async fn conn_params<CO, EC, EX, RNG, TM, WSR>(
  (compression, error_cb): (&CO, &EC),
  (rng, tcp_params, tls_config): (&mut RNG, TcpParams, &Arc<TlsConfig<TM>>),
  listener: &EX::TcpListener,
  router: &Arc<Router<u8>>,
  web_socket_router: &Arc<WSR>,
) -> crate::Result<ConnParams<CO, EC, EX, RNG, TM, WSR>>
where
  CO: Clone,
  EC: Clone,
  EX: Executor,
  RNG: CryptoRng + CryptoSeedableRng,
{
  Ok(ConnParams {
    compression: compression.clone(),
    error_cb: error_cb.clone(),
    rng: RNG::from_crypto_rng(rng)?,
    router: router.clone(),
    stream: listener.accept(tcp_params).await?.0,
    tls_config: tls_config.clone(),
    web_socket_router: web_socket_router.clone(),
  })
}