pocket_relay_client_shared/servers/
mod.rs

1//! Different servers that the clients can spawn and manage
2
3use futures::Future;
4use parking_lot::Mutex;
5use tokio::task::AbortHandle;
6
7pub mod blaze;
8pub mod http;
9pub mod qos;
10pub mod redirector;
11pub mod telemetry;
12pub mod tunnel;
13pub mod udp_tunnel;
14
15/// The port the Redirector server will bind to
16pub const REDIRECTOR_PORT: u16 = 42127;
17/// The port the Blaze server will bind to
18pub const BLAZE_PORT: u16 = 42128;
19/// The port the Telemetry server will bind to
20pub const TELEMETRY_PORT: u16 = 42129;
21/// The port the Quality of Service server will bind to
22pub const QOS_PORT: u16 = 42130;
23/// The port the HTTP server will bind to
24pub const HTTP_PORT: u16 = 42131;
25/// The port used for the host socket
26pub const TUNNEL_HOST_PORT: u16 = 42132;
27/// Port that the OS may choose
28pub const RANDOM_PORT: u16 = 0;
29/// Port that the game itself is on
30pub const GAME_HOST_PORT: u16 = 3659;
31
32// Shared set of abort handles to server tasks
33static SERVER_TASK_COLLECTION: Mutex<Vec<AbortHandle>> = Mutex::new(Vec::new());
34
35/// Returns whether there are running server tasks
36pub fn has_server_tasks() -> bool {
37    let values = &mut *SERVER_TASK_COLLECTION.lock();
38    !values.is_empty()
39}
40
41/// Spawns a server related task future onto the tokio runtime and
42/// adds the abort handle for the task to the task collection
43///
44/// ## Arguments
45/// * `task` - The task future to spawn
46#[inline]
47pub fn spawn_server_task<F>(task: F)
48where
49    F: Future<Output = ()> + Send + 'static,
50{
51    let handle = tokio::spawn(task);
52    add_server_task(handle.abort_handle());
53}
54
55/// Append an abort handle to the server task collection
56///
57/// ## Arguments
58/// * `handle` - The abort handle for the task
59pub fn add_server_task(handle: AbortHandle) {
60    let values = &mut *SERVER_TASK_COLLECTION.lock();
61    values.push(handle);
62}
63
64/// Calls the abort handles for all tasks present
65/// in the task collection
66pub fn stop_server_tasks() {
67    // Take the current tasks list from the task collection
68    let values = {
69        let values = &mut *SERVER_TASK_COLLECTION.lock();
70        values.split_off(0)
71    };
72
73    // Call abort on each of the handles
74    values.into_iter().for_each(|value| value.abort());
75}