@@ -56,10 +56,8 @@ pub struct Timers {
56
56
/// Is the owner of the timer the initiator or the responder for the last handshake?
57
57
is_initiator : bool ,
58
58
timers : [ Instant ; TimerName :: Top as usize ] ,
59
- /// Did we receive data without sending anything back?
60
- ///
61
- /// If `Some`, tracks the timestamp when we want to send a keepalive.
62
- want_passive_keepalive_at : Option < Instant > ,
59
+ /// The last data packet we received without sending a reply.
60
+ last_data_received_without_reply : Option < Instant > ,
63
61
/// The earliest data packet we sent without receiving a reply.
64
62
first_data_sent_without_reply : Option < Instant > ,
65
63
persistent_keepalive : usize ,
@@ -81,7 +79,7 @@ impl Timers {
81
79
Timers {
82
80
is_initiator : false ,
83
81
timers : [ now; TimerName :: Top as usize ] ,
84
- want_passive_keepalive_at : Default :: default ( ) ,
82
+ last_data_received_without_reply : Default :: default ( ) ,
85
83
first_data_sent_without_reply : Default :: default ( ) ,
86
84
persistent_keepalive : usize:: from ( persistent_keepalive. unwrap_or ( 0 ) ) ,
87
85
should_reset_rr : reset_rr,
@@ -126,6 +124,12 @@ impl Timers {
126
124
Some ( first_packet_without_reply + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT )
127
125
}
128
126
127
+ pub ( crate ) fn keepalive_after_time_without_send ( & self ) -> Option < Instant > {
128
+ let last_data_received_without_reply = self . last_data_received_without_reply ?;
129
+
130
+ Some ( last_data_received_without_reply + KEEPALIVE_TIMEOUT )
131
+ }
132
+
129
133
fn is_initiator ( & self ) -> bool {
130
134
self . is_initiator
131
135
}
@@ -141,7 +145,7 @@ impl Timers {
141
145
* t = now;
142
146
}
143
147
self . first_data_sent_without_reply = None ;
144
- self . want_passive_keepalive_at = None ;
148
+ self . last_data_received_without_reply = None ;
145
149
}
146
150
}
147
151
@@ -165,10 +169,10 @@ impl Tunn {
165
169
self . timers . first_data_sent_without_reply = None ;
166
170
}
167
171
TimeLastPacketSent => {
168
- self . timers . want_passive_keepalive_at = None ;
172
+ self . timers . last_data_received_without_reply = None ;
169
173
}
170
174
TimeLastDataPacketReceived => {
171
- self . timers . want_passive_keepalive_at = Some ( now + KEEPALIVE_TIMEOUT ) ;
175
+ self . timers . last_data_received_without_reply = Some ( now) ;
172
176
}
173
177
TimeLastDataPacketSent => {
174
178
match self . timers . first_data_sent_without_reply {
@@ -366,8 +370,8 @@ impl Tunn {
366
370
// to the given peer in KEEPALIVE ms, we send an empty packet.
367
371
if self
368
372
. timers
369
- . want_passive_keepalive_at
370
- . is_some_and ( |keepalive_at | now >= keepalive_at )
373
+ . keepalive_after_time_without_send ( )
374
+ . is_some_and ( |deadline | now >= deadline )
371
375
{
372
376
tracing:: debug!( "KEEPALIVE(KEEPALIVE_TIMEOUT)" ) ;
373
377
keepalive_required = true ;
@@ -415,7 +419,7 @@ impl Tunn {
415
419
pub fn next_timer_update ( & self ) -> Option < Instant > {
416
420
iter:: empty ( )
417
421
. chain ( self . timers . send_handshake_at )
418
- . chain ( self . timers . want_passive_keepalive_at )
422
+ . chain ( self . timers . last_data_received_without_reply )
419
423
. min ( )
420
424
}
421
425
0 commit comments