rust-nex/src/prudp/endpoint.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2025-01-19 20:05:44 +01:00
use std::collections::HashMap;
use std::net::UdpSocket;
2025-01-19 20:05:44 +01:00
use std::sync::{Arc, RwLock};
use log::{error, info};
use rand::random;
use crate::prudp::packet::{flags, PRUDPPacket, types, VirtualPort};
2025-01-19 20:05:44 +01:00
use crate::prudp::sockaddr::PRUDPSockAddr;
#[derive(Debug)]
2025-01-19 13:02:15 +01:00
pub struct Endpoint{
virtual_port: VirtualPort,
2025-01-20 18:01:54 +01:00
socket: UdpSocket,
2025-01-19 20:05:44 +01:00
connections: RwLock<HashMap<PRUDPSockAddr, Connection>>
}
#[derive(Debug)]
pub struct Connection{
sock_addr: PRUDPSockAddr,
id: u64
}
impl Endpoint{
2025-01-20 18:01:54 +01:00
pub fn new(socket: UdpSocket, port: VirtualPort) -> Self{
Self{
2025-01-19 20:05:44 +01:00
socket,
virtual_port: port,
connections: Default::default()
}
}
pub fn get_virual_port(&self) -> VirtualPort{
self.virtual_port
}
2025-01-19 13:02:15 +01:00
2025-01-19 20:05:44 +01:00
pub fn process_packet(&self, connection: PRUDPSockAddr, packet: &PRUDPPacket){
info!("recieved packet on endpoint");
let conn = self.connections.read().expect("poison");
if !conn.contains_key(&connection){
drop(conn);
let mut conn = self.connections.write().expect("poison");
//only insert if we STILL dont have the connection preventing double insertion
if !conn.contains_key(&connection) {
conn.insert(connection, Connection {
sock_addr: connection,
id: random()
});
}
drop(conn);
} else {
drop(conn);
}
let conn = self.connections.read().expect("poison");
let Some(conn) = conn.get(&connection) else {
error!("connection is still not present after making sure connection is present, giving up.");
return;
};
if //((packet.header.types_and_flags.get_flags() & flags::NEED_ACK) != 0) ||
2025-01-19 20:05:44 +01:00
((packet.header.types_and_flags.get_flags() & flags::ACK) != 0) ||
((packet.header.types_and_flags.get_flags() & flags::RELIABLE) != 0) ||
((packet.header.types_and_flags.get_flags() & flags::MULTI_ACK) != 0) {
2025-01-19 20:11:19 +01:00
let copy = packet.header.types_and_flags;
unimplemented!("{:?}", copy)
2025-01-19 20:05:44 +01:00
}
match packet.header.types_and_flags.get_types() {
types::SYN => {
2025-01-20 18:01:54 +01:00
// reset heartbeat?
let response_header = packet.base_response_header();
2025-01-19 20:05:44 +01:00
2025-01-20 18:01:54 +01:00
}
_ => unimplemented!()
}
2025-01-19 20:05:44 +01:00
}
2025-01-19 13:02:15 +01:00
}