From 5dab336049dbc6817e9ff212998690f59f6bbfa8 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Thu, 11 Feb 2021 20:29:42 +0100 Subject: rstnode: add network packet envelope to wire types --- games/rstnode/Cargo.lock | 3 +++ games/rstnode/rst-core/src/wire/env.rs | 20 ++++++++++++++ games/rstnode/rst-core/src/wire/mod.rs | 13 +++++---- games/rstnode/rst-server/Cargo.toml | 5 +++- games/rstnode/rst-server/src/constants.rs | 5 ++++ games/rstnode/rst-server/src/log.rs | 39 +++++++++++++++++++++++++++ games/rstnode/rst-server/src/main.rs | 16 +++++++++-- games/rstnode/rst-server/src/net/mod.rs | 43 ++++++++++++++++++++++++++++++ games/rstnode/rst-server/src/net/parser.rs | 9 +++++++ 9 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 games/rstnode/rst-core/src/wire/env.rs create mode 100644 games/rstnode/rst-server/src/constants.rs create mode 100644 games/rstnode/rst-server/src/log.rs create mode 100644 games/rstnode/rst-server/src/net/mod.rs create mode 100644 games/rstnode/rst-server/src/net/parser.rs diff --git a/games/rstnode/Cargo.lock b/games/rstnode/Cargo.lock index 81e017cbd207..a7f7a83b6123 100644 --- a/games/rstnode/Cargo.lock +++ b/games/rstnode/Cargo.lock @@ -3676,7 +3676,10 @@ dependencies = [ name = "rst-node-server" version = "0.0.0" dependencies = [ + "async-std", "rst-core", + "tracing", + "tracing-subscriber", ] [[package]] diff --git a/games/rstnode/rst-core/src/wire/env.rs b/games/rstnode/rst-core/src/wire/env.rs new file mode 100644 index 000000000000..fd5ed690453e --- /dev/null +++ b/games/rstnode/rst-core/src/wire/env.rs @@ -0,0 +1,20 @@ +use serde::{de::DeserializeOwned, Deserialize, Serialize}; + +#[repr(C)] +#[derive(Serialize, Deserialize)] +pub struct Envelope { + size: u64, + data: Vec, +} + +impl Envelope { + pub fn pack(d: T) -> Self { + let data = bincode::serialize(&d).unwrap(); + let size = data.len() as u64; + Self { size, data } + } + + pub fn unpack(self) -> T { + bincode::deserialize(&self.data).unwrap() + } +} diff --git a/games/rstnode/rst-core/src/wire/mod.rs b/games/rstnode/rst-core/src/wire/mod.rs index cd311383a83a..493f0bcb6885 100644 --- a/games/rstnode/rst-core/src/wire/mod.rs +++ b/games/rstnode/rst-core/src/wire/mod.rs @@ -3,20 +3,19 @@ mod action; pub use action::*; -mod req; -pub use req::*; +mod env; +pub use env::*; mod resp; pub use resp::*; +mod req; +pub use req::*; + mod update; pub use update::*; -use crate::{ - data::{Color, Player}, - map::Map, - Id, -}; +use crate::{data::Color, Id}; use serde::{Deserialize, Serialize}; /// An alias for a User's ID diff --git a/games/rstnode/rst-server/Cargo.toml b/games/rstnode/rst-server/Cargo.toml index 865caf9fab15..bf175b81325d 100644 --- a/games/rstnode/rst-server/Cargo.toml +++ b/games/rstnode/rst-server/Cargo.toml @@ -7,4 +7,7 @@ license = "AGPL-3.0-or-later" authors = ["Bread Machine", "Katharina Fey "] [dependencies] -rst-core = { path = "../rst-core" } \ No newline at end of file +rst-core = { path = "../rst-core" } +async-std = { version = "1.0", features = ["attributes"] } +tracing = "0.1" +tracing-subscriber = "0.2" \ No newline at end of file diff --git a/games/rstnode/rst-server/src/constants.rs b/games/rstnode/rst-server/src/constants.rs new file mode 100644 index 000000000000..e40a5c35c5db --- /dev/null +++ b/games/rstnode/rst-server/src/constants.rs @@ -0,0 +1,5 @@ + + +pub const NAME: &'static str = "RST Node"; +pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +pub const AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS"); diff --git a/games/rstnode/rst-server/src/log.rs b/games/rstnode/rst-server/src/log.rs new file mode 100644 index 000000000000..62d03167bb7a --- /dev/null +++ b/games/rstnode/rst-server/src/log.rs @@ -0,0 +1,39 @@ +//! Logging specifics + +const BANNER: &'static str = " +██████╗ ███████╗████████╗ ███╗ ██╗ ██████╗ ██████╗ ███████╗ +██╔══██╗██╔════╝╚══██╔══╝ ████╗ ██║██╔═══██╗██╔══██╗██╔════╝ +██████╔╝███████╗ ██║ ██╔██╗ ██║██║ ██║██║ ██║█████╗ +██╔══██╗╚════██║ ██║ ██║╚██╗██║██║ ██║██║ ██║██╔══╝ +██║ ██║███████║ ██║ ██║ ╚████║╚██████╔╝██████╔╝███████╗ +╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝"; + +use tracing_subscriber::{filter::LevelFilter, fmt, EnvFilter}; + +pub(crate) fn initialise() { + let filter = EnvFilter::try_from_env("RST_LOG") + .unwrap_or_default() + .add_directive(LevelFilter::DEBUG.into()) + .add_directive("async_std=error".parse().unwrap()) + .add_directive("mio=error".parse().unwrap()); + + // Initialise the logger + fmt().with_env_filter(filter).init(); + info!("Initialising server..."); + info!("{}", BANNER); + info!("Available cores: unknown"); + info!("Available RAM: unknown"); + info!("Version: {}", crate::constants::VERSION); +} + +#[macro_export] +macro_rules! fatal { + () => { + error!("Unknown failure!"); + std::process::exit(2) + }; + ($($arg:tt)*) => ({ + error!($($arg)*); + std::process::exit(2) + }) +} diff --git a/games/rstnode/rst-server/src/main.rs b/games/rstnode/rst-server/src/main.rs index e7a11a969c03..d3335e941ca8 100644 --- a/games/rstnode/rst-server/src/main.rs +++ b/games/rstnode/rst-server/src/main.rs @@ -1,3 +1,15 @@ -fn main() { - println!("Hello, world!"); +#[macro_use] +extern crate tracing; + +mod constants; +mod log; +mod net; +use net::ServerEndpoint; + +#[async_std::main] +async fn main() { + log::initialise(); + + let serv = ServerEndpoint::new("0.0.0.0:10022").await; + serv.listen().await; } diff --git a/games/rstnode/rst-server/src/net/mod.rs b/games/rstnode/rst-server/src/net/mod.rs new file mode 100644 index 000000000000..deb35f7f89b9 --- /dev/null +++ b/games/rstnode/rst-server/src/net/mod.rs @@ -0,0 +1,43 @@ +#![allow(unused)] + +mod parser; + +use async_std::{ + net::UdpSocket, + sync::{Arc, RwLock}, + task, +}; +use rst_core::Id; +use std::{collections::BTreeMap, net::SocketAddr}; + +pub struct ServerEndpoint { + socket: UdpSocket, + bind: String, + clients: RwLock>, +} + +impl ServerEndpoint { + pub async fn new(bind: &str) -> Arc { + let socket = UdpSocket::bind(bind).await.unwrap(); + Arc::new(Self { + socket, + bind: bind.into(), + clients: Default::default(), + }) + } + + pub async fn listen(self: &Arc) { + let mut buf = vec![0; 1024]; + + info!("Listening for connections on {}", self.bind); + + loop { + let (_, peer) = self.socket.recv_from(&mut buf).await.unwrap(); + } + } +} + +pub struct Client { + addr: SocketAddr, +} + diff --git a/games/rstnode/rst-server/src/net/parser.rs b/games/rstnode/rst-server/src/net/parser.rs new file mode 100644 index 000000000000..d320e13095bb --- /dev/null +++ b/games/rstnode/rst-server/src/net/parser.rs @@ -0,0 +1,9 @@ +use rst_core::wire::Request; + +pub async fn request(req: Request) { + use Request::*; + match req { + Register(name, pw) => {}, + _ => todo!(), + } +} -- cgit v1.2.3