xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_idle_network_detector.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_CORE_QUIC_IDLE_NETWORK_DETECTOR_H_
6 #define QUICHE_QUIC_CORE_QUIC_IDLE_NETWORK_DETECTOR_H_
7 
8 #include "quiche/quic/core/quic_alarm.h"
9 #include "quiche/quic/core/quic_alarm_factory.h"
10 #include "quiche/quic/core/quic_one_block_arena.h"
11 #include "quiche/quic/core/quic_time.h"
12 #include "quiche/quic/platform/api/quic_export.h"
13 #include "quiche/quic/platform/api/quic_flags.h"
14 
15 namespace quic {
16 
17 namespace test {
18 class QuicConnectionPeer;
19 class QuicIdleNetworkDetectorTestPeer;
20 }  // namespace test
21 
22 // QuicIdleNetworkDetector detects handshake timeout and idle network timeout.
23 // Handshake timeout detection is disabled after handshake completes. Idle
24 // network deadline is extended by network activity (e.g., sending or receiving
25 // packets).
26 class QUICHE_EXPORT QuicIdleNetworkDetector {
27  public:
28   class QUICHE_EXPORT Delegate {
29    public:
~Delegate()30     virtual ~Delegate() {}
31 
32     // Called when handshake times out.
33     virtual void OnHandshakeTimeout() = 0;
34 
35     // Called when idle network has been detected.
36     virtual void OnIdleNetworkDetected() = 0;
37   };
38 
39   QuicIdleNetworkDetector(Delegate* delegate, QuicTime now,
40                           QuicConnectionArena* arena,
41                           QuicAlarmFactory* alarm_factory,
42                           QuicConnectionContext* context);
43 
44   void OnAlarm();
45 
46   // Called to set handshake_timeout_ and idle_network_timeout_.
47   void SetTimeouts(QuicTime::Delta handshake_timeout,
48                    QuicTime::Delta idle_network_timeout);
49 
50   // Stop the detection once and for all.
51   void StopDetection();
52 
53   // Called when a packet gets sent.
54   void OnPacketSent(QuicTime now, QuicTime::Delta pto_delay);
55 
56   // Called when a packet gets received.
57   void OnPacketReceived(QuicTime now);
58 
enable_shorter_idle_timeout_on_sent_packet()59   void enable_shorter_idle_timeout_on_sent_packet() {
60     shorter_idle_timeout_on_sent_packet_ = true;
61   }
62 
handshake_timeout()63   QuicTime::Delta handshake_timeout() const { return handshake_timeout_; }
64 
time_of_last_received_packet()65   QuicTime time_of_last_received_packet() const {
66     return time_of_last_received_packet_;
67   }
68 
last_network_activity_time()69   QuicTime last_network_activity_time() const {
70     return std::max(time_of_last_received_packet_,
71                     time_of_first_packet_sent_after_receiving_);
72   }
73 
idle_network_timeout()74   QuicTime::Delta idle_network_timeout() const { return idle_network_timeout_; }
75 
76   QuicTime GetIdleNetworkDeadline() const;
77 
78  private:
79   friend class test::QuicConnectionPeer;
80   friend class test::QuicIdleNetworkDetectorTestPeer;
81 
82   void SetAlarm();
83 
84   void MaybeSetAlarmOnSentPacket(QuicTime::Delta pto_delay);
85 
86   QuicTime GetBandwidthUpdateDeadline() const;
87 
88   Delegate* delegate_;  // Not owned.
89 
90   // Start time of the detector, handshake deadline = start_time_ +
91   // handshake_timeout_.
92   const QuicTime start_time_;
93 
94   // Handshake timeout. Infinite means handshake has completed.
95   QuicTime::Delta handshake_timeout_;
96 
97   // Time that last packet is received for this connection. Initialized to
98   // start_time_.
99   QuicTime time_of_last_received_packet_;
100 
101   // Time that the first packet gets sent after the received packet. idle
102   // network deadline = std::max(time_of_last_received_packet_,
103   // time_of_first_packet_sent_after_receiving_) + idle_network_timeout_.
104   // Initialized to 0.
105   QuicTime time_of_first_packet_sent_after_receiving_;
106 
107   // Idle network timeout. Infinite means no idle network timeout.
108   QuicTime::Delta idle_network_timeout_;
109 
110   QuicArenaScopedPtr<QuicAlarm> alarm_;
111 
112   bool shorter_idle_timeout_on_sent_packet_ = false;
113 
114   // Whether |StopDetection| has been called.
115   bool stopped_ = false;
116 };
117 
118 }  // namespace quic
119 
120 #endif  // QUICHE_QUIC_CORE_QUIC_IDLE_NETWORK_DETECTOR_H_
121