Skip to content

Commit

Permalink
replace macros with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Feb 23, 2022
1 parent 8c32ac3 commit bb851db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 67 deletions.
11 changes: 5 additions & 6 deletions src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use futures::future::join_all;
mod tcp;
use tcp::TcpListener;

use crate::utils::Endpoint;
use crate::utils::{EndpointRef, RemoteAddrRef, ConnectOptsRef};
use crate::utils::{Ref, Endpoint, RemoteAddr, ConnectOpts};

pub async fn run(endpoints: Vec<Endpoint>) {
let mut workers = Vec::with_capacity(compute_workers(&endpoints));
Expand All @@ -19,16 +18,16 @@ pub async fn run(endpoints: Vec<Endpoint>) {
join_all(workers).await;
}

pub async fn run_tcp(endpoint: EndpointRef) {
pub async fn run_tcp(endpoint: Ref<Endpoint>) {
let Endpoint {
listen,
remote,
opts,
..
} = endpoint.as_ref();

let remote: RemoteAddrRef = remote.into();
let opts: ConnectOptsRef = opts.into();
let remote: Ref<RemoteAddr> = remote.into();
let opts: Ref<ConnectOpts> = opts.into();

let lis = TcpListener::bind(*listen)
.await
Expand Down Expand Up @@ -66,7 +65,7 @@ pub async fn run_tcp(endpoint: EndpointRef) {
mod udp;

#[cfg(feature = "udp")]
pub async fn run_udp(endpoint: EndpointRef) {
pub async fn run_udp(endpoint: Ref<Endpoint>) {
use tokio::net::UdpSocket;

let Endpoint {
Expand Down
7 changes: 3 additions & 4 deletions src/relay/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ use log::debug;
use tokio::net::TcpSocket;

use crate::utils::socket;
use crate::utils::ConnectOpts;
use crate::utils::{RemoteAddrRef, ConnectOptsRef};
use crate::utils::{Ref, RemoteAddr, ConnectOpts};

#[allow(unused_variables)]
pub async fn connect_and_relay(
mut inbound: TcpStream,
remote: RemoteAddrRef,
conn_opts: ConnectOptsRef,
remote: Ref<RemoteAddr>,
conn_opts: Ref<ConnectOpts>,
) -> Result<()> {
let ConnectOpts {
fast_open,
Expand Down
16 changes: 8 additions & 8 deletions src/relay/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ use std::collections::HashMap;

use log::{debug, info, error};

use tokio::net::UdpSocket;

use crate::utils::DEFAULT_BUF_SIZE;

use crate::utils::RemoteAddr;
use crate::utils::ConnectOptsRef;
use crate::utils::{Ref, RemoteAddr, ConnectOpts};

use crate::utils::timeoutfut;
use crate::utils::socket;

// client <--> allocated socket
use tokio::net::UdpSocket;
use crate::utils::UdpSocketRef;
use crate::utils::{SockMap, SockMapRef};

type SockMap = RwLock<HashMap<SocketAddr, Arc<UdpSocket>>>;

const BUF_SIZE: usize = DEFAULT_BUF_SIZE;

Expand All @@ -28,7 +28,7 @@ pub async fn associate_and_relay(
sock_map: &SockMap,
listen_sock: &UdpSocket,
remote_addr: &RemoteAddr,
conn_opts: ConnectOptsRef,
conn_opts: Ref<ConnectOpts>,
) -> Result<()> {
let timeout = conn_opts.udp_timeout;
let mut buf = vec![0u8; BUF_SIZE];
Expand Down Expand Up @@ -74,9 +74,9 @@ pub async fn associate_and_relay(
}

async fn send_back(
sock_map: SockMapRef,
sock_map: Ref<SockMap>,
client_addr: SocketAddr,
listen_sock: UdpSocketRef,
listen_sock: Ref<UdpSocket>,
alloc_sock: Arc<UdpSocket>,
timeout: usize,
) {
Expand Down
76 changes: 27 additions & 49 deletions src/utils/ref_types.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
use core::ops::Deref;

use cfg_if::cfg_if;

use super::{Endpoint, RemoteAddr, ConnectOpts};

// Safety:
// pointer is not null once inited(comes from an immutable ref)
// pointee memory is always valid during the eventloop
#[repr(transparent)]
pub struct Ref<T>(*const T);

unsafe impl<T> Send for Ref<T> {}
unsafe impl<T> Sync for Ref<T> {}

macro_rules! ptr_wrap {
($old: ident,$new: ident) => {
#[derive(Clone, Copy)]
pub struct $new {
ptr: *const $old,
}

unsafe impl Send for $new {}
unsafe impl Sync for $new {}

impl AsRef<$old> for $new {
#[inline]
fn as_ref(&self) -> &$old {
unsafe { &*self.ptr }
}
}

impl Deref for $new {
type Target = $old;

#[inline]
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}

impl From<&$old> for $new {
fn from(ptr: &$old) -> Self {
$new { ptr }
}
}
};
impl<T> Copy for Ref<T> {}

impl<T> Clone for Ref<T> {
fn clone(&self) -> Self {
*self
}
}

ptr_wrap!(Endpoint, EndpointRef);
ptr_wrap!(RemoteAddr, RemoteAddrRef);
ptr_wrap!(ConnectOpts, ConnectOptsRef);
impl<T> Deref for Ref<T> {
type Target = T;

cfg_if! {
if #[cfg(feature = "udp")] {
use std::sync::{Arc,RwLock};
use std::collections::HashMap;
use std::net::SocketAddr;
use tokio::net::UdpSocket;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*self.0 }
}
}

// client <--> allocated socket
pub type SockMap = RwLock<HashMap<SocketAddr, Arc<UdpSocket>>>;
impl<T> AsRef<T> for Ref<T> {
#[inline]
fn as_ref(&self) -> &T {
unsafe { &*self.0 }
}
}

ptr_wrap!(UdpSocket, UdpSocketRef);
ptr_wrap!(SockMap, SockMapRef);
impl<T> From<&T> for Ref<T> {
fn from(x: &T) -> Self {
Ref(x as *const _)
}
}

0 comments on commit bb851db

Please sign in to comment.