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