From bc02651613784c431ad3fee3e9a5898ecee98c70 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 3 Jan 2023 13:36:53 -0500 Subject: [PATCH] Some simplification. --- vl1-service/src/localsocket.rs | 23 ++++++++++------------- vl1-service/src/vl1service.rs | 3 ++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/vl1-service/src/localsocket.rs b/vl1-service/src/localsocket.rs index 0b17fe8b0..4d178c02c 100644 --- a/vl1-service/src/localsocket.rs +++ b/vl1-service/src/localsocket.rs @@ -1,33 +1,30 @@ // (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md. use std::hash::Hash; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Weak}; use crate::sys::udp::BoundUdpSocket; -static LOCAL_SOCKET_UNIQUE_ID_COUNTER: AtomicUsize = AtomicUsize::new(0); - /// Local socket wrapper to provide to the core. /// -/// This implements very fast hash and equality in terms of an arbitrary unique ID assigned at -/// construction and holds a weak reference to the bound socket so dead sockets will silently -/// cease to exist or work. This also means that this code can check the weak count to determine -/// if the core is currently holding/using a socket for any reason. +/// This wraps a bound UDP socket in weak form so sockets that are released by the UDP +/// binding engine can be "garbage collected" by the core. +#[repr(transparent)] #[derive(Clone)] -pub struct LocalSocket(pub(crate) Weak, usize); +pub struct LocalSocket(Weak); impl LocalSocket { + #[inline] pub fn new(s: &Arc) -> Self { - Self(Arc::downgrade(s), LOCAL_SOCKET_UNIQUE_ID_COUNTER.fetch_add(1, Ordering::SeqCst)) + Self(Arc::downgrade(s)) } - #[inline(always)] + #[inline] pub fn is_valid(&self) -> bool { self.0.strong_count() > 0 } - #[inline(always)] + #[inline] pub fn socket(&self) -> Option> { self.0.upgrade() } @@ -36,7 +33,7 @@ impl LocalSocket { impl PartialEq for LocalSocket { #[inline(always)] fn eq(&self, other: &Self) -> bool { - self.1 == other.1 + self.0.ptr_eq(&other.0) } } @@ -45,7 +42,7 @@ impl Eq for LocalSocket {} impl Hash for LocalSocket { #[inline(always)] fn hash(&self, state: &mut H) { - self.1.hash(state) + self.0.as_ptr().hash(state) } } diff --git a/vl1-service/src/vl1service.rs b/vl1-service/src/vl1service.rs index d2b3c6b59..0c3c2e693 100644 --- a/vl1-service/src/vl1service.rs +++ b/vl1-service/src/vl1service.rs @@ -204,6 +204,7 @@ impl ApplicationLayer for VL1Servi type LocalSocket = crate::LocalSocket; type LocalInterface = crate::LocalInterface; + #[inline] fn event(&self, event: Event) { println!("{}", event.to_string()); match event { @@ -244,7 +245,7 @@ impl ApplicationLayer for VL1Servi Endpoint::IpUdp(address) => { // This is the fast path -- the socket is known to the core so just send it. if let Some(s) = local_socket { - if let Some(s) = s.0.upgrade() { + if let Some(s) = s.socket() { s.send(address, data, packet_ttl); } else { return;