Commit 1c797d7b authored by nanamicat's avatar nanamicat

time

parent 6874aaee
Pipeline #42441 passed with stages
in 2 minutes and 54 seconds
...@@ -45,7 +45,6 @@ async fn main() -> anyhow::Result<()> { ...@@ -45,7 +45,6 @@ async fn main() -> anyhow::Result<()> {
let mut timer = time::interval(INTERVAL); let mut timer = time::interval(INTERVAL);
let mut buf = [0; 1500]; let mut buf = [0; 1500];
let start = Instant::now(); let start = Instant::now();
let start_timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let resolver = Resolver::builder_tokio()?.build(); let resolver = Resolver::builder_tokio()?.build();
...@@ -62,9 +61,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -62,9 +61,7 @@ async fn main() -> anyhow::Result<()> {
&& let Some(peer) = Router::get(&mut routers, addr) && let Some(peer) = Router::get(&mut routers, addr)
&& let Ok((hello, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard()) && let Ok((hello, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard())
{ {
let now = Instant::now(); peer.on_message(&hello);
let now_timestamp = (start_timestamp + now.duration_since(start)).as_millis() as u32;
peer.on_message(&hello, now, now_timestamp);
} else if addr.port() == config.server.port } else if addr.port() == config.server.port
&& let Ok((downlink, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard()) && let Ok((downlink, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard())
&& let Some(uplink) = server.on_message(downlink, &routers_data, &connections[&config.id], &config).await && let Some(uplink) = server.on_message(downlink, &routers_data, &connections[&config.id], &config).await
...@@ -75,7 +72,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -75,7 +72,7 @@ async fn main() -> anyhow::Result<()> {
} }
now = timer.tick() => { now = timer.tick() => {
let hello = Hello { time: (start_timestamp + now.duration_since(start)).as_millis() as u32 }; let hello = Hello { time: SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_millis() as u32 };
let len = bincode::encode_into_slice(&hello, &mut buf, 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() {
......
...@@ -4,6 +4,7 @@ use crate::settings::{Settings, INTERVAL}; ...@@ -4,6 +4,7 @@ use crate::settings::{Settings, INTERVAL};
use saturating_cast::SaturatingCast; use saturating_cast::SaturatingCast;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::SystemTime;
use tokio::time::Instant; use tokio::time::Instant;
pub struct Router { pub struct Router {
...@@ -41,7 +42,7 @@ impl Router { ...@@ -41,7 +42,7 @@ impl Router {
} }
} }
pub fn on_message(&mut self, data: &Hello, now: Instant, now_timestamp: u32) { pub fn on_message(&mut self, data: &Hello) {
let delta = (data.time.wrapping_sub(self.remote_time) as i32 as f32 / INTERVAL.as_millis() as f32).round() as i32; let delta = (data.time.wrapping_sub(self.remote_time) as i32 as f32 / INTERVAL.as_millis() as f32).round() as i32;
match delta { match delta {
-63..=-1 => { -63..=-1 => {
...@@ -51,7 +52,7 @@ impl Router { ...@@ -51,7 +52,7 @@ impl Router {
1..=63 => { 1..=63 => {
self.receive = (self.receive << delta) | 1; self.receive = (self.receive << delta) | 1;
self.remote_time = data.time; self.remote_time = data.time;
self.local_time = now; self.local_time = Instant::now();
} }
_ => { _ => {
self.remote_time = data.time; self.remote_time = data.time;
...@@ -59,7 +60,7 @@ impl Router { ...@@ -59,7 +60,7 @@ impl Router {
} }
} }
let delay = now_timestamp.wrapping_sub(data.time) as i32; let delay = (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as u32).wrapping_sub(data.time) as i32;
self.delay = delay; //+= (delay - self.delay) / 4; self.delay = delay; //+= (delay - self.delay) / 4;
......
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