1- #[ cfg( feature = "strong" ) ]
2- use std:: time:: Instant ;
31use std:: {
42 collections:: HashMap ,
53 io,
@@ -24,8 +22,7 @@ use tokio::{
2422 task,
2523} ;
2624
27- #[ cfg( not( feature = "strong" ) ) ]
28- use crate :: instant:: Instant ;
25+ use crate :: instant:: { ReferenceInstant , RelativeInstant } ;
2926use crate :: {
3027 packet:: EchoRequestPacket ,
3128 raw_pinger:: { RawBlockingPinger , RawPinger } ,
@@ -44,6 +41,7 @@ pub struct Pinger<V: IpVersion> {
4441
4542struct InnerPinger < V : IpVersion > {
4643 raw : RawPinger < V > ,
44+ reference : ReferenceInstant ,
4745 round_sender : mpsc:: UnboundedSender < RoundMessage < V > > ,
4846 identifier : u16 ,
4947 sequence_number : AtomicU16 ,
@@ -53,9 +51,9 @@ enum RoundMessage<V: IpVersion> {
5351 Subscribe {
5452 sequence_number : u16 ,
5553 #[ cfg( feature = "strong" ) ]
56- sender : mpsc:: UnboundedSender < ( V , Instant ) > ,
54+ sender : mpsc:: UnboundedSender < ( V , RelativeInstant ) > ,
5755 #[ cfg( not( feature = "strong" ) ) ]
58- sender : mpsc:: UnboundedSender < ( V , Instant , Instant ) > ,
56+ sender : mpsc:: UnboundedSender < ( V , RelativeInstant , RelativeInstant ) > ,
5957 } ,
6058 Unsubscribe {
6159 sequence_number : u16 ,
@@ -70,6 +68,7 @@ impl<V: IpVersion> Pinger<V> {
7068 /// be beneficial to `Drop` the `Pinger` and recreate it if
7169 /// you are not going to be sending pings for a long period of time.
7270 pub fn new ( ) -> io:: Result < Self > {
71+ let reference = ReferenceInstant :: new ( ) ;
7372 let raw = RawPinger :: new ( ) ?;
7473 let raw_blocking = RawBlockingPinger :: new ( ) ?;
7574
@@ -81,6 +80,7 @@ impl<V: IpVersion> Pinger<V> {
8180
8281 let inner = Arc :: new ( InnerPinger {
8382 raw,
83+ reference : reference. clone ( ) ,
8484 round_sender : sender,
8585 identifier,
8686 sequence_number : AtomicU16 :: new ( 0 ) ,
@@ -89,11 +89,14 @@ impl<V: IpVersion> Pinger<V> {
8989 let mut buf = [ MaybeUninit :: < u8 > :: uninit ( ) ; 1600 ] ;
9090
9191 #[ cfg( feature = "strong" ) ]
92- let mut subscribers: HashMap < u16 , mpsc:: UnboundedSender < ( V , Instant ) > > = HashMap :: new ( ) ;
92+ let mut subscribers: HashMap <
93+ u16 ,
94+ mpsc:: UnboundedSender < ( V , RelativeInstant ) > ,
95+ > = HashMap :: new ( ) ;
9396 #[ cfg( not( feature = "strong" ) ) ]
9497 let mut subscribers: HashMap <
9598 u16 ,
96- mpsc:: UnboundedSender < ( V , Instant , Instant ) > ,
99+ mpsc:: UnboundedSender < ( V , RelativeInstant , RelativeInstant ) > ,
97100 > = HashMap :: new ( ) ;
98101 ' packets: loop {
99102 let maybe_packet = match raw_blocking. recv ( & mut buf) {
@@ -106,13 +109,13 @@ impl<V: IpVersion> Pinger<V> {
106109
107110 match & maybe_packet {
108111 Some ( packet) if packet. identifier ( ) == identifier => {
109- let recv_instant = Instant :: now ( ) ;
112+ let recv_instant = reference . now ( ) ;
110113
111114 #[ cfg( not( feature = "strong" ) ) ]
112115 let send_instant = {
113116 let payload = packet. payload ( ) ;
114- match Instant :: decode (
115- payload[ ..Instant :: ENCODED_LEN ] . try_into ( ) . unwrap ( ) ,
117+ match RelativeInstant :: decode (
118+ payload[ ..RelativeInstant :: ENCODED_LEN ] . try_into ( ) . unwrap ( ) ,
116119 ) {
117120 Some ( send_instant) => send_instant,
118121 None => continue ' packets,
@@ -271,11 +274,11 @@ pub struct MeasureManyStream<'a, V: IpVersion, I: Iterator<Item = V>> {
271274 pinger : & ' a Pinger < V > ,
272275 send_queue : Peekable < I > ,
273276 #[ cfg( feature = "strong" ) ]
274- in_flight : HashMap < V , Instant > ,
277+ in_flight : HashMap < V , RelativeInstant > ,
275278 #[ cfg( feature = "strong" ) ]
276- receiver : mpsc:: UnboundedReceiver < ( V , Instant ) > ,
279+ receiver : mpsc:: UnboundedReceiver < ( V , RelativeInstant ) > ,
277280 #[ cfg( not( feature = "strong" ) ) ]
278- receiver : mpsc:: UnboundedReceiver < ( V , Instant , Instant ) > ,
281+ receiver : mpsc:: UnboundedReceiver < ( V , RelativeInstant , RelativeInstant ) > ,
279282 sequence_number : u16 ,
280283}
281284
@@ -299,7 +302,7 @@ impl<V: IpVersion, I: Iterator<Item = V>> MeasureManyStream<'_, V, I> {
299302 getrandom:: fill ( & mut payload) . expect ( "generate random payload" ) ;
300303 #[ cfg( not( feature = "strong" ) ) ]
301304 {
302- let now = Instant :: now ( ) . encode ( ) ;
305+ let now = self . pinger . inner . reference . now ( ) . encode ( ) ;
303306 let ( now_part, random_part) = payload. split_at_mut ( now. len ( ) ) ;
304307 now_part. copy_from_slice ( & now) ;
305308 getrandom:: fill ( random_part) . expect ( "generate random payload" ) ;
@@ -313,7 +316,7 @@ impl<V: IpVersion, I: Iterator<Item = V>> MeasureManyStream<'_, V, I> {
313316 match self . pinger . inner . raw . poll_send_to ( cx, addr, & packet) {
314317 Poll :: Ready ( _) => {
315318 #[ cfg( feature = "strong" ) ]
316- let sent_at = Instant :: now ( ) ;
319+ let sent_at = self . pinger . inner . reference . now ( ) ;
317320
318321 let taken_addr = self . send_queue . next ( ) ;
319322 debug_assert ! ( taken_addr. is_some( ) ) ;
0 commit comments