Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set current_time to absolute system time to prevent drift #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions demo_bevy/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ use bevy::{
};
use bevy_egui::{EguiContexts, EguiPlugin};
use bevy_renet::{
client_connected,
renet::{ClientId, RenetClient},
RenetClientPlugin,
};
use demo_bevy::{
connection_config, setup_level, ClientChannel, NetworkedEntities, PlayerCommand, PlayerInput, ServerChannel, ServerMessages,
setup_level, ClientChannel, NetworkedEntities, PlayerCommand, PlayerInput, ServerChannel, ServerMessages,
};
use renet_visualizer::{RenetClientVisualizer, RenetVisualizerStyle};

Expand Down
4 changes: 2 additions & 2 deletions renet_netcode/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl NetcodeClientTransport {
}

/// Advances the transport by the duration, and receive packets from the network.
pub fn update(&mut self, duration: Duration, client: &mut RenetClient) -> Result<(), NetcodeTransportError> {
pub fn update(&mut self, _duration: Duration, client: &mut RenetClient) -> Result<(), NetcodeTransportError> {
if let Some(reason) = self.netcode_client.disconnect_reason() {
// Mark the client as disconnected if an error occured in the transport layer
client.disconnect_due_to_transport();
Expand Down Expand Up @@ -124,7 +124,7 @@ impl NetcodeClientTransport {
}
}

if let Some((packet, addr)) = self.netcode_client.update(duration) {
if let Some((packet, addr)) = self.netcode_client.update() {
self.socket.send_to(packet, addr)?;
}

Expand Down
4 changes: 1 addition & 3 deletions renetcode/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ fn client(authentication: ClientAuthentication) {
let stdin_channel = spawn_stdin_channel();
let mut buffer = [0u8; NETCODE_MAX_PACKET_BYTES];

let mut last_updated = Instant::now();
loop {
if let Some(err) = client.disconnect_reason() {
panic!("Client error: {:?}", err);
Expand Down Expand Up @@ -212,10 +211,9 @@ fn client(authentication: ClientAuthentication) {
};
}

if let Some((packet, addr)) = client.update(Instant::now() - last_updated) {
if let Some((packet, addr)) = client.update() {
udp_socket.send_to(packet, addr).unwrap();
}
last_updated = Instant::now();
thread::sleep(Duration::from_millis(50));
}
}
Expand Down
15 changes: 8 additions & 7 deletions renetcode/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, fmt, net::SocketAddr, time::Duration};
use std::{error::Error, fmt, net::SocketAddr, time::{Duration, SystemTime}};

use crate::{
packet::Packet, replay_protection::ReplayProtection, token::ConnectToken, NetcodeError, NETCODE_CHALLENGE_TOKEN_BYTES,
Expand Down Expand Up @@ -270,8 +270,8 @@ impl NetcodeClient {

/// Update the internal state of the client, receives the duration since last updated.
/// Might return the serve address and a protocol packet to be sent to the server.
pub fn update(&mut self, duration: Duration) -> Option<(&mut [u8], SocketAddr)> {
if let Err(e) = self.update_internal_state(duration) {
pub fn update(&mut self) -> Option<(&mut [u8], SocketAddr)> {
if let Err(e) = self.update_internal_state() {
log::error!("Failed to update client: {}", e);
return None;
}
Expand All @@ -280,8 +280,9 @@ impl NetcodeClient {
self.generate_packet()
}

fn update_internal_state(&mut self, duration: Duration) -> Result<(), NetcodeError> {
self.current_time += duration;
fn update_internal_state(&mut self) -> Result<(), NetcodeError> {
self.current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the best approach.

Instead there should be some method to synchronize the clocks.
Otherwise it is not possible to set a custom delay.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the current_time should be synchronized every so often by passing in a special duration.


let connection_timed_out = self.connect_token.timeout_seconds > 0
&& (self.last_packet_received_time + Duration::from_secs(self.connect_token.timeout_seconds as u64) < self.current_time);

Expand Down Expand Up @@ -405,7 +406,7 @@ mod tests {
let client_key = connect_token.client_to_server_key;
let authentication = ClientAuthentication::Secure { connect_token };
let mut client = NetcodeClient::new(Duration::ZERO, authentication).unwrap();
let (packet_buffer, _) = client.update(Duration::ZERO).unwrap();
let (packet_buffer, _) = client.update().unwrap();

let (r_sequence, packet) = Packet::decode(packet_buffer, protocol_id, None, None).unwrap();
assert_eq!(0, r_sequence);
Expand All @@ -419,7 +420,7 @@ mod tests {
client.process_packet(&mut buffer[..len]);
assert_eq!(ClientState::SendingConnectionResponse, client.state);

let (packet_buffer, _) = client.update(Duration::ZERO).unwrap();
let (packet_buffer, _) = client.update().unwrap();
let (_, packet) = Packet::decode(packet_buffer, protocol_id, Some(&client_key), None).unwrap();
assert!(matches!(packet, Packet::Response { .. }));

Expand Down
4 changes: 2 additions & 2 deletions renetcode/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ mod tests {
.unwrap();
let client_auth = ClientAuthentication::Secure { connect_token };
let mut client = NetcodeClient::new(Duration::ZERO, client_auth).unwrap();
let (client_packet, _) = client.update(Duration::ZERO).unwrap();
let (client_packet, _) = client.update().unwrap();

let result = server.process_packet(client_addr, client_packet);
assert!(matches!(result, ServerResult::PacketToSend { .. }));
Expand All @@ -772,7 +772,7 @@ mod tests {
};

assert!(!client.is_connected());
let (client_packet, _) = client.update(Duration::ZERO).unwrap();
let (client_packet, _) = client.update().unwrap();
let result = server.process_packet(client_addr, client_packet);

match result {
Expand Down