handle connection registration
This commit is contained in:
parent
ad86a74efb
commit
20aa273d67
7 changed files with 233 additions and 24 deletions
|
|
@ -49,7 +49,7 @@ fn main() {
|
|||
info!("setting up endpoints");
|
||||
|
||||
let auth_endpoints = vec![
|
||||
Endpoint::new(VirtualPort::new(1,10))
|
||||
Endpoint::new(auth_server.socket.clone(), VirtualPort::new(1,10))
|
||||
];
|
||||
|
||||
auth_server.endpoints.set(auth_endpoints)
|
||||
|
|
|
|||
|
|
@ -1,18 +1,30 @@
|
|||
use std::collections::HashMap;
|
||||
use std::net::UdpSocket;
|
||||
use std::sync::Arc;
|
||||
use log::info;
|
||||
use crate::prudp::packet::{PRUDPPacket, VirtualPort};
|
||||
use crate::prudp::server::Connection;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use log::{error, info};
|
||||
use rand::random;
|
||||
use crate::prudp::packet::{flags, PRUDPPacket, VirtualPort};
|
||||
use crate::prudp::sockaddr::PRUDPSockAddr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Endpoint{
|
||||
virtual_port: VirtualPort,
|
||||
socket: Arc<UdpSocket>,
|
||||
connections: RwLock<HashMap<PRUDPSockAddr, Connection>>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Connection{
|
||||
sock_addr: PRUDPSockAddr,
|
||||
id: u64
|
||||
}
|
||||
|
||||
impl Endpoint{
|
||||
pub fn new(port: VirtualPort) -> Self{
|
||||
pub fn new(socket: Arc<UdpSocket>, port: VirtualPort) -> Self{
|
||||
Self{
|
||||
virtual_port: port
|
||||
socket,
|
||||
virtual_port: port,
|
||||
connections: Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -20,8 +32,44 @@ impl Endpoint{
|
|||
self.virtual_port
|
||||
}
|
||||
|
||||
pub fn process_packet(&self, connection: &Connection, packet: &PRUDPPacket){
|
||||
info!("recieved packet on endpoint")
|
||||
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) ||
|
||||
((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) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -50,6 +50,15 @@ impl TypesFlags{
|
|||
}
|
||||
}
|
||||
|
||||
pub mod flags{
|
||||
pub const ACK: u16 = 0x001;
|
||||
pub const RELIABLE: u16 = 0x002;
|
||||
pub const NEED_ACK: u16 = 0x004;
|
||||
pub const HAS_SIZE: u16 = 0x008;
|
||||
pub const MULTI_ACK: u16 = 0x200;
|
||||
|
||||
}
|
||||
|
||||
impl Debug for TypesFlags{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let stream_type = self.get_types();
|
||||
|
|
@ -59,7 +68,7 @@ impl Debug for TypesFlags{
|
|||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Copy, Clone, Pod, Zeroable, SwapEndian)]
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Pod, Zeroable, SwapEndian, Hash)]
|
||||
pub struct VirtualPort(u8);
|
||||
|
||||
impl VirtualPort{
|
||||
|
|
|
|||
|
|
@ -23,16 +23,12 @@ static SERVER_DATAGRAMS: Lazy<u8> = Lazy::new(||{
|
|||
|
||||
pub struct NexServer{
|
||||
pub endpoints: OnceLock<Vec<Endpoint>>,
|
||||
pub socket: Arc<UdpSocket>,
|
||||
pub running: AtomicBool,
|
||||
//pub auth_module: Arc<dyn AuthModule>
|
||||
_no_outside_construction: PhantomData<()>
|
||||
}
|
||||
|
||||
pub struct Connection<'a>{
|
||||
pub socket: &'a UdpSocket,
|
||||
pub prudp_addr: PRUDPSockAddr
|
||||
}
|
||||
|
||||
|
||||
impl NexServer{
|
||||
fn process_prudp_packet(&self, packet: &PRUDPPacket){
|
||||
|
|
@ -52,10 +48,7 @@ impl NexServer{
|
|||
|
||||
trace!("got valid prudp packet from someone({}): \n{:?}", addr, packet);
|
||||
|
||||
let connection = Connection{
|
||||
socket,
|
||||
prudp_addr: packet.source_sockaddr(addr)
|
||||
};
|
||||
let connection = packet.source_sockaddr(addr);
|
||||
|
||||
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");
|
||||
|
|
@ -65,13 +58,13 @@ impl NexServer{
|
|||
let Some(endpoint) = endpoints.iter().find(|e|{
|
||||
e.get_virual_port().get_port_number() == packet.header.destination_port.get_port_number()
|
||||
}) else {
|
||||
error!("connection to invalid endpoint({}) attempted by {}", packet.header.destination_port.get_port_number(), connection.prudp_addr.regular_socket_addr);
|
||||
error!("connection to invalid endpoint({}) attempted by {}", packet.header.destination_port.get_port_number(), connection.regular_socket_addr);
|
||||
continue;
|
||||
};
|
||||
|
||||
trace!("sending packet to endpoint");
|
||||
|
||||
endpoint.process_packet(&connection, &packet);
|
||||
endpoint.process_packet(connection, &packet);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,16 +90,17 @@ impl NexServer{
|
|||
}
|
||||
|
||||
pub fn new(addr: SocketAddrV4) -> io::Result<(Arc<Self>, JoinHandle<()>)>{
|
||||
let socket = Arc::new(UdpSocket::bind(addr)?);
|
||||
|
||||
let own_impl = NexServer{
|
||||
endpoints: Default::default(),
|
||||
running: AtomicBool::new(true),
|
||||
socket: socket.clone(),
|
||||
_no_outside_construction: Default::default()
|
||||
};
|
||||
|
||||
let arc = Arc::new(own_impl);
|
||||
|
||||
let socket = Arc::new(UdpSocket::bind(addr)?);
|
||||
|
||||
let mut thread = None;
|
||||
|
||||
for _ in 0..*SERVER_DATAGRAMS {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use std::net::SocketAddrV4;
|
||||
use crate::prudp::packet::VirtualPort;
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Debug, Copy, Clone)]
|
||||
pub struct PRUDPSockAddr{
|
||||
pub regular_socket_addr: SocketAddrV4,
|
||||
pub virtual_port: VirtualPort
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue