rstnode: add network packet envelope to wire types

wip/yesman
Katharina Fey 3 years ago
parent 6e8e37406d
commit 5dab336049
Signed by: kookie
GPG Key ID: F972AEEA2887D547
  1. 3
      games/rstnode/Cargo.lock
  2. 20
      games/rstnode/rst-core/src/wire/env.rs
  3. 13
      games/rstnode/rst-core/src/wire/mod.rs
  4. 5
      games/rstnode/rst-server/Cargo.toml
  5. 5
      games/rstnode/rst-server/src/constants.rs
  6. 39
      games/rstnode/rst-server/src/log.rs
  7. 16
      games/rstnode/rst-server/src/main.rs
  8. 43
      games/rstnode/rst-server/src/net/mod.rs
  9. 9
      games/rstnode/rst-server/src/net/parser.rs

@ -3676,7 +3676,10 @@ dependencies = [
name = "rst-node-server"
version = "0.0.0"
dependencies = [
"async-std",
"rst-core",
"tracing",
"tracing-subscriber",
]
[[package]]

@ -0,0 +1,20 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[repr(C)]
#[derive(Serialize, Deserialize)]
pub struct Envelope {
size: u64,
data: Vec<u8>,
}
impl Envelope {
pub fn pack<T: Serialize>(d: T) -> Self {
let data = bincode::serialize(&d).unwrap();
let size = data.len() as u64;
Self { size, data }
}
pub fn unpack<T: DeserializeOwned>(self) -> T {
bincode::deserialize(&self.data).unwrap()
}
}

@ -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

@ -7,4 +7,7 @@ license = "AGPL-3.0-or-later"
authors = ["Bread Machine", "Katharina Fey <kookie@spacekookie.de>"]
[dependencies]
rst-core = { path = "../rst-core" }
rst-core = { path = "../rst-core" }
async-std = { version = "1.0", features = ["attributes"] }
tracing = "0.1"
tracing-subscriber = "0.2"

@ -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");

@ -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)
})
}

@ -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;
}

@ -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<BTreeMap<Id, Client>>,
}
impl ServerEndpoint {
pub async fn new(bind: &str) -> Arc<Self> {
let socket = UdpSocket::bind(bind).await.unwrap();
Arc::new(Self {
socket,
bind: bind.into(),
clients: Default::default(),
})
}
pub async fn listen(self: &Arc<Self>) {
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,
}

@ -0,0 +1,9 @@
use rst_core::wire::Request;
pub async fn request(req: Request) {
use Request::*;
match req {
Register(name, pw) => {},
_ => todo!(),
}
}
Loading…
Cancel
Save