feat: refactor prudp code and start working on refactoring rmc

This commit is contained in:
DJMrTV 2025-02-18 22:55:33 +01:00
commit 3ea7c7e671
37 changed files with 2029 additions and 456 deletions

View file

@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
use log::{error, info, trace};
use thiserror::Error;
use tokio::sync::RwLock;
use crate::prudp::socket::SocketData;
use crate::prudp::socket::{new_socket_pair, AnyInternalSocket, CryptoHandler, ExternalSocket};
use crate::prudp::packet::{PRUDPPacket, VirtualPort};
use crate::prudp::router::Error::VirtualPortTaken;
@ -22,10 +22,9 @@ static SERVER_DATAGRAMS: Lazy<u8> = Lazy::new(||{
});
pub struct Router {
endpoints: RwLock<[Option<Arc<SocketData>>; 16]>,
endpoints: RwLock<[Option<Arc<dyn AnyInternalSocket>>; 16]>,
running: AtomicBool,
socket: Arc<UdpSocket>,
//pub auth_module: Arc<dyn AuthModule>
_no_outside_construction: PhantomData<()>
}
#[derive(Debug, Error)]
@ -36,9 +35,6 @@ pub enum Error{
impl Router {
fn process_prudp_packet(&self, _packet: &PRUDPPacket){
}
async fn process_prudp_packets<'a>(self: Arc<Self>, _socket: Arc<UdpSocket>, addr: SocketAddrV4, udp_message: Vec<u8>){
let mut stream = Cursor::new(&udp_message);
@ -69,7 +65,9 @@ impl Router {
trace!("sending packet to endpoint");
endpoint.process_packet(connection, &packet).await;
tokio::spawn(async move {
endpoint.recieve_packet(connection, packet).await
});
}
}
@ -88,6 +86,7 @@ impl Router {
continue;
};
let current_msg = &msg_buffer[0..len];
tokio::spawn(self.clone().process_prudp_packets(socket.clone(), addr, current_msg.to_vec()));
@ -144,18 +143,22 @@ impl Router {
}
// returns Some(()) i
pub(crate) async fn add_socket(&self, socket: Arc<SocketData>) -> Result<(), Error>{
pub(crate) async fn add_socket<E: CryptoHandler>(&self, virtual_port: VirtualPort, encryption: E)
-> Result<ExternalSocket, Error>{
let mut endpoints = self.endpoints.write().await;
let idx = socket.get_virual_port().get_port_number() as usize;
let idx = virtual_port.get_port_number() as usize;
if endpoints[idx].is_none() {
endpoints[idx] = Some(socket);
} else {
// dont create the socket if we dont need to
if !endpoints[idx].is_none(){
return Err(VirtualPortTaken(idx as u8));
}
Ok(())
let (internal, external) = new_socket_pair(virtual_port, encryption, self.socket.clone());
endpoints[idx] = Some(internal);
Ok(external)
}
pub fn get_own_address(&self) -> SocketAddrV4{