parent
6e8e37406d
commit
5dab336049
@ -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() |
||||
} |
||||
} |
@ -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…
Reference in new issue