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 QUIC_EXPORT_PRIVATE QuicIdleNetworkDetector { 27 public: 28 class QUIC_EXPORT_PRIVATE 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 // Called when bandwidth update alarms. 39 virtual void OnBandwidthUpdateTimeout() = 0; 40 }; 41 42 QuicIdleNetworkDetector(Delegate* delegate, QuicTime now, 43 QuicConnectionArena* arena, 44 QuicAlarmFactory* alarm_factory, 45 QuicConnectionContext* context); 46 47 void OnAlarm(); 48 49 // Called to set handshake_timeout_ and idle_network_timeout_. 50 void SetTimeouts(QuicTime::Delta handshake_timeout, 51 QuicTime::Delta idle_network_timeout); 52 53 // Stop the detection once and for all. 54 void StopDetection(); 55 56 // Called when a packet gets sent. 57 void OnPacketSent(QuicTime now, QuicTime::Delta pto_delay); 58 59 // Called when a packet gets received. 60 void OnPacketReceived(QuicTime now); 61 enable_shorter_idle_timeout_on_sent_packet()62 void enable_shorter_idle_timeout_on_sent_packet() { 63 shorter_idle_timeout_on_sent_packet_ = true; 64 } 65 handshake_timeout()66 QuicTime::Delta handshake_timeout() const { return handshake_timeout_; } 67 time_of_last_received_packet()68 QuicTime time_of_last_received_packet() const { 69 return time_of_last_received_packet_; 70 } 71 last_network_activity_time()72 QuicTime last_network_activity_time() const { 73 return std::max(time_of_last_received_packet_, 74 time_of_first_packet_sent_after_receiving_); 75 } 76 idle_network_timeout()77 QuicTime::Delta idle_network_timeout() const { return idle_network_timeout_; } 78 bandwidth_update_timeout()79 QuicTime::Delta bandwidth_update_timeout() const { 80 return bandwidth_update_timeout_; 81 } 82 83 QuicTime GetIdleNetworkDeadline() const; 84 85 private: 86 friend class test::QuicConnectionPeer; 87 friend class test::QuicIdleNetworkDetectorTestPeer; 88 89 void SetAlarm(); 90 91 void MaybeSetAlarmOnSentPacket(QuicTime::Delta pto_delay); 92 93 QuicTime GetBandwidthUpdateDeadline() const; 94 95 Delegate* delegate_; // Not owned. 96 97 // Start time of the detector, handshake deadline = start_time_ + 98 // handshake_timeout_. 99 const QuicTime start_time_; 100 101 // Handshake timeout. Infinite means handshake has completed. 102 QuicTime::Delta handshake_timeout_; 103 104 // Time that last packet is received for this connection. Initialized to 105 // start_time_. 106 QuicTime time_of_last_received_packet_; 107 108 // Time that the first packet gets sent after the received packet. idle 109 // network deadline = std::max(time_of_last_received_packet_, 110 // time_of_first_packet_sent_after_receiving_) + idle_network_timeout_. 111 // Initialized to 0. 112 QuicTime time_of_first_packet_sent_after_receiving_; 113 114 // Idle network timeout. Infinite means no idle network timeout. 115 QuicTime::Delta idle_network_timeout_; 116 117 // Bandwidth update timeout. Infinite means no bandwidth update timeout. 118 QuicTime::Delta bandwidth_update_timeout_; 119 120 QuicArenaScopedPtr<QuicAlarm> alarm_; 121 122 bool shorter_idle_timeout_on_sent_packet_ = false; 123 124 // Whether |StopDetection| has been called. 125 bool stopped_ = false; 126 }; 127 128 } // namespace quic 129 130 #endif // QUICHE_QUIC_CORE_QUIC_IDLE_NETWORK_DETECTOR_H_ 131