1 // Copyright (c) 2018 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_TEST_TOOLS_SIMPLE_SESSION_NOTIFIER_H_ 6 #define QUICHE_QUIC_TEST_TOOLS_SIMPLE_SESSION_NOTIFIER_H_ 7 8 #include "net/third_party/quiche/src/quic/core/quic_circular_deque.h" 9 #include "net/third_party/quiche/src/quic/core/quic_interval_set.h" 10 #include "net/third_party/quiche/src/quic/core/session_notifier_interface.h" 11 #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" 12 13 namespace quic { 14 15 class QuicConnection; 16 17 namespace test { 18 19 // SimpleSessionNotifier implements the basic functionalities of a session, and 20 // it manages stream data and control frames. 21 class SimpleSessionNotifier : public SessionNotifierInterface { 22 public: 23 explicit SimpleSessionNotifier(QuicConnection* connection); 24 ~SimpleSessionNotifier() override; 25 26 // Tries to write stream data and returns data consumed. 27 QuicConsumedData WriteOrBufferData(QuicStreamId id, 28 QuicByteCount data_length, 29 StreamSendingState state); 30 31 // Tries to write RST_STREAM_FRAME. 32 void WriteOrBufferRstStream(QuicStreamId id, 33 QuicRstStreamErrorCode error, 34 QuicStreamOffset bytes_written); 35 // Tries to write PING. 36 void WriteOrBufferPing(); 37 38 // Tries to write CRYPTO data and returns the number of bytes written. 39 size_t WriteCryptoData(EncryptionLevel level, 40 QuicByteCount data_length, 41 QuicStreamOffset offset); 42 43 // Neuters unencrypted data of crypto stream. 44 void NeuterUnencryptedData(); 45 46 // Called when connection_ becomes writable. 47 void OnCanWrite(); 48 49 // Called to reset stream. 50 void OnStreamReset(QuicStreamId id, QuicRstStreamErrorCode error); 51 52 // Returns true if there are 1) unsent control frames and stream data, or 2) 53 // lost control frames and stream data. 54 bool WillingToWrite() const; 55 56 // Number of sent stream bytes. Please note, this does not count 57 // retransmissions. 58 QuicByteCount StreamBytesSent() const; 59 60 // Number of stream bytes waiting to be sent for the first time. 61 QuicByteCount StreamBytesToSend() const; 62 63 // Returns true if there is any stream data waiting to be sent for the first 64 // time. 65 bool HasBufferedStreamData() const; 66 67 // Returns true if stream |id| has any outstanding data. 68 bool StreamIsWaitingForAcks(QuicStreamId id) const; 69 70 // SessionNotifierInterface methods: 71 bool OnFrameAcked(const QuicFrame& frame, 72 QuicTime::Delta ack_delay_time, 73 QuicTime receive_timestamp) override; OnStreamFrameRetransmitted(const QuicStreamFrame &)74 void OnStreamFrameRetransmitted(const QuicStreamFrame& /*frame*/) override {} 75 void OnFrameLost(const QuicFrame& frame) override; 76 void RetransmitFrames(const QuicFrames& frames, 77 TransmissionType type) override; 78 bool IsFrameOutstanding(const QuicFrame& frame) const override; 79 bool HasUnackedCryptoData() const override; 80 bool HasUnackedStreamData() const override; 81 82 private: 83 struct StreamState { 84 StreamState(); 85 ~StreamState(); 86 87 // Total number of bytes. 88 QuicByteCount bytes_total; 89 // Number of sent bytes. 90 QuicByteCount bytes_sent; 91 // Record of acked offsets. 92 QuicIntervalSet<QuicStreamOffset> bytes_acked; 93 // Data considered as lost and needs to be retransmitted. 94 QuicIntervalSet<QuicStreamOffset> pending_retransmissions; 95 96 bool fin_buffered; 97 bool fin_sent; 98 bool fin_outstanding; 99 bool fin_lost; 100 }; 101 102 friend std::ostream& operator<<(std::ostream& os, const StreamState& s); 103 104 using StreamMap = QuicUnorderedMap<QuicStreamId, StreamState>; 105 106 void OnStreamDataConsumed(QuicStreamId id, 107 QuicStreamOffset offset, 108 QuicByteCount data_length, 109 bool fin); 110 111 bool OnControlFrameAcked(const QuicFrame& frame); 112 113 void OnControlFrameLost(const QuicFrame& frame); 114 115 bool RetransmitLostControlFrames(); 116 117 bool RetransmitLostCryptoData(); 118 119 bool RetransmitLostStreamData(); 120 121 bool WriteBufferedControlFrames(); 122 123 bool IsControlFrameOutstanding(const QuicFrame& frame) const; 124 125 bool HasBufferedControlFrames() const; 126 127 bool HasLostStreamData() const; 128 129 bool StreamHasBufferedData(QuicStreamId id) const; 130 131 QuicCircularDeque<QuicFrame> control_frames_; 132 133 QuicLinkedHashMap<QuicControlFrameId, bool> lost_control_frames_; 134 135 // Id of latest saved control frame. 0 if no control frame has been saved. 136 QuicControlFrameId last_control_frame_id_; 137 138 // The control frame at the 0th index of control_frames_. 139 QuicControlFrameId least_unacked_; 140 141 // ID of the least unsent control frame. 142 QuicControlFrameId least_unsent_; 143 144 StreamMap stream_map_; 145 146 // Transferred crypto bytes according to encryption levels. 147 QuicIntervalSet<QuicStreamOffset> 148 crypto_bytes_transferred_[NUM_ENCRYPTION_LEVELS]; 149 150 StreamState crypto_state_[NUM_ENCRYPTION_LEVELS]; 151 152 QuicConnection* connection_; 153 }; 154 155 } // namespace test 156 157 } // namespace quic 158 159 #endif // QUICHE_QUIC_TEST_TOOLS_SIMPLE_SESSION_NOTIFIER_H_ 160