added redirection of data to their corresponding endpoints

This commit is contained in:
DJMrTV 2025-01-19 18:57:36 +01:00
commit e0d9fa444b
5 changed files with 52 additions and 20 deletions

View file

@ -3,9 +3,11 @@ use std::{env, fs};
use std::fs::File;
use std::net::{Ipv4Addr, SocketAddrV4};
use chrono::Local;
use log::info;
use log::{info, trace};
use once_cell::sync::Lazy;
use simplelog::{ColorChoice, CombinedLogger, Config, LevelFilter, TerminalMode, TermLogger, WriteLogger};
use crate::prudp::endpoint::Endpoint;
use crate::prudp::packet::VirtualPort;
use crate::prudp::server::NexServer;
mod endianness;
@ -29,7 +31,7 @@ fn main() {
CombinedLogger::init(
vec![
TermLogger::new(LevelFilter::Info, Config::default(), TerminalMode::Mixed, ColorChoice::Auto),
WriteLogger::new(LevelFilter::Info, Config::default(), {
WriteLogger::new(LevelFilter::max(), Config::default(), {
fs::create_dir_all("log").unwrap();
File::create(format!("log/{}.log", Local::now().to_rfc2822())).unwrap()
})
@ -44,7 +46,17 @@ fn main() {
NexServer::new(SocketAddrV4::new(*OWN_IP, *AUTH_SERVER_PORT))
.expect("unable to startauth server");
info!("joining auth server");
info!("setting up endpoints");
let auth_endpoints = vec![
Endpoint::new(VirtualPort::new(1,10))
];
auth_server.endpoints.set(auth_endpoints)
.expect("endpoints were somehow set by something else???");
trace!("joining auth server");
auth_server_join_handle.join().unwrap();
}

View file

@ -1,19 +1,27 @@
use std::net::UdpSocket;
use std::sync::Arc;
use crate::prudp::packet::VirtualPort;
use log::info;
use crate::prudp::packet::{PRUDPPacket, VirtualPort};
use crate::prudp::server::Connection;
#[derive(Debug)]
pub struct Endpoint{
socket: Arc<UdpSocket>,
virtual_port: VirtualPort,
}
impl Endpoint{
pub fn new(port: VirtualPort) -> Self{
Self{
virtual_port: port
}
}
pub fn get_virual_port(&self) -> VirtualPort{
self.virtual_port
}
fn process_packet(connection: &Connection){
pub fn process_packet(&self, connection: &Connection, packet: &PRUDPPacket){
info!("recieved packet on endpoint")
}
}

View file

@ -1,5 +1,5 @@
pub mod packet;
pub mod server;
mod endpoint;
pub mod endpoint;
mod auth_module;
mod sockaddr;

View file

@ -63,14 +63,17 @@ impl Debug for TypesFlags{
pub struct VirtualPort(u8);
impl VirtualPort{
pub fn get_stream_type(self) -> u8 {
#[inline]
pub const fn get_stream_type(self) -> u8 {
(self.0 & 0xF0) >> 4
}
pub fn get_port_number(self) -> u8 {
#[inline]
pub const fn get_port_number(self) -> u8 {
(self.0 & 0x0F)
}
#[inline]
pub fn stream_type(self, val: u8) -> Self {
let masked_val = val & 0x0F;
assert_eq!(masked_val, val);
@ -78,12 +81,18 @@ impl VirtualPort{
Self((self.0 & 0xF0) | masked_val)
}
#[inline]
pub fn port_number(self, val: u8) -> Self {
let masked_val = val & 0x0F;
assert_eq!(masked_val, val);
Self((self.0 & 0x0F) | (masked_val << 4))
}
#[inline]
pub fn new(port: u8, stream_type: u8) -> Self{
Self(0).stream_type(stream_type).port_number(port)
}
}
impl Debug for VirtualPort{

View file

@ -1,14 +1,15 @@
use std::{env, io, thread};
use std::cell::OnceCell;
use std::io::Cursor;
use std::marker::PhantomData;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream, UdpSocket};
use std::net::SocketAddr::V4;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{Arc, Mutex, OnceLock, RwLock};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::JoinHandle;
use once_cell::sync::Lazy;
use log::{error, info};
use log::{error, info, trace, warn};
use crate::prudp::auth_module::AuthModule;
use crate::prudp::endpoint::Endpoint;
use crate::prudp::packet::{PRUDPPacket, VirtualPort};
@ -21,15 +22,15 @@ static SERVER_DATAGRAMS: Lazy<u8> = Lazy::new(||{
});
pub struct NexServer{
pub endpoints: RwLock<Vec<Endpoint>>,
pub endpoints: OnceLock<Vec<Endpoint>>,
pub running: AtomicBool,
//pub auth_module: Arc<dyn AuthModule>
_no_outside_construction: PhantomData<()>
}
pub struct Connection<'a>{
socket: &'a UdpSocket,
prudp_addr: PRUDPSockAddr
pub socket: &'a UdpSocket,
pub prudp_addr: PRUDPSockAddr
}
@ -49,14 +50,17 @@ impl NexServer{
},
};
info!("got valid prudp packet from someone({}): \n{:?}", addr, packet);
trace!("got valid prudp packet from someone({}): \n{:?}", addr, packet);
let connection = Connection{
socket,
prudp_addr: packet.source_sockaddr(addr)
};
let endpoints = self.endpoints.read().expect("poison");
let Some(endpoints) = self.endpoints.get() else{
warn!("Got a message: ignoring because the server is still starting or the endpoints havent been set up");
break;
};
let Some(endpoint) = endpoints.iter().find(|e|{
e.get_virual_port().get_port_number() == connection.prudp_addr.virtual_port.get_port_number()
@ -65,10 +69,9 @@ impl NexServer{
continue;
};
trace!("sending packet to endpoint");
endpoint.process_packet(&connection, &packet);
}
}