QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 23 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/core/quic_flow_controller.h" |
| 26 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 27 | #include "net/third_party/quiche/src/quic/core/quic_stream_send_buffer.h" |
| 28 | #include "net/third_party/quiche/src/quic/core/quic_stream_sequencer.h" |
| 29 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 30 | #include "net/third_party/quiche/src/quic/core/session_notifier_interface.h" |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 31 | #include "net/third_party/quiche/src/quic/core/stream_delegate_interface.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 33 | #include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_span.h" |
| 34 | #include "net/third_party/quiche/src/quic/platform/api/quic_reference_counted.h" |
dmcardle | 15db688 | 2020-02-26 12:55:36 -0800 | [diff] [blame] | 35 | #include "net/third_party/quiche/src/common/platform/api/quiche_optional.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 36 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 37 | #include "net/third_party/quiche/src/spdy/core/spdy_protocol.h" |
| 38 | |
| 39 | namespace quic { |
| 40 | |
| 41 | namespace test { |
| 42 | class QuicStreamPeer; |
| 43 | } // namespace test |
| 44 | |
| 45 | class QuicSession; |
| 46 | class QuicStream; |
| 47 | |
| 48 | // Buffers frames for a stream until the first byte of that frame arrives. |
| 49 | class QUIC_EXPORT_PRIVATE PendingStream |
| 50 | : public QuicStreamSequencer::StreamInterface { |
| 51 | public: |
| 52 | PendingStream(QuicStreamId id, QuicSession* session); |
| 53 | PendingStream(const PendingStream&) = delete; |
| 54 | PendingStream(PendingStream&&) = default; |
| 55 | ~PendingStream() override = default; |
| 56 | |
| 57 | // QuicStreamSequencer::StreamInterface |
| 58 | void OnDataAvailable() override; |
| 59 | void OnFinRead() override; |
| 60 | void AddBytesConsumed(QuicByteCount bytes) override; |
| 61 | void Reset(QuicRstStreamErrorCode error) override; |
renjietang | 87df0d0 | 2020-02-13 11:53:52 -0800 | [diff] [blame] | 62 | void OnUnrecoverableError(QuicErrorCode error, |
| 63 | const std::string& details) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 64 | QuicStreamId id() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | |
| 66 | // Buffers the contents of |frame|. Frame must have a non-zero offset. |
| 67 | // If the data violates flow control, the connection will be closed. |
| 68 | void OnStreamFrame(const QuicStreamFrame& frame); |
| 69 | |
| 70 | // Stores the final byte offset from |frame|. |
| 71 | // If the final offset violates flow control, the connection will be closed. |
| 72 | void OnRstStreamFrame(const QuicRstStreamFrame& frame); |
| 73 | |
| 74 | // Returns the number of bytes read on this stream. |
| 75 | uint64_t stream_bytes_read() { return stream_bytes_read_; } |
| 76 | |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 77 | const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
| 78 | |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 79 | void MarkConsumed(QuicByteCount num_bytes); |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 80 | |
bnc | 4ff6062 | 2019-08-09 18:55:45 -0700 | [diff] [blame] | 81 | // Tells the sequencer to ignore all incoming data itself and not call |
| 82 | // OnDataAvailable(). |
| 83 | void StopReading(); |
| 84 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | private: |
| 86 | friend class QuicStream; |
| 87 | |
| 88 | bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); |
| 89 | |
| 90 | // ID of this stream. |
| 91 | QuicStreamId id_; |
| 92 | |
| 93 | // Session which owns this. |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 94 | // TODO(b/136274541): Remove session pointer from streams. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | QuicSession* session_; |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 96 | StreamDelegateInterface* stream_delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | |
| 98 | // Bytes read refers to payload bytes only: they do not include framing, |
| 99 | // encryption overhead etc. |
| 100 | uint64_t stream_bytes_read_; |
| 101 | |
| 102 | // True if a frame containing a fin has been received. |
| 103 | bool fin_received_; |
| 104 | |
| 105 | // Connection-level flow controller. Owned by the session. |
| 106 | QuicFlowController* connection_flow_controller_; |
| 107 | // Stream-level flow controller. |
| 108 | QuicFlowController flow_controller_; |
| 109 | // Stores the buffered frames. |
| 110 | QuicStreamSequencer sequencer_; |
| 111 | }; |
| 112 | |
| 113 | class QUIC_EXPORT_PRIVATE QuicStream |
| 114 | : public QuicStreamSequencer::StreamInterface { |
| 115 | public: |
bnc | 3e004e9 | 2020-08-31 07:03:13 -0700 | [diff] [blame^] | 116 | // Default priority for Google QUIC. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | // This is somewhat arbitrary. It's possible, but unlikely, we will either |
| 118 | // fail to set a priority client-side, or cancel a stream before stripping the |
| 119 | // priority from the wire server-side. In either case, start out with a |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 120 | // priority in the middle in case of Google QUIC. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | static const spdy::SpdyPriority kDefaultPriority = 3; |
| 122 | static_assert(kDefaultPriority == |
| 123 | (spdy::kV3LowestPriority + spdy::kV3HighestPriority) / 2, |
| 124 | "Unexpected value of kDefaultPriority"); |
| 125 | |
| 126 | // Creates a new stream with stream_id |id| associated with |session|. If |
| 127 | // |is_static| is true, then the stream will be given precedence |
| 128 | // over other streams when determing what streams should write next. |
| 129 | // |type| indicates whether the stream is bidirectional, read unidirectional |
| 130 | // or write unidirectional. |
| 131 | // TODO(fayang): Remove |type| when IETF stream ID numbering fully kicks in. |
| 132 | QuicStream(QuicStreamId id, |
| 133 | QuicSession* session, |
| 134 | bool is_static, |
| 135 | StreamType type); |
renjietang | baea59c | 2019-05-29 15:08:14 -0700 | [diff] [blame] | 136 | QuicStream(PendingStream* pending, StreamType type, bool is_static); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | QuicStream(const QuicStream&) = delete; |
| 138 | QuicStream& operator=(const QuicStream&) = delete; |
| 139 | |
| 140 | virtual ~QuicStream(); |
| 141 | |
bnc | 3e004e9 | 2020-08-31 07:03:13 -0700 | [diff] [blame^] | 142 | // Default priority for IETF QUIC, defined by the priority extension at |
| 143 | // https://httpwg.org/http-extensions/draft-ietf-httpbis-priority.html#urgency. |
| 144 | // TODO(bnc): Remove this method and reinstate static const int |
| 145 | // kDefaultUrgency member when removing quic_http3_new_default_urgency_value. |
| 146 | static int DefaultUrgency(); |
| 147 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | // QuicStreamSequencer::StreamInterface implementation. |
| 149 | QuicStreamId id() const override { return id_; } |
| 150 | // Called by the stream subclass after it has consumed the final incoming |
| 151 | // data. |
| 152 | void OnFinRead() override; |
| 153 | |
| 154 | // Called by the subclass or the sequencer to reset the stream from this |
| 155 | // end. |
| 156 | void Reset(QuicRstStreamErrorCode error) override; |
| 157 | |
| 158 | // Called by the subclass or the sequencer to close the entire connection from |
| 159 | // this end. |
renjietang | 87df0d0 | 2020-02-13 11:53:52 -0800 | [diff] [blame] | 160 | void OnUnrecoverableError(QuicErrorCode error, |
| 161 | const std::string& details) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 163 | // Called by the session when a (potentially duplicate) stream frame has been |
| 164 | // received for this stream. |
| 165 | virtual void OnStreamFrame(const QuicStreamFrame& frame); |
| 166 | |
| 167 | // Called by the session when the connection becomes writeable to allow the |
| 168 | // stream to write any pending data. |
| 169 | virtual void OnCanWrite(); |
| 170 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 171 | // Called by the session when the endpoint receives a RST_STREAM from the |
| 172 | // peer. |
| 173 | virtual void OnStreamReset(const QuicRstStreamFrame& frame); |
| 174 | |
| 175 | // Called by the session when the endpoint receives or sends a connection |
| 176 | // close, and should immediately close the stream. |
| 177 | virtual void OnConnectionClosed(QuicErrorCode error, |
| 178 | ConnectionCloseSource source); |
| 179 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 180 | const spdy::SpdyStreamPrecedence& precedence() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 181 | |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 182 | // Send PRIORITY_UPDATE frame if application protocol supports it. |
| 183 | virtual void MaybeSendPriorityUpdateFrame() {} |
| 184 | |
| 185 | // Sets |priority_| to priority. This should only be called before bytes are |
| 186 | // written to the server. For a server stream, this is called when a |
| 187 | // PRIORITY_UPDATE frame is received. This calls |
| 188 | // MaybeSendPriorityUpdateFrame(), which for a client stream might send a |
| 189 | // PRIORITY_UPDATE frame. |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 190 | void SetPriority(const spdy::SpdyStreamPrecedence& precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 191 | |
| 192 | // Returns true if this stream is still waiting for acks of sent data. |
| 193 | // This will return false if all data has been acked, or if the stream |
| 194 | // is no longer interested in data being acked (which happens when |
| 195 | // a stream is reset because of an error). |
| 196 | bool IsWaitingForAcks() const; |
| 197 | |
| 198 | // Number of bytes available to read. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 199 | QuicByteCount ReadableBytes() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 200 | |
| 201 | QuicRstStreamErrorCode stream_error() const { return stream_error_; } |
| 202 | QuicErrorCode connection_error() const { return connection_error_; } |
| 203 | |
| 204 | bool reading_stopped() const { |
| 205 | return sequencer_.ignore_read_data() || read_side_closed_; |
| 206 | } |
| 207 | bool write_side_closed() const { return write_side_closed_; } |
| 208 | |
renjietang | 647b3cf | 2020-08-04 13:23:12 -0700 | [diff] [blame] | 209 | bool IsZombie() const { |
| 210 | return read_side_closed_ && write_side_closed_ && IsWaitingForAcks(); |
| 211 | } |
| 212 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | bool rst_received() const { return rst_received_; } |
| 214 | bool rst_sent() const { return rst_sent_; } |
| 215 | bool fin_received() const { return fin_received_; } |
| 216 | bool fin_sent() const { return fin_sent_; } |
| 217 | bool fin_outstanding() const { return fin_outstanding_; } |
| 218 | bool fin_lost() const { return fin_lost_; } |
| 219 | |
| 220 | uint64_t BufferedDataBytes() const; |
| 221 | |
| 222 | uint64_t stream_bytes_read() const { return stream_bytes_read_; } |
| 223 | uint64_t stream_bytes_written() const; |
| 224 | |
| 225 | size_t busy_counter() const { return busy_counter_; } |
| 226 | void set_busy_counter(size_t busy_counter) { busy_counter_ = busy_counter; } |
| 227 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 228 | // Adjust the flow control window according to new offset in |frame|. |
| 229 | virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame); |
| 230 | |
| 231 | int num_frames_received() const; |
| 232 | int num_duplicate_frames_received() const; |
| 233 | |
renjietang | 07b2e8d | 2020-08-10 15:18:24 -0700 | [diff] [blame] | 234 | // Flow controller related methods. |
| 235 | bool IsFlowControlBlocked() const; |
| 236 | QuicStreamOffset highest_received_byte_offset() const; |
| 237 | void UpdateReceiveWindowSize(QuicStreamOffset size); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 238 | |
| 239 | // Called when endpoint receives a frame which could increase the highest |
| 240 | // offset. |
| 241 | // Returns true if the highest offset did increase. |
| 242 | bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | |
renjietang | e0f96d7 | 2020-04-28 11:25:29 -0700 | [diff] [blame] | 244 | // Set the flow controller's send window offset from session config. |
renjietang | 3f68bdf | 2020-08-10 11:52:30 -0700 | [diff] [blame] | 245 | // |was_zero_rtt_rejected| is true if this config is from a rejected IETF QUIC |
| 246 | // 0-RTT attempt. Closes the connection and returns false if |new_offset| is |
| 247 | // not valid. |
| 248 | bool MaybeConfigSendWindowOffset(QuicStreamOffset new_offset, |
| 249 | bool was_zero_rtt_rejected); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 250 | |
| 251 | // Returns true if the stream has received either a RST_STREAM or a FIN - |
| 252 | // either of which gives a definitive number of bytes which the peer has |
| 253 | // sent. If this is not true on deletion of the stream object, the session |
| 254 | // must keep track of the stream's byte offset until a definitive final value |
| 255 | // arrives. |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 256 | bool HasReceivedFinalOffset() const { return fin_received_ || rst_received_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 257 | |
| 258 | // Returns true if the stream has queued data waiting to write. |
| 259 | bool HasBufferedData() const; |
| 260 | |
| 261 | // Returns the version of QUIC being used for this stream. |
| 262 | QuicTransportVersion transport_version() const; |
| 263 | |
| 264 | // Returns the crypto handshake protocol that was used on this stream's |
| 265 | // connection. |
| 266 | HandshakeProtocol handshake_protocol() const; |
| 267 | |
| 268 | // Sets the sequencer to consume all incoming data itself and not call |
| 269 | // OnDataAvailable(). |
| 270 | // When the FIN is received, the stream will be notified automatically (via |
| 271 | // OnFinRead()) (which may happen during the call of StopReading()). |
| 272 | // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and |
| 273 | // stop sending stream-level flow-control updates when this end sends FIN. |
| 274 | virtual void StopReading(); |
| 275 | |
| 276 | // Sends as much of 'data' to the connection as the connection will consume, |
| 277 | // and then buffers any remaining data in queued_data_. |
| 278 | // If fin is true: if it is immediately passed on to the session, |
| 279 | // write_side_closed() becomes true, otherwise fin_buffered_ becomes true. |
| 280 | void WriteOrBufferData( |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 281 | quiche::QuicheStringPiece data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 282 | bool fin, |
| 283 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 284 | |
| 285 | // Adds random padding after the fin is consumed for this stream. |
| 286 | void AddRandomPaddingAfterFin(); |
| 287 | |
| 288 | // Write |data_length| of data starts at |offset| from send buffer. |
| 289 | bool WriteStreamData(QuicStreamOffset offset, |
| 290 | QuicByteCount data_length, |
| 291 | QuicDataWriter* writer); |
| 292 | |
| 293 | // Called when data [offset, offset + data_length) is acked. |fin_acked| |
| 294 | // indicates whether the fin is acked. Returns true and updates |
| 295 | // |newly_acked_length| if any new stream data (including fin) gets acked. |
| 296 | virtual bool OnStreamFrameAcked(QuicStreamOffset offset, |
| 297 | QuicByteCount data_length, |
| 298 | bool fin_acked, |
| 299 | QuicTime::Delta ack_delay_time, |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 300 | QuicTime receive_timestamp, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 301 | QuicByteCount* newly_acked_length); |
| 302 | |
| 303 | // Called when data [offset, offset + data_length) was retransmitted. |
| 304 | // |fin_retransmitted| indicates whether fin was retransmitted. |
| 305 | virtual void OnStreamFrameRetransmitted(QuicStreamOffset offset, |
| 306 | QuicByteCount data_length, |
| 307 | bool fin_retransmitted); |
| 308 | |
| 309 | // Called when data [offset, offset + data_length) is considered as lost. |
| 310 | // |fin_lost| indicates whether the fin is considered as lost. |
| 311 | virtual void OnStreamFrameLost(QuicStreamOffset offset, |
| 312 | QuicByteCount data_length, |
| 313 | bool fin_lost); |
| 314 | |
| 315 | // Called to retransmit outstanding portion in data [offset, offset + |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 316 | // data_length) and |fin| with Transmission |type|. |
| 317 | // Returns true if all data gets retransmitted. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 318 | virtual bool RetransmitStreamData(QuicStreamOffset offset, |
| 319 | QuicByteCount data_length, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 320 | bool fin, |
| 321 | TransmissionType type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 322 | |
| 323 | // Sets deadline of this stream to be now + |ttl|, returns true if the setting |
| 324 | // succeeds. |
| 325 | bool MaybeSetTtl(QuicTime::Delta ttl); |
| 326 | |
| 327 | // Same as WritevData except data is provided in reference counted memory so |
| 328 | // that data copy is avoided. |
| 329 | QuicConsumedData WriteMemSlices(QuicMemSliceSpan span, bool fin); |
| 330 | |
| 331 | // Returns true if any stream data is lost (including fin) and needs to be |
| 332 | // retransmitted. |
| 333 | virtual bool HasPendingRetransmission() const; |
| 334 | |
| 335 | // Returns true if any portion of data [offset, offset + data_length) is |
| 336 | // outstanding or fin is outstanding (if |fin| is true). Returns false |
| 337 | // otherwise. |
| 338 | bool IsStreamFrameOutstanding(QuicStreamOffset offset, |
| 339 | QuicByteCount data_length, |
| 340 | bool fin) const; |
| 341 | |
| 342 | StreamType type() const { return type_; } |
| 343 | |
renjietang | ed49cb9 | 2020-03-03 14:30:53 -0800 | [diff] [blame] | 344 | // Handle received StopSending frame. Returns true if the processing finishes |
| 345 | // gracefully. |
| 346 | virtual bool OnStopSending(uint16_t code); |
renjietang | d976228 | 2019-11-11 17:11:18 -0800 | [diff] [blame] | 347 | |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 348 | // Returns true if the stream is static. |
| 349 | bool is_static() const { return is_static_; } |
| 350 | |
fayang | be6d664 | 2020-04-16 14:15:34 -0700 | [diff] [blame] | 351 | bool was_draining() const { return was_draining_; } |
| 352 | |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 353 | static spdy::SpdyStreamPrecedence CalculateDefaultPriority( |
| 354 | const QuicSession* session); |
| 355 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 356 | protected: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 357 | // Called when data of [offset, offset + data_length] is buffered in send |
| 358 | // buffer. |
| 359 | virtual void OnDataBuffered( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 360 | QuicStreamOffset /*offset*/, |
| 361 | QuicByteCount /*data_length*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 362 | const QuicReferenceCountedPointer<QuicAckListenerInterface>& |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 363 | /*ack_listener*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 364 | |
fayang | b8f8344 | 2020-06-01 12:09:17 -0700 | [diff] [blame] | 365 | // Called just before the object is destroyed. |
| 366 | // The object should not be accessed after OnClose is called. |
| 367 | // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor |
| 368 | // a RST_STREAM has been sent. |
| 369 | virtual void OnClose(); |
| 370 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 371 | // True if buffered data in send buffer is below buffered_data_threshold_. |
| 372 | bool CanWriteNewData() const; |
| 373 | |
| 374 | // True if buffered data in send buffer is still below |
| 375 | // buffered_data_threshold_ even after writing |length| bytes. |
| 376 | bool CanWriteNewDataAfterData(QuicByteCount length) const; |
| 377 | |
| 378 | // Called when upper layer can write new data. |
| 379 | virtual void OnCanWriteNewData() {} |
| 380 | |
| 381 | // Called when |bytes_consumed| bytes has been consumed. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 382 | virtual void OnStreamDataConsumed(QuicByteCount bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 383 | |
ianswett | 9363720 | 2019-04-03 08:05:29 -0700 | [diff] [blame] | 384 | // Called by the stream sequencer as bytes are consumed from the buffer. |
| 385 | // If the receive window has dropped below the threshold, then send a |
| 386 | // WINDOW_UPDATE frame. |
| 387 | void AddBytesConsumed(QuicByteCount bytes) override; |
| 388 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 389 | // Writes pending retransmissions if any. |
| 390 | virtual void WritePendingRetransmission(); |
| 391 | |
| 392 | // This is called when stream tries to retransmit data after deadline_. Make |
| 393 | // this virtual so that subclasses can implement their own logics. |
| 394 | virtual void OnDeadlinePassed(); |
| 395 | |
fayang | 3a51d1a | 2020-04-16 13:42:08 -0700 | [diff] [blame] | 396 | // Called to set fin_sent_. This is only used by Google QUIC while body is |
| 397 | // empty. |
| 398 | void SetFinSent(); |
| 399 | |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 400 | // Close the write side of the socket. Further writes will fail. |
| 401 | // Can be called by the subclass or internally. |
| 402 | // Does not send a FIN. May cause the stream to be closed. |
| 403 | virtual void CloseWriteSide(); |
| 404 | |
renjietang | df2240d | 2020-07-17 12:17:00 -0700 | [diff] [blame] | 405 | void set_rst_received(bool rst_received) { rst_received_ = rst_received; } |
| 406 | void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; } |
| 407 | |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 408 | StreamDelegateInterface* stream_delegate() { return stream_delegate_; } |
| 409 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 410 | bool fin_buffered() const { return fin_buffered_; } |
| 411 | |
| 412 | const QuicSession* session() const { return session_; } |
| 413 | QuicSession* session() { return session_; } |
| 414 | |
| 415 | const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
| 416 | QuicStreamSequencer* sequencer() { return &sequencer_; } |
| 417 | |
| 418 | void DisableConnectionFlowControlForThisStream() { |
| 419 | stream_contributes_to_connection_flow_control_ = false; |
| 420 | } |
| 421 | |
| 422 | const QuicIntervalSet<QuicStreamOffset>& bytes_acked() const; |
| 423 | |
| 424 | const QuicStreamSendBuffer& send_buffer() const { return send_buffer_; } |
| 425 | |
| 426 | QuicStreamSendBuffer& send_buffer() { return send_buffer_; } |
| 427 | |
| 428 | private: |
| 429 | friend class test::QuicStreamPeer; |
| 430 | friend class QuicStreamUtils; |
| 431 | |
| 432 | QuicStream(QuicStreamId id, |
| 433 | QuicSession* session, |
| 434 | QuicStreamSequencer sequencer, |
| 435 | bool is_static, |
| 436 | StreamType type, |
| 437 | uint64_t stream_bytes_read, |
| 438 | bool fin_received, |
dmcardle | 15db688 | 2020-02-26 12:55:36 -0800 | [diff] [blame] | 439 | quiche::QuicheOptional<QuicFlowController> flow_controller, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 440 | QuicFlowController* connection_flow_controller); |
| 441 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 442 | // Calls MaybeSendBlocked on the stream's flow controller and the connection |
| 443 | // level flow controller. If the stream is flow control blocked by the |
| 444 | // connection-level flow controller but not by the stream-level flow |
| 445 | // controller, marks this stream as connection-level write blocked. |
| 446 | void MaybeSendBlocked(); |
| 447 | |
| 448 | // Write buffered data in send buffer. TODO(fayang): Consider combine |
| 449 | // WriteOrBufferData, Writev and WriteBufferedData. |
| 450 | void WriteBufferedData(); |
| 451 | |
renjietang | f00e1e1 | 2020-08-26 10:51:15 -0700 | [diff] [blame] | 452 | // Close the read side of the stream. May cause the stream to be closed. |
| 453 | void CloseReadSide(); |
| 454 | |
ianswett | 9363720 | 2019-04-03 08:05:29 -0700 | [diff] [blame] | 455 | // Called when bytes are sent to the peer. |
| 456 | void AddBytesSent(QuicByteCount bytes); |
| 457 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 458 | // Returns true if deadline_ has passed. |
| 459 | bool HasDeadlinePassed() const; |
| 460 | |
| 461 | QuicStreamSequencer sequencer_; |
| 462 | QuicStreamId id_; |
| 463 | // Pointer to the owning QuicSession object. |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 464 | // TODO(b/136274541): Remove session pointer from streams. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 465 | QuicSession* session_; |
renjietang | f196f6a | 2020-02-12 12:34:23 -0800 | [diff] [blame] | 466 | StreamDelegateInterface* stream_delegate_; |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 467 | // The precedence of the stream, once parsed. |
| 468 | spdy::SpdyStreamPrecedence precedence_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 469 | // Bytes read refers to payload bytes only: they do not include framing, |
| 470 | // encryption overhead etc. |
| 471 | uint64_t stream_bytes_read_; |
| 472 | |
| 473 | // Stream error code received from a RstStreamFrame or error code sent by the |
| 474 | // visitor or sequencer in the RstStreamFrame. |
| 475 | QuicRstStreamErrorCode stream_error_; |
| 476 | // Connection error code due to which the stream was closed. |stream_error_| |
| 477 | // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers |
| 478 | // should check |connection_error_|. |
| 479 | QuicErrorCode connection_error_; |
| 480 | |
| 481 | // True if the read side is closed and further frames should be rejected. |
| 482 | bool read_side_closed_; |
| 483 | // True if the write side is closed, and further writes should fail. |
| 484 | bool write_side_closed_; |
| 485 | |
| 486 | // True if the subclass has written a FIN with WriteOrBufferData, but it was |
| 487 | // buffered in queued_data_ rather than being sent to the session. |
| 488 | bool fin_buffered_; |
| 489 | // True if a FIN has been sent to the session. |
| 490 | bool fin_sent_; |
| 491 | // True if a FIN is waiting to be acked. |
| 492 | bool fin_outstanding_; |
| 493 | // True if a FIN is lost. |
| 494 | bool fin_lost_; |
| 495 | |
| 496 | // True if this stream has received (and the sequencer has accepted) a |
| 497 | // StreamFrame with the FIN set. |
| 498 | bool fin_received_; |
| 499 | |
| 500 | // True if an RST_STREAM has been sent to the session. |
| 501 | // In combination with fin_sent_, used to ensure that a FIN and/or a |
| 502 | // RST_STREAM is always sent to terminate the stream. |
| 503 | bool rst_sent_; |
| 504 | |
| 505 | // True if this stream has received a RST_STREAM frame. |
| 506 | bool rst_received_; |
| 507 | |
dmcardle | 15db688 | 2020-02-26 12:55:36 -0800 | [diff] [blame] | 508 | quiche::QuicheOptional<QuicFlowController> flow_controller_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 509 | |
| 510 | // The connection level flow controller. Not owned. |
| 511 | QuicFlowController* connection_flow_controller_; |
| 512 | |
| 513 | // Special streams, such as the crypto and headers streams, do not respect |
| 514 | // connection level flow control limits (but are stream level flow control |
| 515 | // limited). |
| 516 | bool stream_contributes_to_connection_flow_control_; |
| 517 | |
| 518 | // A counter incremented when OnCanWrite() is called and no progress is made. |
| 519 | // For debugging only. |
| 520 | size_t busy_counter_; |
| 521 | |
| 522 | // Indicates whether paddings will be added after the fin is consumed for this |
| 523 | // stream. |
| 524 | bool add_random_padding_after_fin_; |
| 525 | |
| 526 | // Send buffer of this stream. Send buffer is cleaned up when data gets acked |
| 527 | // or discarded. |
| 528 | QuicStreamSendBuffer send_buffer_; |
| 529 | |
bnc | 5a40142 | 2019-06-18 04:32:32 -0700 | [diff] [blame] | 530 | // Latched value of quic_buffered_data_threshold. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 531 | const QuicByteCount buffered_data_threshold_; |
| 532 | |
| 533 | // If true, then this stream has precedence over other streams for write |
| 534 | // scheduling. |
| 535 | const bool is_static_; |
| 536 | |
| 537 | // If initialized, reset this stream at this deadline. |
| 538 | QuicTime deadline_; |
| 539 | |
fayang | 6dfe58d | 2020-06-01 08:02:47 -0700 | [diff] [blame] | 540 | // True if this stream has entered draining state. |
fayang | be6d664 | 2020-04-16 14:15:34 -0700 | [diff] [blame] | 541 | bool was_draining_; |
| 542 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 543 | // Indicates whether this stream is bidirectional, read unidirectional or |
| 544 | // write unidirectional. |
| 545 | const StreamType type_; |
renjietang | ff3d3a3 | 2020-02-13 15:13:51 -0800 | [diff] [blame] | 546 | |
| 547 | Perspective perspective_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 548 | }; |
| 549 | |
| 550 | } // namespace quic |
| 551 | |
| 552 | #endif // QUICHE_QUIC_CORE_QUIC_STREAM_H_ |