mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-07 21:13:44 +02:00
Some simplification.
This commit is contained in:
parent
9430d336f9
commit
bc02651613
2 changed files with 12 additions and 14 deletions
|
@ -1,33 +1,30 @@
|
||||||
// (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md.
|
// (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md.
|
||||||
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
use crate::sys::udp::BoundUdpSocket;
|
use crate::sys::udp::BoundUdpSocket;
|
||||||
|
|
||||||
static LOCAL_SOCKET_UNIQUE_ID_COUNTER: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
|
|
||||||
/// Local socket wrapper to provide to the core.
|
/// Local socket wrapper to provide to the core.
|
||||||
///
|
///
|
||||||
/// This implements very fast hash and equality in terms of an arbitrary unique ID assigned at
|
/// This wraps a bound UDP socket in weak form so sockets that are released by the UDP
|
||||||
/// construction and holds a weak reference to the bound socket so dead sockets will silently
|
/// binding engine can be "garbage collected" by the core.
|
||||||
/// cease to exist or work. This also means that this code can check the weak count to determine
|
#[repr(transparent)]
|
||||||
/// if the core is currently holding/using a socket for any reason.
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct LocalSocket(pub(crate) Weak<BoundUdpSocket>, usize);
|
pub struct LocalSocket(Weak<BoundUdpSocket>);
|
||||||
|
|
||||||
impl LocalSocket {
|
impl LocalSocket {
|
||||||
|
#[inline]
|
||||||
pub fn new(s: &Arc<BoundUdpSocket>) -> Self {
|
pub fn new(s: &Arc<BoundUdpSocket>) -> 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 {
|
pub fn is_valid(&self) -> bool {
|
||||||
self.0.strong_count() > 0
|
self.0.strong_count() > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
pub fn socket(&self) -> Option<Arc<BoundUdpSocket>> {
|
pub fn socket(&self) -> Option<Arc<BoundUdpSocket>> {
|
||||||
self.0.upgrade()
|
self.0.upgrade()
|
||||||
}
|
}
|
||||||
|
@ -36,7 +33,7 @@ impl LocalSocket {
|
||||||
impl PartialEq for LocalSocket {
|
impl PartialEq for LocalSocket {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn eq(&self, other: &Self) -> bool {
|
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 {
|
impl Hash for LocalSocket {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||||
self.1.hash(state)
|
self.0.as_ptr().hash(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -204,6 +204,7 @@ impl<Inner: InnerProtocolLayer + ?Sized + 'static> ApplicationLayer for VL1Servi
|
||||||
type LocalSocket = crate::LocalSocket;
|
type LocalSocket = crate::LocalSocket;
|
||||||
type LocalInterface = crate::LocalInterface;
|
type LocalInterface = crate::LocalInterface;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn event(&self, event: Event) {
|
fn event(&self, event: Event) {
|
||||||
println!("{}", event.to_string());
|
println!("{}", event.to_string());
|
||||||
match event {
|
match event {
|
||||||
|
@ -244,7 +245,7 @@ impl<Inner: InnerProtocolLayer + ?Sized + 'static> ApplicationLayer for VL1Servi
|
||||||
Endpoint::IpUdp(address) => {
|
Endpoint::IpUdp(address) => {
|
||||||
// This is the fast path -- the socket is known to the core so just send it.
|
// 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) = local_socket {
|
||||||
if let Some(s) = s.0.upgrade() {
|
if let Some(s) = s.socket() {
|
||||||
s.send(address, data, packet_ttl);
|
s.send(address, data, packet_ttl);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Reference in a new issue