diff options
| author | Fuwn <[email protected]> | 2021-05-20 17:05:59 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-20 17:05:59 +0000 |
| commit | 9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8 (patch) | |
| tree | 8cfff6a23bb72db2660e68c63a8cf9d0539a061f /crates/whirl_server/src/lib.rs | |
| parent | feat(readme): add sqlfluff as a dev dep (diff) | |
| download | archived-whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.tar.xz archived-whirl-9dbc613765de8ab7dfa8e1374cf6661dcfd56bc8.zip | |
refactor(global): move crates around, stricter module isolation
Diffstat (limited to 'crates/whirl_server/src/lib.rs')
| -rw-r--r-- | crates/whirl_server/src/lib.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/crates/whirl_server/src/lib.rs b/crates/whirl_server/src/lib.rs new file mode 100644 index 0000000..9c3e9d0 --- /dev/null +++ b/crates/whirl_server/src/lib.rs @@ -0,0 +1,90 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +#![feature( + type_ascription, + hash_set_entry, + type_name_of_val, + decl_macro, + proc_macro_hygiene +)] +#![warn(rust_2018_idioms)] +#![recursion_limit = "128"] + +#[macro_use] +extern crate log; +#[macro_use] +extern crate async_trait; + +pub mod cmd; +mod interaction; +pub mod net; + +pub mod distributor; +pub mod hub; +mod packet_parser; +mod types; + +use std::{error::Error, fmt, net::SocketAddr, sync::Arc}; + +use tokio::{ + net::{TcpListener, TcpStream}, + sync::Mutex, +}; + +use crate::interaction::shared::Shared; + +#[derive(Debug)] +pub enum ServerType { + AnonRoomServer, + AnonUserServer, + AutoServer, + RoomServer, + UserServer, +} +// https://stackoverflow.com/a/32712140/14452787 +impl fmt::Display for ServerType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self) } +} + +#[async_trait] +pub trait Server { + async fn listen(address: &str, server_type: ServerType) -> Result<(), Box<dyn Error>> { + let listener = TcpListener::bind(address).await?; + let state = Arc::new(Mutex::new(Shared::new())); + let mut counter = 0; + + info!( + "server of type {} now listening at {}", + server_type.to_string(), + address + ); + + loop { + let (stream, address) = listener.accept().await?; + counter += 1; + let state = Arc::clone(&state); + + debug!("accepted client at {}", address); + + tokio::spawn(async move { + if let Err(e) = Self::handle(state, stream, address, counter).await { + error!("an error occurred: {}", e); + } + + if std::env::var("EXIT_ON_CLIENT_DISCONNECT").unwrap_or_else(|_| "false".to_string()) + == "true" + { + std::process::exit(0); + } + }); + } + } + + async fn handle( + state: Arc<Mutex<Shared>>, + stream: TcpStream, + _address: SocketAddr, + count: usize, + ) -> Result<(), Box<dyn Error>>; +} |