Commit a6800c1e authored by nanamicat's avatar nanamicat

32 bit time

parent 94e98c1e
Pipeline #42083 passed with stages
in 4 minutes and 29 seconds
...@@ -33,7 +33,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -33,7 +33,7 @@ async fn main() -> anyhow::Result<()> {
// groups // groups
// .iter() // .iter()
// .map(|g| (g.id, g.routers(&groups, &routers_data))) // .map(|g| (g.id, g.routers(&groups, &routers_data)))
// .collect::<BTreeMap<u16, HashSet<u8>>>(), // .collect::<BTreeMap<u32, HashSet<u8>>>(),
); );
let mut hello = Hello { time: 0 }; let mut hello = Hello { time: 0 };
...@@ -73,11 +73,11 @@ async fn main() -> anyhow::Result<()> { ...@@ -73,11 +73,11 @@ async fn main() -> anyhow::Result<()> {
_ = timer.tick() => { _ = timer.tick() => {
// to clients // to clients
hello.time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_millis() as u16; hello.time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_millis() as u32;
let message = bincode::encode_to_vec(&hello, bincode::config::standard())?; let len = bincode::encode_into_slice(&hello, &mut buf, bincode::config::standard())?;
for id in connections[&config.id].keys() { for id in connections[&config.id].keys() {
let router = &routers[id]; let router = &routers[id];
let _ = socket.send_to(message.as_slice(), router.link_address).await; let _ = socket.send_to(&buf[..len], router.link_address).await;
} }
// to server // to server
...@@ -91,8 +91,8 @@ async fn main() -> anyhow::Result<()> { ...@@ -91,8 +91,8 @@ async fn main() -> anyhow::Result<()> {
.map(|(from,_)|routers.get_mut(from).unwrap().update(hello.time)) .map(|(from,_)|routers.get_mut(from).unwrap().update(hello.time))
.collect(), .collect(),
}; };
let message = bincode::encode_to_vec(&report, bincode::config::standard())?; let len = bincode::encode_into_slice(&report, &mut buf, bincode::config::standard())?;
let _ = socket.send_to(message.as_slice(), config.server).await; let _ = socket.send_to(&buf[..len], config.server).await;
} }
} }
} }
......
...@@ -3,7 +3,7 @@ use bincode::{Decode, Encode}; ...@@ -3,7 +3,7 @@ use bincode::{Decode, Encode};
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
pub struct Hello { pub struct Hello {
pub time: u16, pub time: u32,
} }
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
......
...@@ -9,9 +9,9 @@ use std::time::SystemTime; ...@@ -9,9 +9,9 @@ use std::time::SystemTime;
pub struct Router { pub struct Router {
pub link_address: SocketAddr, pub link_address: SocketAddr,
quality: PeerQuality, quality: PeerQuality,
remote_time: u16, remote_time: u32,
local_time: u16, local_time: u32,
history: Vec<Option<i16>>, history: Vec<Option<i32>>,
} }
impl Router { impl Router {
...@@ -49,13 +49,13 @@ impl Router { ...@@ -49,13 +49,13 @@ impl Router {
pub fn on_message(&mut self, data: &Hello) { pub fn on_message(&mut self, data: &Hello) {
// 这个包发出距离上一个包 // 这个包发出距离上一个包
let diff = data.time.wrapping_sub(self.remote_time) as i16; let diff = data.time.wrapping_sub(self.remote_time) as i32;
// 收到时间略小于或相等的,可能是网络乱序或重包,忽略 // 收到时间略小于或相等的,可能是网络乱序或重包,忽略
if -(TIMEOUT.as_millis() as i16) < diff && diff <= 0 { if -(TIMEOUT.as_millis() as i32) < diff && diff <= 0 {
return; return;
} }
if 0 < diff && diff <= (TIMEOUT.as_millis() as i16) { if 0 < diff && diff <= (TIMEOUT.as_millis() as i32) {
// 差距较小,补上中间丢的包 // 差距较小,补上中间丢的包
let step = (diff as f64 / INTERVAL.as_millis() as f64).round() as u8; let step = (diff as f64 / INTERVAL.as_millis() as f64).round() as u8;
for _ in 0..step - 1 { for _ in 0..step - 1 {
...@@ -67,9 +67,9 @@ impl Router { ...@@ -67,9 +67,9 @@ impl Router {
} }
self.remote_time = data.time; self.remote_time = data.time;
self.local_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as u16; self.local_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as u32;
let delay = self.local_time.wrapping_sub(self.remote_time) as i16; let delay = self.local_time.wrapping_sub(self.remote_time) as i32;
self.history.push(Some(delay)); self.history.push(Some(delay));
...@@ -77,7 +77,7 @@ impl Router { ...@@ -77,7 +77,7 @@ impl Router {
self.history.drain(0..self.history.len() - HISTORY as usize); self.history.drain(0..self.history.len() - HISTORY as usize);
} }
let received: Vec<i16> = self.history.iter().filter_map(|&s| s).collect(); let received: Vec<i32> = self.history.iter().filter_map(|&s| s).collect();
assert!(!received.is_empty()); // 因为走到这里一定刚放过一个进去 assert!(!received.is_empty()); // 因为走到这里一定刚放过一个进去
self.quality.reliability = (received.len() * 255 / HISTORY as usize) as u8; self.quality.reliability = (received.len() * 255 / HISTORY as usize) as u8;
...@@ -85,14 +85,14 @@ impl Router { ...@@ -85,14 +85,14 @@ impl Router {
self.quality.jitter = (1..received.len()).map(|i| f64::from(received[i].abs_diff(received[i - 1]))).collect::<Mean>().mean() as u8; self.quality.jitter = (1..received.len()).map(|i| f64::from(received[i].abs_diff(received[i - 1]))).collect::<Mean>().mean() as u8;
} }
pub(crate) fn update(&mut self, local_time: u16) -> PeerQuality { pub(crate) fn update(&mut self, local_time: u32) -> PeerQuality {
if self.quality.reliability > 0 { if self.quality.reliability > 0 {
let diff = (local_time.wrapping_sub(self.local_time) as i16 as f64 / INTERVAL.as_millis() as f64).round() as i16; let diff = (local_time.wrapping_sub(self.local_time) as i32 as f64 / INTERVAL.as_millis() as f64).round() as i32;
// 有几个包没到 // 有几个包没到
if diff > TIMEOUT.as_millis() as i16 { if diff > TIMEOUT.as_millis() as i32 {
self.reset(); self.reset();
} else if diff >= (INTERVAL.as_millis() * 2) as i16 { } else if diff >= (INTERVAL.as_millis() * 2) as i32 {
self.quality.reliability = self.quality.reliability.saturating_sub(255 / HISTORY); self.quality.reliability = self.quality.reliability.saturating_sub(255 / HISTORY);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment