blob: 74240539ae78e30a3fa4a5b9ff68d45f7e60e62f [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 teama6ef0a62019-03-07 20:34:33 -05008#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"
dschinazi580d30b2019-04-26 15:05:20 -070010#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
12namespace quic {
13
14class QuicConnection;
15
16namespace test {
17
18// SimpleSessionNotifier implements the basic functionalities of a session, and
19// it manages stream data and control frames.
20class 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);
zhongyifbb25772019-04-10 16:54:08 -070034 // Tries to write PING.
35 void WriteOrBufferPing();
QUICHE teama6ef0a62019-03-07 20:34:33 -050036
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 team9467db02019-05-30 09:38:45 -070068 QuicTime::Delta ack_delay_time,
69 QuicTime receive_timestamp) override;
dschinazi17d42422019-06-18 16:35:07 -070070 void OnStreamFrameRetransmitted(const QuicStreamFrame& /*frame*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050071 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;
zhongyi1b2f7832019-06-14 13:31:34 -070076 bool HasUnackedStreamData() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050077
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_