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 | |
| 8 | #include "testing/gmock/include/gmock/gmock.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 | |
| 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); |
| 34 | |
| 35 | // Tries to write CRYPTO data and returns the number of bytes written. |
| 36 | size_t WriteCryptoData(EncryptionLevel level, |
| 37 | QuicByteCount data_length, |
| 38 | QuicStreamOffset offset); |
| 39 | |
| 40 | // Neuters unencrypted data of crypto stream. |
| 41 | void NeuterUnencryptedData(); |
| 42 | |
| 43 | // Called when connection_ becomes writable. |
| 44 | void OnCanWrite(); |
| 45 | |
| 46 | // Returns true if there are 1) unsent control frames and stream data, or 2) |
| 47 | // lost control frames and stream data. |
| 48 | bool WillingToWrite() const; |
| 49 | |
| 50 | // Number of sent stream bytes. Please note, this does not count |
| 51 | // retransmissions. |
| 52 | QuicByteCount StreamBytesSent() const; |
| 53 | |
| 54 | // Number of stream bytes waiting to be sent for the first time. |
| 55 | QuicByteCount StreamBytesToSend() const; |
| 56 | |
| 57 | // Returns true if there is any stream data waiting to be sent for the first |
| 58 | // time. |
| 59 | bool HasBufferedStreamData() const; |
| 60 | |
| 61 | // Returns true if stream |id| has any outstanding data. |
| 62 | bool StreamIsWaitingForAcks(QuicStreamId id) const; |
| 63 | |
| 64 | // SessionNotifierInterface methods: |
| 65 | bool OnFrameAcked(const QuicFrame& frame, |
| 66 | QuicTime::Delta ack_delay_time) override; |
| 67 | void OnStreamFrameRetransmitted(const QuicStreamFrame& frame) override {} |
| 68 | void OnFrameLost(const QuicFrame& frame) override; |
| 69 | void RetransmitFrames(const QuicFrames& frames, |
| 70 | TransmissionType type) override; |
| 71 | bool IsFrameOutstanding(const QuicFrame& frame) const override; |
| 72 | bool HasUnackedCryptoData() const override; |
| 73 | |
| 74 | private: |
| 75 | struct StreamState { |
| 76 | StreamState(); |
| 77 | ~StreamState(); |
| 78 | |
| 79 | // Total number of bytes. |
| 80 | QuicByteCount bytes_total; |
| 81 | // Number of sent bytes. |
| 82 | QuicByteCount bytes_sent; |
| 83 | // Record of acked offsets. |
| 84 | QuicIntervalSet<QuicStreamOffset> bytes_acked; |
| 85 | // Data considered as lost and needs to be retransmitted. |
| 86 | QuicIntervalSet<QuicStreamOffset> pending_retransmissions; |
| 87 | |
| 88 | bool fin_buffered; |
| 89 | bool fin_sent; |
| 90 | bool fin_outstanding; |
| 91 | bool fin_lost; |
| 92 | }; |
| 93 | |
| 94 | friend std::ostream& operator<<(std::ostream& os, const StreamState& s); |
| 95 | |
| 96 | using StreamMap = QuicUnorderedMap<QuicStreamId, StreamState>; |
| 97 | |
| 98 | void OnStreamDataConsumed(QuicStreamId id, |
| 99 | QuicStreamOffset offset, |
| 100 | QuicByteCount data_length, |
| 101 | bool fin); |
| 102 | |
| 103 | bool OnControlFrameAcked(const QuicFrame& frame); |
| 104 | |
| 105 | void OnControlFrameLost(const QuicFrame& frame); |
| 106 | |
| 107 | bool RetransmitLostControlFrames(); |
| 108 | |
| 109 | bool RetransmitLostCryptoData(); |
| 110 | |
| 111 | bool RetransmitLostStreamData(); |
| 112 | |
| 113 | bool WriteBufferedControlFrames(); |
| 114 | |
| 115 | bool IsControlFrameOutstanding(const QuicFrame& frame) const; |
| 116 | |
| 117 | bool HasBufferedControlFrames() const; |
| 118 | |
| 119 | bool HasLostStreamData() const; |
| 120 | |
| 121 | bool StreamHasBufferedData(QuicStreamId id) const; |
| 122 | |
| 123 | QuicDeque<QuicFrame> control_frames_; |
| 124 | |
| 125 | QuicLinkedHashMap<QuicControlFrameId, bool> lost_control_frames_; |
| 126 | |
| 127 | // Id of latest saved control frame. 0 if no control frame has been saved. |
| 128 | QuicControlFrameId last_control_frame_id_; |
| 129 | |
| 130 | // The control frame at the 0th index of control_frames_. |
| 131 | QuicControlFrameId least_unacked_; |
| 132 | |
| 133 | // ID of the least unsent control frame. |
| 134 | QuicControlFrameId least_unsent_; |
| 135 | |
| 136 | StreamMap stream_map_; |
| 137 | |
| 138 | // Transferred crypto bytes according to encryption levels. |
| 139 | QuicIntervalSet<QuicStreamOffset> |
| 140 | crypto_bytes_transferred_[NUM_ENCRYPTION_LEVELS]; |
| 141 | |
| 142 | StreamState crypto_state_[NUM_ENCRYPTION_LEVELS]; |
| 143 | |
| 144 | QuicConnection* connection_; |
| 145 | }; |
| 146 | |
| 147 | } // namespace test |
| 148 | |
| 149 | } // namespace quic |
| 150 | |
| 151 | #endif // QUICHE_QUIC_TEST_TOOLS_SIMPLE_SESSION_NOTIFIER_H_ |