handle connection registration
This commit is contained in:
parent
ad86a74efb
commit
20aa273d67
7 changed files with 233 additions and 24 deletions
156
Cargo.lock
generated
156
Cargo.lock
generated
|
|
@ -29,6 +29,12 @@ version = "1.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.16.0"
|
||||
|
|
@ -55,6 +61,12 @@ dependencies = [
|
|||
"syn 2.0.96",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.10"
|
||||
|
|
@ -125,6 +137,16 @@ version = "0.15.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
|
|
@ -135,6 +157,21 @@ dependencies = [
|
|||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.0-rc.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a78f88e84d239c7f2619ae8b091603c26208e1cb322571f5a29d6806f56ee5e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"rustix",
|
||||
"wasi",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.61"
|
||||
|
|
@ -189,6 +226,12 @@ version = "0.2.169"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.25"
|
||||
|
|
@ -231,6 +274,15 @@ version = "0.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||
dependencies = [
|
||||
"zerocopy 0.7.35",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.93"
|
||||
|
|
@ -249,6 +301,37 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.0-beta.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6fccbfebb3972a41a31c605a59207d9fba5489b9a87d9d87024cb6df73a32ec7"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"zerocopy 0.8.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0-beta.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f16da77124f4ee9fabd55ce6540866e9101431863b4876de58b68797f331adf2"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.0-beta.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a98fa0b8309344136abe6244130311e76997e546f76fae8054422a7539b43df7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"zerocopy 0.8.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rc4"
|
||||
version = "0.1.0"
|
||||
|
|
@ -258,6 +341,19 @@ dependencies = [
|
|||
"cipher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.19"
|
||||
|
|
@ -311,6 +407,7 @@ dependencies = [
|
|||
"dotenv",
|
||||
"log",
|
||||
"once_cell",
|
||||
"rand",
|
||||
"rc4",
|
||||
"simplelog",
|
||||
"thiserror",
|
||||
|
|
@ -429,6 +526,15 @@ version = "0.9.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.13.3+wasi-0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
|
||||
dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.100"
|
||||
|
|
@ -577,3 +683,53 @@ name = "windows_x86_64_msvc"
|
|||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"zerocopy-derive 0.7.35",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a367f292d93d4eab890745e75a778da40909cab4d6ff8173693812f79c4a2468"
|
||||
dependencies = [
|
||||
"zerocopy-derive 0.8.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.96",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3931cb58c62c13adec22e38686b559c86a30565e16ad6e8510a337cedc611e1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.96",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -13,4 +13,5 @@ v_byte_macros = { git = "https://github.com/DJMrTV/VByteMacros" }
|
|||
simplelog = "0.12.2"
|
||||
chrono = "0.4.39"
|
||||
log = "0.4.25"
|
||||
anyhow = "1.0.95"
|
||||
anyhow = "1.0.95"
|
||||
rand = "0.9.0-beta.3"
|
||||
|
|
@ -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