1
0

unifying const types

This commit is contained in:
EAimTY 2023-05-29 20:50:24 +09:00
parent 6c61c70c06
commit 02ba5056ee
3 changed files with 25 additions and 25 deletions

View File

@ -19,7 +19,7 @@ use socks5_proto::Address as Socks5Address;
use std::{ use std::{
net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket}, net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket},
sync::{ sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicU32, Ordering},
Arc, Arc,
}, },
time::Duration, time::Duration,
@ -36,8 +36,8 @@ static ENDPOINT: OnceCell<Mutex<Endpoint>> = OnceCell::new();
static CONNECTION: AsyncOnceCell<AsyncMutex<Connection>> = AsyncOnceCell::const_new(); static CONNECTION: AsyncOnceCell<AsyncMutex<Connection>> = AsyncOnceCell::const_new();
static TIMEOUT: AtomicCell<Duration> = AtomicCell::new(Duration::from_secs(0)); static TIMEOUT: AtomicCell<Duration> = AtomicCell::new(Duration::from_secs(0));
pub const CONNECTION_CLOSE_ERROR_CODE: VarInt = VarInt::from_u32(0); pub const ERROR_CODE: VarInt = VarInt::from_u32(0);
const DEFAULT_CONCURRENT_STREAMS: usize = 32; const DEFAULT_CONCURRENT_STREAMS: u32 = 32;
pub struct Endpoint { pub struct Endpoint {
ep: QuinnEndpoint, ep: QuinnEndpoint,
@ -197,8 +197,8 @@ pub struct Connection {
udp_relay_mode: UdpRelayMode, udp_relay_mode: UdpRelayMode,
remote_uni_stream_cnt: Counter, remote_uni_stream_cnt: Counter,
remote_bi_stream_cnt: Counter, remote_bi_stream_cnt: Counter,
max_concurrent_uni_streams: Arc<AtomicUsize>, max_concurrent_uni_streams: Arc<AtomicU32>,
max_concurrent_bi_streams: Arc<AtomicUsize>, max_concurrent_bi_streams: Arc<AtomicU32>,
} }
impl Connection { impl Connection {
@ -252,8 +252,8 @@ impl Connection {
udp_relay_mode, udp_relay_mode,
remote_uni_stream_cnt: Counter::new(), remote_uni_stream_cnt: Counter::new(),
remote_bi_stream_cnt: Counter::new(), remote_bi_stream_cnt: Counter::new(),
max_concurrent_uni_streams: Arc::new(AtomicUsize::new(DEFAULT_CONCURRENT_STREAMS)), max_concurrent_uni_streams: Arc::new(AtomicU32::new(DEFAULT_CONCURRENT_STREAMS)),
max_concurrent_bi_streams: Arc::new(AtomicUsize::new(DEFAULT_CONCURRENT_STREAMS)), max_concurrent_bi_streams: Arc::new(AtomicU32::new(DEFAULT_CONCURRENT_STREAMS)),
}; };
tokio::spawn(conn.clone().init(heartbeat, gc_interval, gc_lifetime)); tokio::spawn(conn.clone().init(heartbeat, gc_interval, gc_lifetime));
@ -346,12 +346,12 @@ impl Connection {
async fn accept_uni_stream(&self) -> Result<(RecvStream, Register), Error> { async fn accept_uni_stream(&self) -> Result<(RecvStream, Register), Error> {
let max = self.max_concurrent_uni_streams.load(Ordering::Relaxed); let max = self.max_concurrent_uni_streams.load(Ordering::Relaxed);
if self.remote_uni_stream_cnt.count() == max { if self.remote_uni_stream_cnt.count() as u32 == max {
self.max_concurrent_uni_streams self.max_concurrent_uni_streams
.store(max * 2, Ordering::Relaxed); .store(max * 2, Ordering::Relaxed);
self.conn self.conn
.set_max_concurrent_uni_streams(VarInt::from((max * 2) as u32)); .set_max_concurrent_uni_streams(VarInt::from(max * 2));
} }
let recv = self.conn.accept_uni().await?; let recv = self.conn.accept_uni().await?;
@ -362,12 +362,12 @@ impl Connection {
async fn accept_bi_stream(&self) -> Result<(SendStream, RecvStream, Register), Error> { async fn accept_bi_stream(&self) -> Result<(SendStream, RecvStream, Register), Error> {
let max = self.max_concurrent_bi_streams.load(Ordering::Relaxed); let max = self.max_concurrent_bi_streams.load(Ordering::Relaxed);
if self.remote_bi_stream_cnt.count() == max { if self.remote_bi_stream_cnt.count() as u32 == max {
self.max_concurrent_bi_streams self.max_concurrent_bi_streams
.store(max * 2, Ordering::Relaxed); .store(max * 2, Ordering::Relaxed);
self.conn self.conn
.set_max_concurrent_bi_streams(VarInt::from((max * 2) as u32)); .set_max_concurrent_bi_streams(VarInt::from(max * 2));
} }
let (send, recv) = self.conn.accept_bi().await?; let (send, recv) = self.conn.accept_bi().await?;
@ -386,7 +386,7 @@ impl Connection {
Err(err) => Err(Error::Model(err)), Err(err) => Err(Error::Model(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
UdpRelayMode::Quic => { UdpRelayMode::Quic => {
log::debug!( log::info!(
"[relay] [packet] [{assoc_id:#06x}] [from-quic] [{pkt_id:#06x}] {frag_id}/{frag_total}", "[relay] [packet] [{assoc_id:#06x}] [from-quic] [{pkt_id:#06x}] {frag_id}/{frag_total}",
assoc_id = pkt.assoc_id(), assoc_id = pkt.assoc_id(),
pkt_id = pkt.pkt_id(), pkt_id = pkt.pkt_id(),
@ -428,7 +428,7 @@ impl Connection {
Err(err) => Err(Error::Model(err)), Err(err) => Err(Error::Model(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
UdpRelayMode::Native => { UdpRelayMode::Native => {
log::debug!( log::info!(
"[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] {frag_id}/{frag_total}", "[relay] [packet] [{assoc_id:#06x}] [from-native] [{pkt_id:#06x}] {frag_id}/{frag_total}",
assoc_id = pkt.assoc_id(), assoc_id = pkt.assoc_id(),
pkt_id = pkt.pkt_id(), pkt_id = pkt.pkt_id(),

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
config::Local, config::Local,
connection::{Connection as TuicConnection, CONNECTION_CLOSE_ERROR_CODE}, connection::{Connection as TuicConnection, ERROR_CODE},
Error, Error,
}; };
use bytes::Bytes; use bytes::Bytes;
@ -314,7 +314,7 @@ impl Server {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
let _ = conn.shutdown().await; let _ = conn.shutdown().await;
let _ = relay.get_mut().reset(CONNECTION_CLOSE_ERROR_CODE); let _ = relay.get_mut().reset(ERROR_CODE);
log::warn!("[socks5] [{peer_addr}] [connect] [{target_addr}] TCP stream relaying error: {err}"); log::warn!("[socks5] [{peer_addr}] [connect] [{target_addr}] TCP stream relaying error: {err}");
} }
}, },

View File

@ -21,7 +21,7 @@ use std::{
net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket as StdUdpSocket}, net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket as StdUdpSocket},
pin::Pin, pin::Pin,
sync::{ sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicU32, Ordering},
Arc, Arc,
}, },
task::{Context, Poll, Waker}, task::{Context, Poll, Waker},
@ -42,7 +42,7 @@ use tuic_quinn::{side, Authenticate, Connect, Connection as Model, Packet, Task}
use uuid::Uuid; use uuid::Uuid;
const ERROR_CODE: VarInt = VarInt::from_u32(0); const ERROR_CODE: VarInt = VarInt::from_u32(0);
const DEFAULT_CONCURRENT_STREAMS: usize = 32; const DEFAULT_CONCURRENT_STREAMS: u32 = 32;
pub struct Server { pub struct Server {
ep: Endpoint, ep: Endpoint,
@ -186,8 +186,8 @@ struct Connection {
max_external_pkt_size: usize, max_external_pkt_size: usize,
remote_uni_stream_cnt: Counter, remote_uni_stream_cnt: Counter,
remote_bi_stream_cnt: Counter, remote_bi_stream_cnt: Counter,
max_concurrent_uni_streams: Arc<AtomicUsize>, max_concurrent_uni_streams: Arc<AtomicU32>,
max_concurrent_bi_streams: Arc<AtomicUsize>, max_concurrent_bi_streams: Arc<AtomicU32>,
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -283,8 +283,8 @@ impl Connection {
max_external_pkt_size, max_external_pkt_size,
remote_uni_stream_cnt: Counter::new(), remote_uni_stream_cnt: Counter::new(),
remote_bi_stream_cnt: Counter::new(), remote_bi_stream_cnt: Counter::new(),
max_concurrent_uni_streams: Arc::new(AtomicUsize::new(DEFAULT_CONCURRENT_STREAMS)), max_concurrent_uni_streams: Arc::new(AtomicU32::new(DEFAULT_CONCURRENT_STREAMS)),
max_concurrent_bi_streams: Arc::new(AtomicUsize::new(DEFAULT_CONCURRENT_STREAMS)), max_concurrent_bi_streams: Arc::new(AtomicU32::new(DEFAULT_CONCURRENT_STREAMS)),
} }
} }
@ -319,12 +319,12 @@ impl Connection {
let max = self.max_concurrent_uni_streams.load(Ordering::Relaxed); let max = self.max_concurrent_uni_streams.load(Ordering::Relaxed);
if self.remote_uni_stream_cnt.count() == max { if self.remote_uni_stream_cnt.count() as u32 == max {
self.max_concurrent_uni_streams self.max_concurrent_uni_streams
.store(max * 2, Ordering::Relaxed); .store(max * 2, Ordering::Relaxed);
self.inner self.inner
.set_max_concurrent_uni_streams(VarInt::from((max * 2) as u32)); .set_max_concurrent_uni_streams(VarInt::from(max * 2));
} }
let pre_process = async { let pre_process = async {
@ -371,12 +371,12 @@ impl Connection {
let max = self.max_concurrent_bi_streams.load(Ordering::Relaxed); let max = self.max_concurrent_bi_streams.load(Ordering::Relaxed);
if self.remote_bi_stream_cnt.count() == max { if self.remote_bi_stream_cnt.count() as u32 == max {
self.max_concurrent_bi_streams self.max_concurrent_bi_streams
.store(max * 2, Ordering::Relaxed); .store(max * 2, Ordering::Relaxed);
self.inner self.inner
.set_max_concurrent_bi_streams(VarInt::from((max * 2) as u32)); .set_max_concurrent_bi_streams(VarInt::from(max * 2));
} }
let pre_process = async { let pre_process = async {