diff --git a/Cargo.toml b/Cargo.toml index d4ecb51..d5054cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kcpserver" -version = "1.1.3" +version = "1.1.4" authors = ["luyi "] edition = "2021" license = "MIT/Apache-2.0" @@ -20,13 +20,9 @@ log = "0.4" async-trait = "0.1.40" async-lock = "3.3" data-rw = "1.6" -chrono="0.4" atomic-waker = "1" futures="0.3" [dev-dependencies] -env_logger = "0.10" +env_logger = "0.11" anyhow = "1" - -[profile.dev] -overflow-checks=false \ No newline at end of file diff --git a/src/kcp/kcp_module/kcp.rs b/src/kcp/kcp_module/kcp.rs index 0368acd..0bd061d 100644 --- a/src/kcp/kcp_module/kcp.rs +++ b/src/kcp/kcp_module/kcp.rs @@ -257,19 +257,31 @@ impl Kcp { /// `output` is the callback object for writing. /// /// `conv` represents conversation. - pub fn new(conv: u32, output: UDPPeer, key: Option>) -> Self { - Kcp::construct(conv, output, false, key) + pub fn new(conv: u32, current_ts: u32, output: UDPPeer, key: Option>) -> Self { + Kcp::construct(conv, current_ts, output, false, key) } /// Creates a KCP control object in stream mode, `conv` must be equal in both endpoints in one connection. /// `output` is the callback object for writing. /// /// `conv` represents conversation. - pub fn new_stream(conv: u32, output: UDPPeer, stream: bool, key: Option>) -> Self { - Kcp::construct(conv, output, stream, key) + pub fn new_stream( + conv: u32, + current_ts: u32, + output: UDPPeer, + stream: bool, + key: Option>, + ) -> Self { + Kcp::construct(conv, current_ts, output, stream, key) } - fn construct(conv: u32, output: UDPPeer, stream: bool, key: Option>) -> Self { + fn construct( + conv: u32, + current_ts: u32, + output: UDPPeer, + stream: bool, + key: Option>, + ) -> Self { Kcp { conv, snd_una: 0, @@ -280,7 +292,7 @@ impl Kcp { state: 0, cwnd: 0, probe: 0, - current: 0, + current: current_ts, xmit: 0, nodelay: false, updated: false, @@ -496,18 +508,15 @@ impl Kcp { return; } - for i in 0..self.snd_buf.len() { + let mut i = 0_usize; + while i < self.snd_buf.len() { match sn.cmp(&self.snd_buf[i].sn) { Ordering::Equal => { self.snd_buf.remove(i); break; } - Ordering::Less => { - break; - } - _ => { - continue; - } + Ordering::Less => break, + _ => i += 1, } } } @@ -1023,7 +1032,6 @@ impl Kcp { /// Or you can ask `check` when to call this again. pub async fn update(&mut self, current: u32) -> KcpResult<()> { self.current = current; - if !self.updated { self.updated = true; self.ts_flush = self.current; @@ -1108,11 +1116,11 @@ impl Kcp { } /// Set check interval - pub fn set_interval(&mut self, mut interval: u32) { - if interval > 5000 { - interval = 5000; - } else if interval < 10 { - interval = 10; + pub fn set_interval(&mut self, interval: u32) { + match interval { + interval if interval < 10 => self.interval = 10, + interval if interval > 5000 => self.interval = 5000, + _ => self.interval = interval, } self.interval = interval; } diff --git a/src/kcp/kcp_server/kcp_listener.rs b/src/kcp/kcp_server/kcp_listener.rs index 8e933f3..39ed2d3 100644 --- a/src/kcp/kcp_server/kcp_listener.rs +++ b/src/kcp/kcp_server/kcp_listener.rs @@ -7,7 +7,7 @@ use std::io; use std::net::ToSocketAddrs; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; -use std::time::Duration; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tokio::time::sleep; use udp_server::prelude::{UDPPeer, UdpServer}; @@ -234,7 +234,7 @@ where udp_peer: &UDPPeer, key: Option>, ) -> KCPPeer { - let mut kcp = Kcp::new(conv, udp_peer.clone(), key); + let mut kcp = Kcp::new(conv, Self::timestamp(), udp_peer.clone(), key); self.config.apply_config(&mut kcp); KcpPeer::new(kcp, conv, udp_peer.get_addr()) } @@ -242,7 +242,10 @@ where /// 获取当前时间戳 转换为u32 #[inline(always)] fn timestamp() -> u32 { - let time = chrono::Local::now().timestamp_millis() & 0xffffffff; - time as u32 + let start = SystemTime::now(); + let since_the_epoch = start + .duration_since(UNIX_EPOCH) + .expect("time went afterwards"); + since_the_epoch.as_millis() as u32 } }