blob: ca20eb4b2698cebcad115590120e2a6fd991cacf [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 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// The base class for client/server QUIC streams.
6
7// It does not contain the entire interface needed by an application to interact
8// with a QUIC stream. Some parts of the interface must be obtained by
9// accessing the owning session object. A subclass of QuicStream
10// connects the object and the application that generates and consumes the data
11// of the stream.
12
13// The QuicStream object has a dependent QuicStreamSequencer object,
14// which is given the stream frames as they arrive, and provides stream data in
15// order by invoking ProcessRawData().
16
17#ifndef QUICHE_QUIC_CORE_QUIC_STREAM_H_
18#define QUICHE_QUIC_CORE_QUIC_STREAM_H_
19
20#include <cstddef>
21#include <cstdint>
22#include <list>
vasilvv872e7a32019-03-12 16:42:44 -070023#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050024
vasilvvc872ee42020-10-07 19:50:22 -070025#include "absl/strings/string_view.h"
vasilvv7df418b2020-10-13 13:47:09 -070026#include "absl/types/optional.h"
vasilvv53f59f62021-06-09 10:48:48 -070027#include "absl/types/span.h"
QUICHE team5be974e2020-12-29 18:35:24 -050028#include "quic/core/quic_flow_controller.h"
29#include "quic/core/quic_packets.h"
30#include "quic/core/quic_stream_send_buffer.h"
31#include "quic/core/quic_stream_sequencer.h"
32#include "quic/core/quic_types.h"
33#include "quic/core/session_notifier_interface.h"
34#include "quic/core/stream_delegate_interface.h"
35#include "quic/platform/api/quic_export.h"
vasilvv53f59f62021-06-09 10:48:48 -070036#include "quic/platform/api/quic_mem_slice.h"
QUICHE team5be974e2020-12-29 18:35:24 -050037#include "quic/platform/api/quic_mem_slice_span.h"
38#include "quic/platform/api/quic_reference_counted.h"
39#include "spdy/core/spdy_protocol.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050040
41namespace quic {
42
43namespace test {
44class QuicStreamPeer;
45} // namespace test
46
47class QuicSession;
48class QuicStream;
49
50// Buffers frames for a stream until the first byte of that frame arrives.
51class QUIC_EXPORT_PRIVATE PendingStream
52 : public QuicStreamSequencer::StreamInterface {
53 public:
54 PendingStream(QuicStreamId id, QuicSession* session);
55 PendingStream(const PendingStream&) = delete;
56 PendingStream(PendingStream&&) = default;
57 ~PendingStream() override = default;
58
59 // QuicStreamSequencer::StreamInterface
60 void OnDataAvailable() override;
61 void OnFinRead() override;
62 void AddBytesConsumed(QuicByteCount bytes) override;
63 void Reset(QuicRstStreamErrorCode error) override;
renjietang87df0d02020-02-13 11:53:52 -080064 void OnUnrecoverableError(QuicErrorCode error,
65 const std::string& details) override;
mattm55006b02021-01-14 15:09:54 -080066 void OnUnrecoverableError(QuicErrorCode error,
67 QuicIetfTransportErrorCodes ietf_error,
68 const std::string& details) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050069 QuicStreamId id() const override;
fayang528c36b2020-12-23 07:02:09 -080070 ParsedQuicVersion version() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050071
72 // Buffers the contents of |frame|. Frame must have a non-zero offset.
73 // If the data violates flow control, the connection will be closed.
74 void OnStreamFrame(const QuicStreamFrame& frame);
75
76 // Stores the final byte offset from |frame|.
77 // If the final offset violates flow control, the connection will be closed.
78 void OnRstStreamFrame(const QuicRstStreamFrame& frame);
79
80 // Returns the number of bytes read on this stream.
81 uint64_t stream_bytes_read() { return stream_bytes_read_; }
82
renjietang0c558862019-05-08 13:26:23 -070083 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
84
dschinazif1e7b422020-04-30 12:21:28 -070085 void MarkConsumed(QuicByteCount num_bytes);
renjietangbb1c4892019-05-24 15:58:44 -070086
bnc4ff60622019-08-09 18:55:45 -070087 // Tells the sequencer to ignore all incoming data itself and not call
88 // OnDataAvailable().
89 void StopReading();
90
QUICHE teama6ef0a62019-03-07 20:34:33 -050091 private:
92 friend class QuicStream;
93
94 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
95
96 // ID of this stream.
97 QuicStreamId id_;
98
fayang528c36b2020-12-23 07:02:09 -080099 // QUIC version being used by this stream.
100 ParsedQuicVersion version_;
101
renjietang49aec632020-08-31 10:56:28 -0700102 // |stream_delegate_| must outlive this stream.
renjietangf196f6a2020-02-12 12:34:23 -0800103 StreamDelegateInterface* stream_delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104
105 // Bytes read refers to payload bytes only: they do not include framing,
106 // encryption overhead etc.
107 uint64_t stream_bytes_read_;
108
109 // True if a frame containing a fin has been received.
110 bool fin_received_;
111
112 // Connection-level flow controller. Owned by the session.
113 QuicFlowController* connection_flow_controller_;
114 // Stream-level flow controller.
115 QuicFlowController flow_controller_;
116 // Stores the buffered frames.
117 QuicStreamSequencer sequencer_;
118};
119
120class QUIC_EXPORT_PRIVATE QuicStream
121 : public QuicStreamSequencer::StreamInterface {
122 public:
bnc3e004e92020-08-31 07:03:13 -0700123 // Default priority for Google QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 // This is somewhat arbitrary. It's possible, but unlikely, we will either
125 // fail to set a priority client-side, or cancel a stream before stripping the
126 // priority from the wire server-side. In either case, start out with a
bnc5f202512020-02-01 18:43:02 -0800127 // priority in the middle in case of Google QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128 static const spdy::SpdyPriority kDefaultPriority = 3;
129 static_assert(kDefaultPriority ==
130 (spdy::kV3LowestPriority + spdy::kV3HighestPriority) / 2,
131 "Unexpected value of kDefaultPriority");
132
133 // Creates a new stream with stream_id |id| associated with |session|. If
134 // |is_static| is true, then the stream will be given precedence
135 // over other streams when determing what streams should write next.
136 // |type| indicates whether the stream is bidirectional, read unidirectional
137 // or write unidirectional.
138 // TODO(fayang): Remove |type| when IETF stream ID numbering fully kicks in.
139 QuicStream(QuicStreamId id,
140 QuicSession* session,
141 bool is_static,
142 StreamType type);
renjietang49aec632020-08-31 10:56:28 -0700143 QuicStream(PendingStream* pending,
144 QuicSession* session,
145 StreamType type,
146 bool is_static);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 QuicStream(const QuicStream&) = delete;
148 QuicStream& operator=(const QuicStream&) = delete;
149
150 virtual ~QuicStream();
151
bnc3e004e92020-08-31 07:03:13 -0700152 // Default priority for IETF QUIC, defined by the priority extension at
153 // https://httpwg.org/http-extensions/draft-ietf-httpbis-priority.html#urgency.
bnc9e7c08c2020-11-09 11:24:49 -0800154 static const int kDefaultUrgency = 3;
bnc3e004e92020-08-31 07:03:13 -0700155
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156 // QuicStreamSequencer::StreamInterface implementation.
157 QuicStreamId id() const override { return id_; }
fayang528c36b2020-12-23 07:02:09 -0800158 ParsedQuicVersion version() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500159 // Called by the stream subclass after it has consumed the final incoming
160 // data.
161 void OnFinRead() override;
162
163 // Called by the subclass or the sequencer to reset the stream from this
164 // end.
165 void Reset(QuicRstStreamErrorCode error) override;
166
167 // Called by the subclass or the sequencer to close the entire connection from
168 // this end.
renjietang87df0d02020-02-13 11:53:52 -0800169 void OnUnrecoverableError(QuicErrorCode error,
170 const std::string& details) override;
mattm55006b02021-01-14 15:09:54 -0800171 void OnUnrecoverableError(QuicErrorCode error,
172 QuicIetfTransportErrorCodes ietf_error,
173 const std::string& details) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500174
QUICHE teama6ef0a62019-03-07 20:34:33 -0500175 // Called by the session when a (potentially duplicate) stream frame has been
176 // received for this stream.
177 virtual void OnStreamFrame(const QuicStreamFrame& frame);
178
179 // Called by the session when the connection becomes writeable to allow the
180 // stream to write any pending data.
181 virtual void OnCanWrite();
182
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183 // Called by the session when the endpoint receives a RST_STREAM from the
184 // peer.
185 virtual void OnStreamReset(const QuicRstStreamFrame& frame);
186
187 // Called by the session when the endpoint receives or sends a connection
188 // close, and should immediately close the stream.
189 virtual void OnConnectionClosed(QuicErrorCode error,
190 ConnectionCloseSource source);
191
fayang476683a2019-07-25 12:42:16 -0700192 const spdy::SpdyStreamPrecedence& precedence() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193
bnccf09f952020-01-30 17:35:59 -0800194 // Send PRIORITY_UPDATE frame if application protocol supports it.
195 virtual void MaybeSendPriorityUpdateFrame() {}
196
197 // Sets |priority_| to priority. This should only be called before bytes are
198 // written to the server. For a server stream, this is called when a
199 // PRIORITY_UPDATE frame is received. This calls
200 // MaybeSendPriorityUpdateFrame(), which for a client stream might send a
201 // PRIORITY_UPDATE frame.
fayang476683a2019-07-25 12:42:16 -0700202 void SetPriority(const spdy::SpdyStreamPrecedence& precedence);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500203
204 // Returns true if this stream is still waiting for acks of sent data.
205 // This will return false if all data has been acked, or if the stream
206 // is no longer interested in data being acked (which happens when
207 // a stream is reset because of an error).
208 bool IsWaitingForAcks() const;
209
210 // Number of bytes available to read.
dschinazif1e7b422020-04-30 12:21:28 -0700211 QuicByteCount ReadableBytes() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500212
213 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
214 QuicErrorCode connection_error() const { return connection_error_; }
215
216 bool reading_stopped() const {
217 return sequencer_.ignore_read_data() || read_side_closed_;
218 }
219 bool write_side_closed() const { return write_side_closed_; }
QUICHE team9fade462020-10-12 12:54:41 -0700220 bool read_side_closed() const { return read_side_closed_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500221
renjietang647b3cf2020-08-04 13:23:12 -0700222 bool IsZombie() const {
223 return read_side_closed_ && write_side_closed_ && IsWaitingForAcks();
224 }
225
QUICHE teama6ef0a62019-03-07 20:34:33 -0500226 bool rst_received() const { return rst_received_; }
227 bool rst_sent() const { return rst_sent_; }
228 bool fin_received() const { return fin_received_; }
229 bool fin_sent() const { return fin_sent_; }
230 bool fin_outstanding() const { return fin_outstanding_; }
231 bool fin_lost() const { return fin_lost_; }
232
233 uint64_t BufferedDataBytes() const;
234
235 uint64_t stream_bytes_read() const { return stream_bytes_read_; }
236 uint64_t stream_bytes_written() const;
237
238 size_t busy_counter() const { return busy_counter_; }
239 void set_busy_counter(size_t busy_counter) { busy_counter_ = busy_counter; }
240
QUICHE teama6ef0a62019-03-07 20:34:33 -0500241 // Adjust the flow control window according to new offset in |frame|.
242 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
243
244 int num_frames_received() const;
245 int num_duplicate_frames_received() const;
246
renjietang07b2e8d2020-08-10 15:18:24 -0700247 // Flow controller related methods.
248 bool IsFlowControlBlocked() const;
249 QuicStreamOffset highest_received_byte_offset() const;
250 void UpdateReceiveWindowSize(QuicStreamOffset size);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500251
252 // Called when endpoint receives a frame which could increase the highest
253 // offset.
254 // Returns true if the highest offset did increase.
255 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500256
renjietange0f96d72020-04-28 11:25:29 -0700257 // Set the flow controller's send window offset from session config.
renjietang3f68bdf2020-08-10 11:52:30 -0700258 // |was_zero_rtt_rejected| is true if this config is from a rejected IETF QUIC
259 // 0-RTT attempt. Closes the connection and returns false if |new_offset| is
260 // not valid.
261 bool MaybeConfigSendWindowOffset(QuicStreamOffset new_offset,
262 bool was_zero_rtt_rejected);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500263
264 // Returns true if the stream has received either a RST_STREAM or a FIN -
265 // either of which gives a definitive number of bytes which the peer has
266 // sent. If this is not true on deletion of the stream object, the session
267 // must keep track of the stream's byte offset until a definitive final value
268 // arrives.
renjietang6c066562019-11-04 17:05:59 -0800269 bool HasReceivedFinalOffset() const { return fin_received_ || rst_received_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500270
271 // Returns true if the stream has queued data waiting to write.
272 bool HasBufferedData() const;
273
274 // Returns the version of QUIC being used for this stream.
275 QuicTransportVersion transport_version() const;
276
277 // Returns the crypto handshake protocol that was used on this stream's
278 // connection.
279 HandshakeProtocol handshake_protocol() const;
280
281 // Sets the sequencer to consume all incoming data itself and not call
282 // OnDataAvailable().
283 // When the FIN is received, the stream will be notified automatically (via
284 // OnFinRead()) (which may happen during the call of StopReading()).
285 // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and
286 // stop sending stream-level flow-control updates when this end sends FIN.
287 virtual void StopReading();
288
fayang9c41f8b2020-10-30 13:13:06 -0700289 // Sends as much of |data| to the connection on the application encryption
290 // level as the connection will consume, and then buffers any remaining data
291 // in the send buffer. If fin is true: if it is immediately passed on to the
292 // session, write_side_closed() becomes true, otherwise fin_buffered_ becomes
293 // true.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500294 void WriteOrBufferData(
vasilvvc872ee42020-10-07 19:50:22 -0700295 absl::string_view data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500296 bool fin,
297 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
298
fayang9c41f8b2020-10-30 13:13:06 -0700299 // Sends |data| to connection with specified |level|.
300 void WriteOrBufferDataAtLevel(
301 absl::string_view data,
302 bool fin,
303 EncryptionLevel level,
304 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
305
QUICHE teama6ef0a62019-03-07 20:34:33 -0500306 // Adds random padding after the fin is consumed for this stream.
307 void AddRandomPaddingAfterFin();
308
309 // Write |data_length| of data starts at |offset| from send buffer.
310 bool WriteStreamData(QuicStreamOffset offset,
311 QuicByteCount data_length,
312 QuicDataWriter* writer);
313
314 // Called when data [offset, offset + data_length) is acked. |fin_acked|
315 // indicates whether the fin is acked. Returns true and updates
316 // |newly_acked_length| if any new stream data (including fin) gets acked.
317 virtual bool OnStreamFrameAcked(QuicStreamOffset offset,
318 QuicByteCount data_length,
319 bool fin_acked,
320 QuicTime::Delta ack_delay_time,
QUICHE team2f5f30b2020-02-18 08:52:28 -0800321 QuicTime receive_timestamp,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500322 QuicByteCount* newly_acked_length);
323
324 // Called when data [offset, offset + data_length) was retransmitted.
325 // |fin_retransmitted| indicates whether fin was retransmitted.
326 virtual void OnStreamFrameRetransmitted(QuicStreamOffset offset,
327 QuicByteCount data_length,
328 bool fin_retransmitted);
329
330 // Called when data [offset, offset + data_length) is considered as lost.
331 // |fin_lost| indicates whether the fin is considered as lost.
332 virtual void OnStreamFrameLost(QuicStreamOffset offset,
333 QuicByteCount data_length,
334 bool fin_lost);
335
336 // Called to retransmit outstanding portion in data [offset, offset +
renjietang4d992bf2020-03-03 13:01:55 -0800337 // data_length) and |fin| with Transmission |type|.
338 // Returns true if all data gets retransmitted.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500339 virtual bool RetransmitStreamData(QuicStreamOffset offset,
340 QuicByteCount data_length,
renjietang4d992bf2020-03-03 13:01:55 -0800341 bool fin,
342 TransmissionType type);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500343
344 // Sets deadline of this stream to be now + |ttl|, returns true if the setting
345 // succeeds.
346 bool MaybeSetTtl(QuicTime::Delta ttl);
347
vasilvv53f59f62021-06-09 10:48:48 -0700348 // Commits data into the stream write buffer, and potentially sends it over
349 // the wire. This method has all-or-nothing semantics: if the write buffer is
350 // not full, all of the memslices in |span| are moved into it; otherwise,
351 // nothing happens.
352 // TODO(vasilvv): deprecate and remove QuicMemSliceSpan version.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500353 QuicConsumedData WriteMemSlices(QuicMemSliceSpan span, bool fin);
vasilvv53f59f62021-06-09 10:48:48 -0700354 QuicConsumedData WriteMemSlices(absl::Span<QuicMemSlice> span, bool fin);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500355
356 // Returns true if any stream data is lost (including fin) and needs to be
357 // retransmitted.
358 virtual bool HasPendingRetransmission() const;
359
360 // Returns true if any portion of data [offset, offset + data_length) is
361 // outstanding or fin is outstanding (if |fin| is true). Returns false
362 // otherwise.
363 bool IsStreamFrameOutstanding(QuicStreamOffset offset,
364 QuicByteCount data_length,
365 bool fin) const;
366
367 StreamType type() const { return type_; }
368
renjietanged49cb92020-03-03 14:30:53 -0800369 // Handle received StopSending frame. Returns true if the processing finishes
370 // gracefully.
bnc187eea32020-09-02 12:16:15 -0700371 virtual bool OnStopSending(QuicRstStreamErrorCode code);
renjietangd9762282019-11-11 17:11:18 -0800372
renjietangfbeb5bf2019-04-19 15:06:20 -0700373 // Returns true if the stream is static.
374 bool is_static() const { return is_static_; }
375
fayangbe6d6642020-04-16 14:15:34 -0700376 bool was_draining() const { return was_draining_; }
377
bnc5f202512020-02-01 18:43:02 -0800378 static spdy::SpdyStreamPrecedence CalculateDefaultPriority(
379 const QuicSession* session);
380
fayanga330b7c2020-09-10 08:15:39 -0700381 QuicTime creation_time() const { return creation_time_; }
382
383 bool fin_buffered() const { return fin_buffered_; }
384
vasilvv547041f2021-03-17 17:29:58 -0700385 // True if buffered data in send buffer is below buffered_data_threshold_.
386 bool CanWriteNewData() const;
387
vasilvv847b0142021-03-24 10:32:35 -0700388 // Called immediately after the stream is created from a pending stream,
389 // indicating it can start processing data.
390 void OnStreamCreatedFromPendingStream();
391
QUICHE teama6ef0a62019-03-07 20:34:33 -0500392 protected:
QUICHE teama6ef0a62019-03-07 20:34:33 -0500393 // Called when data of [offset, offset + data_length] is buffered in send
394 // buffer.
395 virtual void OnDataBuffered(
dschinazi17d42422019-06-18 16:35:07 -0700396 QuicStreamOffset /*offset*/,
397 QuicByteCount /*data_length*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500398 const QuicReferenceCountedPointer<QuicAckListenerInterface>&
dschinazi17d42422019-06-18 16:35:07 -0700399 /*ack_listener*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500400
fayangb8f83442020-06-01 12:09:17 -0700401 // Called just before the object is destroyed.
402 // The object should not be accessed after OnClose is called.
403 // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor
404 // a RST_STREAM has been sent.
405 virtual void OnClose();
406
QUICHE teama6ef0a62019-03-07 20:34:33 -0500407 // True if buffered data in send buffer is still below
408 // buffered_data_threshold_ even after writing |length| bytes.
409 bool CanWriteNewDataAfterData(QuicByteCount length) const;
410
411 // Called when upper layer can write new data.
412 virtual void OnCanWriteNewData() {}
413
414 // Called when |bytes_consumed| bytes has been consumed.
dschinazif1e7b422020-04-30 12:21:28 -0700415 virtual void OnStreamDataConsumed(QuicByteCount bytes_consumed);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500416
ianswett93637202019-04-03 08:05:29 -0700417 // Called by the stream sequencer as bytes are consumed from the buffer.
418 // If the receive window has dropped below the threshold, then send a
419 // WINDOW_UPDATE frame.
420 void AddBytesConsumed(QuicByteCount bytes) override;
421
QUICHE teama6ef0a62019-03-07 20:34:33 -0500422 // Writes pending retransmissions if any.
423 virtual void WritePendingRetransmission();
424
425 // This is called when stream tries to retransmit data after deadline_. Make
426 // this virtual so that subclasses can implement their own logics.
427 virtual void OnDeadlinePassed();
428
fayang3a51d1a2020-04-16 13:42:08 -0700429 // Called to set fin_sent_. This is only used by Google QUIC while body is
430 // empty.
431 void SetFinSent();
432
renjietang052df7c2020-10-13 14:46:09 -0700433 // Send STOP_SENDING if it hasn't been sent yet.
434 void MaybeSendStopSending(QuicRstStreamErrorCode error);
435
436 // Send RESET_STREAM if it hasn't been sent yet.
437 void MaybeSendRstStream(QuicRstStreamErrorCode error);
438
renjietangf00e1e12020-08-26 10:51:15 -0700439 // Close the write side of the socket. Further writes will fail.
440 // Can be called by the subclass or internally.
441 // Does not send a FIN. May cause the stream to be closed.
442 virtual void CloseWriteSide();
443
renjietangdf2240d2020-07-17 12:17:00 -0700444 void set_rst_received(bool rst_received) { rst_received_ = rst_received; }
445 void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; }
446
renjietangf196f6a2020-02-12 12:34:23 -0800447 StreamDelegateInterface* stream_delegate() { return stream_delegate_; }
448
QUICHE teama6ef0a62019-03-07 20:34:33 -0500449 const QuicSession* session() const { return session_; }
450 QuicSession* session() { return session_; }
451
452 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
453 QuicStreamSequencer* sequencer() { return &sequencer_; }
454
455 void DisableConnectionFlowControlForThisStream() {
456 stream_contributes_to_connection_flow_control_ = false;
457 }
458
459 const QuicIntervalSet<QuicStreamOffset>& bytes_acked() const;
460
461 const QuicStreamSendBuffer& send_buffer() const { return send_buffer_; }
462
463 QuicStreamSendBuffer& send_buffer() { return send_buffer_; }
464
danzh2475cc82021-02-19 14:54:15 -0800465 // Return the current flow control send window in bytes.
466 absl::optional<QuicByteCount> GetSendWindow() const;
danzh6fec8e92021-02-24 07:53:56 -0800467 absl::optional<QuicByteCount> GetReceiveWindow() const;
danzh2475cc82021-02-19 14:54:15 -0800468
QUICHE teama6ef0a62019-03-07 20:34:33 -0500469 private:
470 friend class test::QuicStreamPeer;
471 friend class QuicStreamUtils;
472
vasilvv53f59f62021-06-09 10:48:48 -0700473 // Wraps around either QuicMemSliceSpan or absl::Span<QuicMemSlice>.
474 // TODO(vasilvv): delete this after QuicMemSliceSpan is gone.
475 class QUIC_EXPORT_PRIVATE MemSliceSpanWrapper {
476 public:
477 explicit MemSliceSpanWrapper(QuicMemSliceSpan span) : old_(span) {}
478 explicit MemSliceSpanWrapper(absl::Span<QuicMemSlice> span) : new_(span) {}
479
480 bool empty() { return old_.has_value() ? old_->empty() : new_.empty(); }
481 size_t SaveTo(QuicStreamSendBuffer& send_buffer) {
482 if (old_.has_value()) {
483 return send_buffer.SaveMemSliceSpan(*old_);
484 }
485 return send_buffer.SaveMemSliceSpan(new_);
486 }
487
488 private:
489 absl::optional<QuicMemSliceSpan> old_;
490 absl::Span<QuicMemSlice> new_;
491 };
492
QUICHE teama6ef0a62019-03-07 20:34:33 -0500493 QuicStream(QuicStreamId id,
494 QuicSession* session,
495 QuicStreamSequencer sequencer,
496 bool is_static,
497 StreamType type,
498 uint64_t stream_bytes_read,
499 bool fin_received,
vasilvv7df418b2020-10-13 13:47:09 -0700500 absl::optional<QuicFlowController> flow_controller,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500501 QuicFlowController* connection_flow_controller);
502
QUICHE teama6ef0a62019-03-07 20:34:33 -0500503 // Calls MaybeSendBlocked on the stream's flow controller and the connection
504 // level flow controller. If the stream is flow control blocked by the
505 // connection-level flow controller but not by the stream-level flow
506 // controller, marks this stream as connection-level write blocked.
507 void MaybeSendBlocked();
508
fayang9c41f8b2020-10-30 13:13:06 -0700509 // Write buffered data in send buffer.
510 // TODO(fayang): Change absl::optional<EncryptionLevel> to EncryptionLevel
511 // when deprecating quic_use_write_or_buffer_data_at_level.
512 void WriteBufferedData(absl::optional<EncryptionLevel> level);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500513
renjietangf00e1e12020-08-26 10:51:15 -0700514 // Close the read side of the stream. May cause the stream to be closed.
515 void CloseReadSide();
516
ianswett93637202019-04-03 08:05:29 -0700517 // Called when bytes are sent to the peer.
518 void AddBytesSent(QuicByteCount bytes);
519
fayang9c41f8b2020-10-30 13:13:06 -0700520 // TODO(fayang): Inline this function when deprecating
521 // quic_use_write_or_buffer_data_at_level.
522 void WriteOrBufferDataInner(
523 absl::string_view data,
524 bool fin,
525 absl::optional<EncryptionLevel> level,
526 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
527
QUICHE teama6ef0a62019-03-07 20:34:33 -0500528 // Returns true if deadline_ has passed.
529 bool HasDeadlinePassed() const;
530
vasilvv53f59f62021-06-09 10:48:48 -0700531 QuicConsumedData WriteMemSlicesInner(MemSliceSpanWrapper span, bool fin);
532
QUICHE teama6ef0a62019-03-07 20:34:33 -0500533 QuicStreamSequencer sequencer_;
534 QuicStreamId id_;
535 // Pointer to the owning QuicSession object.
renjietangf196f6a2020-02-12 12:34:23 -0800536 // TODO(b/136274541): Remove session pointer from streams.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500537 QuicSession* session_;
renjietangf196f6a2020-02-12 12:34:23 -0800538 StreamDelegateInterface* stream_delegate_;
fayang476683a2019-07-25 12:42:16 -0700539 // The precedence of the stream, once parsed.
540 spdy::SpdyStreamPrecedence precedence_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500541 // Bytes read refers to payload bytes only: they do not include framing,
542 // encryption overhead etc.
543 uint64_t stream_bytes_read_;
544
545 // Stream error code received from a RstStreamFrame or error code sent by the
546 // visitor or sequencer in the RstStreamFrame.
547 QuicRstStreamErrorCode stream_error_;
548 // Connection error code due to which the stream was closed. |stream_error_|
549 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
550 // should check |connection_error_|.
551 QuicErrorCode connection_error_;
552
553 // True if the read side is closed and further frames should be rejected.
554 bool read_side_closed_;
555 // True if the write side is closed, and further writes should fail.
556 bool write_side_closed_;
557
558 // True if the subclass has written a FIN with WriteOrBufferData, but it was
559 // buffered in queued_data_ rather than being sent to the session.
560 bool fin_buffered_;
561 // True if a FIN has been sent to the session.
562 bool fin_sent_;
563 // True if a FIN is waiting to be acked.
564 bool fin_outstanding_;
565 // True if a FIN is lost.
566 bool fin_lost_;
567
568 // True if this stream has received (and the sequencer has accepted) a
569 // StreamFrame with the FIN set.
570 bool fin_received_;
571
572 // True if an RST_STREAM has been sent to the session.
573 // In combination with fin_sent_, used to ensure that a FIN and/or a
574 // RST_STREAM is always sent to terminate the stream.
575 bool rst_sent_;
576
577 // True if this stream has received a RST_STREAM frame.
578 bool rst_received_;
579
renjietang052df7c2020-10-13 14:46:09 -0700580 // True if the stream has sent STOP_SENDING to the session.
581 bool stop_sending_sent_;
582
vasilvv7df418b2020-10-13 13:47:09 -0700583 absl::optional<QuicFlowController> flow_controller_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500584
585 // The connection level flow controller. Not owned.
586 QuicFlowController* connection_flow_controller_;
587
588 // Special streams, such as the crypto and headers streams, do not respect
589 // connection level flow control limits (but are stream level flow control
590 // limited).
591 bool stream_contributes_to_connection_flow_control_;
592
593 // A counter incremented when OnCanWrite() is called and no progress is made.
594 // For debugging only.
595 size_t busy_counter_;
596
597 // Indicates whether paddings will be added after the fin is consumed for this
598 // stream.
599 bool add_random_padding_after_fin_;
600
601 // Send buffer of this stream. Send buffer is cleaned up when data gets acked
602 // or discarded.
603 QuicStreamSendBuffer send_buffer_;
604
bnc5a401422019-06-18 04:32:32 -0700605 // Latched value of quic_buffered_data_threshold.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500606 const QuicByteCount buffered_data_threshold_;
607
608 // If true, then this stream has precedence over other streams for write
609 // scheduling.
610 const bool is_static_;
611
612 // If initialized, reset this stream at this deadline.
613 QuicTime deadline_;
614
fayang6dfe58d2020-06-01 08:02:47 -0700615 // True if this stream has entered draining state.
fayangbe6d6642020-04-16 14:15:34 -0700616 bool was_draining_;
617
QUICHE teama6ef0a62019-03-07 20:34:33 -0500618 // Indicates whether this stream is bidirectional, read unidirectional or
619 // write unidirectional.
620 const StreamType type_;
renjietangff3d3a32020-02-13 15:13:51 -0800621
fayanga330b7c2020-09-10 08:15:39 -0700622 // Creation time of this stream, as reported by the QuicClock.
623 const QuicTime creation_time_;
624
renjietangff3d3a32020-02-13 15:13:51 -0800625 Perspective perspective_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500626};
627
628} // namespace quic
629
630#endif // QUICHE_QUIC_CORE_QUIC_STREAM_H_