Skip to content

Commit

Permalink
fix: kcp ctor current ts is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
luyi committed Jul 1, 2024
1 parent ef49aa8 commit e56d09b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kcpserver"
version = "1.1.3"
version = "1.1.4"
authors = ["luyi <[email protected]>"]
edition = "2021"
license = "MIT/Apache-2.0"
Expand All @@ -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
46 changes: 27 additions & 19 deletions src/kcp/kcp_module/kcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>>) -> Self {
Kcp::construct(conv, output, false, key)
pub fn new(conv: u32, current_ts: u32, output: UDPPeer, key: Option<Vec<u8>>) -> 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<Vec<u8>>) -> Self {
Kcp::construct(conv, output, stream, key)
pub fn new_stream(
conv: u32,
current_ts: u32,
output: UDPPeer,
stream: bool,
key: Option<Vec<u8>>,
) -> Self {
Kcp::construct(conv, current_ts, output, stream, key)
}

fn construct(conv: u32, output: UDPPeer, stream: bool, key: Option<Vec<u8>>) -> Self {
fn construct(
conv: u32,
current_ts: u32,
output: UDPPeer,
stream: bool,
key: Option<Vec<u8>>,
) -> Self {
Kcp {
conv,
snd_una: 0,
Expand All @@ -280,7 +292,7 @@ impl Kcp {
state: 0,
cwnd: 0,
probe: 0,
current: 0,
current: current_ts,
xmit: 0,
nodelay: false,
updated: false,
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions src/kcp/kcp_server/kcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -234,15 +234,18 @@ where
udp_peer: &UDPPeer,
key: Option<Vec<u8>>,
) -> 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())
}

/// 获取当前时间戳 转换为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
}
}

0 comments on commit e56d09b

Please sign in to comment.