-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clock: implement the new clock, basic connection_cookie
- Loading branch information
Showing
11 changed files
with
143 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,48 @@ | ||
use std::collections::hash_map::DefaultHasher; | ||
use std::hash::{Hash, Hasher}; | ||
use std::net::SocketAddr; | ||
use std::time::SystemTime; | ||
use std::time::Duration; | ||
|
||
use aquatic_udp_protocol::ConnectionId; | ||
|
||
pub fn get_connection_id(remote_address: &SocketAddr) -> ConnectionId { | ||
match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) { | ||
Ok(duration) => ConnectionId(((duration.as_secs() / 3600) | ((remote_address.port() as u64) << 36)) as i64), | ||
Err(_) => ConnectionId(0x7FFFFFFFFFFFFFFF), | ||
} | ||
use super::clock::{DefaultTime, Time, UnixTimeValue}; | ||
use crate::udp::ServerError; | ||
|
||
pub fn make_connection_cookie(lifetime: &Duration, remote_address: &SocketAddr) -> ConnectionId { | ||
let period = DefaultTime::periods_after_elapse(&lifetime, &lifetime); | ||
|
||
let mut hasher = DefaultHasher::new(); | ||
|
||
remote_address.hash(&mut hasher); | ||
hasher.write_u128(period); | ||
|
||
let connection_id_cookie = i64::from_le_bytes(hasher.finish().to_le_bytes()); | ||
|
||
ConnectionId(connection_id_cookie) | ||
} | ||
|
||
pub fn current_time() -> u64 { | ||
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() | ||
pub fn check_connection_cookie( | ||
lifetime: &Duration, | ||
remote_address: &SocketAddr, | ||
connection_id: &ConnectionId, | ||
) -> Result<(), ServerError> { | ||
let period = DefaultTime::periods_after_elapse(&lifetime, &lifetime); | ||
|
||
for n in 0..=1 { | ||
let mut hasher = DefaultHasher::new(); | ||
|
||
remote_address.hash(&mut hasher); | ||
hasher.write_u128(period.saturating_sub(n)); | ||
|
||
let connection_id_cookie = i64::from_le_bytes(hasher.finish().to_le_bytes()); | ||
|
||
if (*connection_id).0 == connection_id_cookie { | ||
return Ok(()); | ||
} | ||
} | ||
Err(ServerError::InvalidConnectionId) | ||
} | ||
|
||
pub fn ser_instant<S: serde::Serializer>(inst: &std::time::Instant, ser: S) -> Result<S::Ok, S::Error> { | ||
ser.serialize_u64(inst.elapsed().as_millis() as u64) | ||
pub fn ser_unix_time_value<S: serde::Serializer>(unix_time_value: &UnixTimeValue, ser: S) -> Result<S::Ok, S::Error> { | ||
ser.serialize_u64(unix_time_value.as_millis() as u64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.