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 | // A QuicSession, which demuxes a single connection to individual streams. |
| 6 | |
| 7 | #ifndef QUICHE_QUIC_CORE_QUIC_SESSION_H_ |
| 8 | #define QUICHE_QUIC_CORE_QUIC_SESSION_H_ |
| 9 | |
| 10 | #include <cstddef> |
| 11 | #include <map> |
| 12 | #include <memory> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "base/macros.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/legacy_quic_stream_id_manager.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_control_frame_manager.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/quic_crypto_stream.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/quic_packet_creator.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 22 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
| 23 | #include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h" |
| 25 | #include "net/third_party/quiche/src/quic/core/session_notifier_interface.h" |
| 26 | #include "net/third_party/quiche/src/quic/core/uber_quic_stream_id_manager.h" |
| 27 | #include "net/third_party/quiche/src/quic/platform/api/quic_containers.h" |
| 28 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 29 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 30 | #include "net/third_party/quiche/src/quic/platform/api/quic_string.h" |
| 31 | |
| 32 | namespace quic { |
| 33 | |
| 34 | class QuicCryptoStream; |
| 35 | class QuicFlowController; |
| 36 | class QuicStream; |
| 37 | class QuicStreamIdManager; |
| 38 | |
| 39 | namespace test { |
| 40 | class QuicSessionPeer; |
| 41 | } // namespace test |
| 42 | |
| 43 | class QUIC_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface, |
| 44 | public SessionNotifierInterface, |
| 45 | public QuicStreamFrameDataProducer { |
| 46 | public: |
| 47 | // An interface from the session to the entity owning the session. |
| 48 | // This lets the session notify its owner (the Dispatcher) when the connection |
| 49 | // is closed, blocked, or added/removed from the time-wait list. |
| 50 | class Visitor { |
| 51 | public: |
| 52 | virtual ~Visitor() {} |
| 53 | |
| 54 | // Called when the connection is closed after the streams have been closed. |
| 55 | virtual void OnConnectionClosed(QuicConnectionId connection_id, |
| 56 | QuicErrorCode error, |
| 57 | const QuicString& error_details, |
| 58 | ConnectionCloseSource source) = 0; |
| 59 | |
| 60 | // Called when the session has become write blocked. |
| 61 | virtual void OnWriteBlocked(QuicBlockedWriterInterface* blocked_writer) = 0; |
| 62 | |
| 63 | // Called when the session receives reset on a stream from the peer. |
| 64 | virtual void OnRstStreamReceived(const QuicRstStreamFrame& frame) = 0; |
| 65 | |
| 66 | // Called when the session receives a STOP_SENDING for a stream from the |
| 67 | // peer. |
| 68 | virtual void OnStopSendingReceived(const QuicStopSendingFrame& frame) = 0; |
| 69 | }; |
| 70 | |
| 71 | // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream. |
| 72 | enum CryptoHandshakeEvent { |
| 73 | // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been |
| 74 | // sent by a client and that subsequent packets will be encrypted. (Client |
| 75 | // only.) |
| 76 | ENCRYPTION_FIRST_ESTABLISHED, |
| 77 | // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by |
| 78 | // the server and thus the encryption key has been updated. Therefore the |
| 79 | // connection should resend any packets that were sent under |
| 80 | // ENCRYPTION_ZERO_RTT. (Client only.) |
| 81 | ENCRYPTION_REESTABLISHED, |
| 82 | // HANDSHAKE_CONFIRMED, in a client, indicates the server has accepted |
| 83 | // our handshake. In a server it indicates that a full, valid client hello |
| 84 | // has been received. (Client and server.) |
| 85 | HANDSHAKE_CONFIRMED, |
| 86 | }; |
| 87 | |
| 88 | // Does not take ownership of |connection| or |visitor|. |
| 89 | QuicSession(QuicConnection* connection, |
| 90 | Visitor* owner, |
| 91 | const QuicConfig& config, |
| 92 | const ParsedQuicVersionVector& supported_versions); |
| 93 | QuicSession(const QuicSession&) = delete; |
| 94 | QuicSession& operator=(const QuicSession&) = delete; |
| 95 | |
| 96 | ~QuicSession() override; |
| 97 | |
| 98 | virtual void Initialize(); |
| 99 | |
| 100 | // QuicConnectionVisitorInterface methods: |
| 101 | void OnStreamFrame(const QuicStreamFrame& frame) override; |
| 102 | void OnCryptoFrame(const QuicCryptoFrame& frame) override; |
| 103 | void OnRstStream(const QuicRstStreamFrame& frame) override; |
| 104 | void OnGoAway(const QuicGoAwayFrame& frame) override; |
| 105 | void OnMessageReceived(QuicStringPiece message) override; |
| 106 | void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override; |
| 107 | void OnBlockedFrame(const QuicBlockedFrame& frame) override; |
| 108 | void OnConnectionClosed(QuicErrorCode error, |
| 109 | const QuicString& error_details, |
| 110 | ConnectionCloseSource source) override; |
| 111 | void OnWriteBlocked() override; |
| 112 | void OnSuccessfulVersionNegotiation( |
| 113 | const ParsedQuicVersion& version) override; |
| 114 | void OnConnectivityProbeReceived( |
| 115 | const QuicSocketAddress& self_address, |
| 116 | const QuicSocketAddress& peer_address) override; |
| 117 | void OnCanWrite() override; |
| 118 | void OnCongestionWindowChange(QuicTime /*now*/) override {} |
| 119 | void OnConnectionMigration(AddressChangeType type) override {} |
| 120 | // Adds a connection level WINDOW_UPDATE frame. |
| 121 | void OnAckNeedsRetransmittableFrame() override; |
| 122 | void SendPing() override; |
| 123 | bool WillingAndAbleToWrite() const override; |
| 124 | bool HasPendingHandshake() const override; |
| 125 | void OnPathDegrading() override; |
| 126 | bool AllowSelfAddressChange() const override; |
| 127 | void OnForwardProgressConfirmed() override; |
| 128 | bool OnMaxStreamIdFrame(const QuicMaxStreamIdFrame& frame) override; |
| 129 | bool OnStreamIdBlockedFrame(const QuicStreamIdBlockedFrame& frame) override; |
| 130 | bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override; |
| 131 | |
| 132 | // QuicStreamFrameDataProducer |
| 133 | WriteStreamDataResult WriteStreamData(QuicStreamId id, |
| 134 | QuicStreamOffset offset, |
| 135 | QuicByteCount data_length, |
| 136 | QuicDataWriter* writer) override; |
| 137 | bool WriteCryptoData(EncryptionLevel level, |
| 138 | QuicStreamOffset offset, |
| 139 | QuicByteCount data_length, |
| 140 | QuicDataWriter* writer) override; |
| 141 | |
| 142 | // SessionNotifierInterface methods: |
| 143 | bool OnFrameAcked(const QuicFrame& frame, |
| 144 | QuicTime::Delta ack_delay_time) override; |
| 145 | void OnStreamFrameRetransmitted(const QuicStreamFrame& frame) override; |
| 146 | void OnFrameLost(const QuicFrame& frame) override; |
| 147 | void RetransmitFrames(const QuicFrames& frames, |
| 148 | TransmissionType type) override; |
| 149 | bool IsFrameOutstanding(const QuicFrame& frame) const override; |
| 150 | bool HasUnackedCryptoData() const override; |
| 151 | |
| 152 | // Called on every incoming packet. Passes |packet| through to |connection_|. |
| 153 | virtual void ProcessUdpPacket(const QuicSocketAddress& self_address, |
| 154 | const QuicSocketAddress& peer_address, |
| 155 | const QuicReceivedPacket& packet); |
| 156 | |
| 157 | // Called by streams when they want to write data to the peer. |
| 158 | // Returns a pair with the number of bytes consumed from data, and a boolean |
| 159 | // indicating if the fin bit was consumed. This does not indicate the data |
| 160 | // has been sent on the wire: it may have been turned into a packet and queued |
| 161 | // if the socket was unexpectedly blocked. |
| 162 | virtual QuicConsumedData WritevData(QuicStream* stream, |
| 163 | QuicStreamId id, |
| 164 | size_t write_length, |
| 165 | QuicStreamOffset offset, |
| 166 | StreamSendingState state); |
| 167 | |
| 168 | // Called by application to send |message|. Data copy can be avoided if |
| 169 | // |message| is provided in reference counted memory. |
| 170 | // Please note, |message| provided in reference counted memory would be moved |
| 171 | // internally when message is successfully sent. Thereafter, it would be |
| 172 | // undefined behavior if callers try to access the slices through their own |
| 173 | // copy of the span object. |
| 174 | // Returns the message result which includes the message status and message ID |
| 175 | // (valid if the write succeeds). SendMessage flushes a message packet even it |
| 176 | // is not full. If the application wants to bundle other data in the same |
| 177 | // packet, please consider adding a packet flusher around the SendMessage |
| 178 | // and/or WritevData calls. |
| 179 | // |
| 180 | // OnMessageAcked and OnMessageLost are called when a particular message gets |
| 181 | // acked or lost. |
| 182 | // |
| 183 | // Note that SendMessage will fail with status = MESSAGE_STATUS_BLOCKED |
| 184 | // if connection is congestion control blocked or underlying socket is write |
| 185 | // blocked. In this case the caller can retry sending message again when |
| 186 | // connection becomes available, for example after getting OnCanWrite() |
| 187 | // callback. |
| 188 | MessageResult SendMessage(QuicMemSliceSpan message); |
| 189 | |
| 190 | // Called when message with |message_id| gets acked. |
| 191 | virtual void OnMessageAcked(QuicMessageId message_id); |
| 192 | |
| 193 | // Called when message with |message_id| is considered as lost. |
| 194 | virtual void OnMessageLost(QuicMessageId message_id); |
| 195 | |
| 196 | // Called by control frame manager when it wants to write control frames to |
| 197 | // the peer. Returns true if |frame| is consumed, false otherwise. |
| 198 | virtual bool WriteControlFrame(const QuicFrame& frame); |
| 199 | |
| 200 | // Called by streams when they want to close the stream in both directions. |
| 201 | virtual void SendRstStream(QuicStreamId id, |
| 202 | QuicRstStreamErrorCode error, |
| 203 | QuicStreamOffset bytes_written); |
| 204 | |
| 205 | // Called when the session wants to go away and not accept any new streams. |
| 206 | virtual void SendGoAway(QuicErrorCode error_code, const QuicString& reason); |
| 207 | |
| 208 | // Sends a BLOCKED frame. |
| 209 | virtual void SendBlocked(QuicStreamId id); |
| 210 | |
| 211 | // Sends a WINDOW_UPDATE frame. |
| 212 | virtual void SendWindowUpdate(QuicStreamId id, QuicStreamOffset byte_offset); |
| 213 | |
| 214 | // Send a MAX_STREAM_ID frame. |
| 215 | void SendMaxStreamId(QuicStreamId max_allowed_incoming_id); |
| 216 | |
| 217 | // Send a STREAM_ID_BLOCKED frame. |
| 218 | void SendStreamIdBlocked(QuicStreamId max_allowed_outgoing_id); |
| 219 | |
| 220 | // Create and transmit a STOP_SENDING frame |
| 221 | virtual void SendStopSending(uint16_t code, QuicStreamId stream_id); |
| 222 | |
| 223 | // Removes the stream associated with 'stream_id' from the active stream map. |
| 224 | virtual void CloseStream(QuicStreamId stream_id); |
| 225 | |
| 226 | // Returns true if outgoing packets will be encrypted, even if the server |
| 227 | // hasn't confirmed the handshake yet. |
| 228 | virtual bool IsEncryptionEstablished() const; |
| 229 | |
| 230 | // For a client, returns true if the server has confirmed our handshake. For |
| 231 | // a server, returns true if a full, valid client hello has been received. |
| 232 | virtual bool IsCryptoHandshakeConfirmed() const; |
| 233 | |
| 234 | // Called by the QuicCryptoStream when a new QuicConfig has been negotiated. |
| 235 | virtual void OnConfigNegotiated(); |
| 236 | |
| 237 | // Called by the QuicCryptoStream when the handshake enters a new state. |
| 238 | // |
| 239 | // Clients will call this function in the order: |
| 240 | // ENCRYPTION_FIRST_ESTABLISHED |
| 241 | // zero or more ENCRYPTION_REESTABLISHED |
| 242 | // HANDSHAKE_CONFIRMED |
| 243 | // |
| 244 | // Servers will simply call it once with HANDSHAKE_CONFIRMED. |
| 245 | virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event); |
| 246 | |
| 247 | // Called by the QuicCryptoStream when a handshake message is sent. |
| 248 | virtual void OnCryptoHandshakeMessageSent( |
| 249 | const CryptoHandshakeMessage& message); |
| 250 | |
| 251 | // Called by the QuicCryptoStream when a handshake message is received. |
| 252 | virtual void OnCryptoHandshakeMessageReceived( |
| 253 | const CryptoHandshakeMessage& message); |
| 254 | |
| 255 | // Called by the stream on creation to set priority in the write blocked list. |
| 256 | virtual void RegisterStreamPriority(QuicStreamId id, |
| 257 | bool is_static, |
| 258 | spdy::SpdyPriority priority); |
| 259 | // Called by the stream on deletion to clear priority from the write blocked |
| 260 | // list. |
| 261 | virtual void UnregisterStreamPriority(QuicStreamId id, bool is_static); |
| 262 | // Called by the stream on SetPriority to update priority on the write blocked |
| 263 | // list. |
| 264 | virtual void UpdateStreamPriority(QuicStreamId id, |
| 265 | spdy::SpdyPriority new_priority); |
| 266 | |
| 267 | // Returns mutable config for this session. Returned config is owned |
| 268 | // by QuicSession. |
| 269 | QuicConfig* config(); |
| 270 | |
| 271 | // Returns true if the stream existed previously and has been closed. |
| 272 | // Returns false if the stream is still active or if the stream has |
| 273 | // not yet been created. |
| 274 | bool IsClosedStream(QuicStreamId id); |
| 275 | |
| 276 | QuicConnection* connection() { return connection_; } |
| 277 | const QuicConnection* connection() const { return connection_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 278 | const QuicSocketAddress& peer_address() const { |
| 279 | return connection_->peer_address(); |
| 280 | } |
| 281 | const QuicSocketAddress& self_address() const { |
| 282 | return connection_->self_address(); |
| 283 | } |
| 284 | QuicConnectionId connection_id() const { |
| 285 | return connection_->connection_id(); |
| 286 | } |
| 287 | |
| 288 | // Returns the number of currently open streams, excluding the reserved |
| 289 | // headers and crypto streams, and never counting unfinished streams. |
| 290 | size_t GetNumActiveStreams() const; |
| 291 | |
| 292 | // Returns the number of currently draining streams. |
| 293 | size_t GetNumDrainingStreams() const; |
| 294 | |
| 295 | // Returns the number of currently open peer initiated streams, excluding the |
| 296 | // reserved headers and crypto streams. |
| 297 | size_t GetNumOpenIncomingStreams() const; |
| 298 | |
| 299 | // Returns the number of currently open self initiated streams, excluding the |
| 300 | // reserved headers and crypto streams. |
| 301 | size_t GetNumOpenOutgoingStreams() const; |
| 302 | |
| 303 | // Add the stream to the session's write-blocked list because it is blocked by |
| 304 | // connection-level flow control but not by its own stream-level flow control. |
| 305 | // The stream will be given a chance to write when a connection-level |
| 306 | // WINDOW_UPDATE arrives. |
| 307 | void MarkConnectionLevelWriteBlocked(QuicStreamId id); |
| 308 | |
| 309 | // Called when stream |id| is done waiting for acks either because all data |
| 310 | // gets acked or is not interested in data being acked (which happens when |
| 311 | // a stream is reset because of an error). |
| 312 | void OnStreamDoneWaitingForAcks(QuicStreamId id); |
| 313 | |
| 314 | // Called to cancel retransmission of unencypted crypto stream data. |
| 315 | void NeuterUnencryptedData(); |
| 316 | |
| 317 | // Returns true if the session has data to be sent, either queued in the |
| 318 | // connection, or in a write-blocked stream. |
| 319 | bool HasDataToWrite() const; |
| 320 | |
| 321 | // Returns the largest payload that will fit into a single MESSAGE frame. |
| 322 | // Because overhead can vary during a connection, this method should be |
| 323 | // checked for every message. |
| 324 | QuicPacketLength GetLargestMessagePayload() const; |
| 325 | |
| 326 | bool goaway_sent() const { return goaway_sent_; } |
| 327 | |
| 328 | bool goaway_received() const { return goaway_received_; } |
| 329 | |
| 330 | QuicErrorCode error() const { return error_; } |
| 331 | |
| 332 | Perspective perspective() const { return connection_->perspective(); } |
| 333 | |
| 334 | QuicFlowController* flow_controller() { return &flow_controller_; } |
| 335 | |
| 336 | // Returns true if connection is flow controller blocked. |
| 337 | bool IsConnectionFlowControlBlocked() const; |
| 338 | |
| 339 | // Returns true if any stream is flow controller blocked. |
| 340 | bool IsStreamFlowControlBlocked(); |
| 341 | |
| 342 | size_t max_open_incoming_bidirectional_streams() const; |
| 343 | size_t max_open_incoming_unidirectional_streams() const; |
| 344 | |
| 345 | size_t MaxAvailableBidirectionalStreams() const; |
| 346 | size_t MaxAvailableUnidirectionalStreams() const; |
| 347 | |
| 348 | // Returns existing static or dynamic stream with id = |stream_id|. If no |
| 349 | // such stream exists, and |stream_id| is a peer-created dynamic stream id, |
| 350 | // then a new stream is created and returned. In all other cases, nullptr is |
| 351 | // returned. |
| 352 | QuicStream* GetOrCreateStream(const QuicStreamId stream_id); |
| 353 | |
| 354 | // Mark a stream as draining. |
| 355 | virtual void StreamDraining(QuicStreamId id); |
| 356 | |
| 357 | // Returns true if this stream should yield writes to another blocked stream. |
| 358 | bool ShouldYield(QuicStreamId stream_id); |
| 359 | |
| 360 | // Set transmission type of next sending packets. |
| 361 | void SetTransmissionType(TransmissionType type); |
| 362 | |
| 363 | // Clean up closed_streams_. |
| 364 | void CleanUpClosedStreams(); |
| 365 | |
| 366 | bool session_decides_what_to_write() const; |
| 367 | |
| 368 | const ParsedQuicVersionVector& supported_versions() const { |
| 369 | return supported_versions_; |
| 370 | } |
| 371 | |
| 372 | // Called when new outgoing streams are available to be opened. This occurs |
| 373 | // when an extant, open, stream is moved to draining or closed. The default |
| 374 | // implementation does nothing. |
| 375 | virtual void OnCanCreateNewOutgoingStream(); |
| 376 | |
| 377 | QuicStreamId next_outgoing_bidirectional_stream_id() const; |
| 378 | QuicStreamId next_outgoing_unidirectional_stream_id() const; |
| 379 | |
| 380 | // Return true if given stream is peer initiated. |
| 381 | bool IsIncomingStream(QuicStreamId id) const; |
| 382 | |
| 383 | size_t GetNumLocallyClosedOutgoingStreamsHighestOffset() const; |
| 384 | |
| 385 | size_t num_locally_closed_incoming_streams_highest_offset() const { |
| 386 | return num_locally_closed_incoming_streams_highest_offset_; |
| 387 | } |
| 388 | |
| 389 | // Does actual work of sending reset-stream or reset-stream&stop-sending |
| 390 | // If the connection is not version 99/IETF QUIC, will always send a |
| 391 | // RESET_STREAM and close_write_side_only is ignored. If the connection is |
| 392 | // IETF QUIC/Version 99 then will send a RESET_STREAM and STOP_SENDING if |
| 393 | // close_write_side_only is false, just a RESET_STREAM if |
| 394 | // close_write_side_only is true. |
| 395 | virtual void SendRstStreamInner(QuicStreamId id, |
| 396 | QuicRstStreamErrorCode error, |
| 397 | QuicStreamOffset bytes_written, |
| 398 | bool close_write_side_only); |
| 399 | |
| 400 | protected: |
| 401 | using StaticStreamMap = QuicSmallMap<QuicStreamId, QuicStream*, 2>; |
| 402 | |
| 403 | using DynamicStreamMap = |
| 404 | QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>; |
| 405 | |
| 406 | using PendingStreamMap = |
| 407 | QuicSmallMap<QuicStreamId, std::unique_ptr<PendingStream>, 10>; |
| 408 | |
| 409 | using ClosedStreams = std::vector<std::unique_ptr<QuicStream>>; |
| 410 | |
| 411 | using ZombieStreamMap = |
| 412 | QuicSmallMap<QuicStreamId, std::unique_ptr<QuicStream>, 10>; |
| 413 | |
| 414 | // Creates a new stream to handle a peer-initiated stream. |
| 415 | // Caller does not own the returned stream. |
| 416 | // Returns nullptr and does error handling if the stream can not be created. |
| 417 | virtual QuicStream* CreateIncomingStream(QuicStreamId id) = 0; |
| 418 | virtual QuicStream* CreateIncomingStream(PendingStream pending) = 0; |
| 419 | |
| 420 | // Return the reserved crypto stream. |
| 421 | virtual QuicCryptoStream* GetMutableCryptoStream() = 0; |
| 422 | |
| 423 | // Return the reserved crypto stream as a constant pointer. |
| 424 | virtual const QuicCryptoStream* GetCryptoStream() const = 0; |
| 425 | |
| 426 | // Adds |stream| to the dynamic stream map. |
| 427 | virtual void ActivateStream(std::unique_ptr<QuicStream> stream); |
| 428 | |
| 429 | // Returns the stream ID for a new outgoing bidirectional/unidirectional |
| 430 | // stream, and increments the underlying counter. |
| 431 | QuicStreamId GetNextOutgoingBidirectionalStreamId(); |
| 432 | QuicStreamId GetNextOutgoingUnidirectionalStreamId(); |
| 433 | |
| 434 | // Indicates whether the next outgoing bidirectional/unidirectional stream ID |
| 435 | // can be allocated or not. The test for version-99/IETF QUIC is whether it |
| 436 | // will exceed the maximum-stream-id or not. For non-version-99 (Google) QUIC |
| 437 | // it checks whether the next stream would exceed the limit on the number of |
| 438 | // open streams. |
| 439 | bool CanOpenNextOutgoingBidirectionalStream(); |
| 440 | bool CanOpenNextOutgoingUnidirectionalStream(); |
| 441 | |
| 442 | // Returns the number of open dynamic streams. |
| 443 | uint64_t GetNumOpenDynamicStreams() const; |
| 444 | |
| 445 | // Returns existing stream with id = |stream_id|. If no such stream exists, |
| 446 | // and |stream_id| is a peer-created id, then a new stream is created and |
| 447 | // returned. However if |stream_id| is a locally-created id and no such stream |
| 448 | // exists, the connection is closed. |
| 449 | // Caller does not own the returned stream. |
| 450 | QuicStream* GetOrCreateDynamicStream(QuicStreamId stream_id); |
| 451 | |
| 452 | // Performs the work required to close |stream_id|. If |locally_reset| |
| 453 | // then the stream has been reset by this endpoint, not by the peer. |
| 454 | virtual void CloseStreamInner(QuicStreamId stream_id, bool locally_reset); |
| 455 | |
| 456 | // When a stream is closed locally, it may not yet know how many bytes the |
| 457 | // peer sent on that stream. |
| 458 | // When this data arrives (via stream frame w. FIN, trailing headers, or RST) |
| 459 | // this method is called, and correctly updates the connection level flow |
| 460 | // controller. |
| 461 | virtual void OnFinalByteOffsetReceived(QuicStreamId id, |
| 462 | QuicStreamOffset final_byte_offset); |
| 463 | |
| 464 | // Returns true if incoming streams should be buffered until the first |
| 465 | // byte of the stream arrives. |
| 466 | virtual bool ShouldBufferIncomingStream(QuicStreamId id) const { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | // Register (|id|, |stream|) with the static stream map. Override previous |
| 471 | // registrations with the same id. |
| 472 | void RegisterStaticStream(QuicStreamId id, QuicStream* stream); |
| 473 | const StaticStreamMap& static_streams() const { return static_stream_map_; } |
| 474 | |
| 475 | DynamicStreamMap& dynamic_streams() { return dynamic_stream_map_; } |
| 476 | const DynamicStreamMap& dynamic_streams() const { |
| 477 | return dynamic_stream_map_; |
| 478 | } |
| 479 | |
| 480 | ClosedStreams* closed_streams() { return &closed_streams_; } |
| 481 | |
| 482 | const ZombieStreamMap& zombie_streams() const { return zombie_streams_; } |
| 483 | |
| 484 | void set_largest_peer_created_stream_id( |
| 485 | QuicStreamId largest_peer_created_stream_id); |
| 486 | |
| 487 | void set_error(QuicErrorCode error) { error_ = error; } |
| 488 | QuicWriteBlockedList* write_blocked_streams() { |
| 489 | return &write_blocked_streams_; |
| 490 | } |
| 491 | |
| 492 | size_t GetNumDynamicOutgoingStreams() const; |
| 493 | |
| 494 | size_t GetNumDrainingOutgoingStreams() const; |
| 495 | |
| 496 | // Returns true if the stream is still active. |
| 497 | bool IsOpenStream(QuicStreamId id); |
| 498 | |
| 499 | // Close connection when receive a frame for a locally-created nonexistant |
| 500 | // stream. |
| 501 | // Prerequisite: IsClosedStream(stream_id) == false |
| 502 | // Server session might need to override this method to allow server push |
| 503 | // stream to be promised before creating an active stream. |
| 504 | virtual void HandleFrameOnNonexistentOutgoingStream(QuicStreamId stream_id); |
| 505 | |
| 506 | virtual bool MaybeIncreaseLargestPeerStreamId(const QuicStreamId stream_id); |
| 507 | |
| 508 | void InsertLocallyClosedStreamsHighestOffset(const QuicStreamId id, |
| 509 | QuicStreamOffset offset); |
| 510 | // If stream is a locally closed stream, this RST will update FIN offset. |
| 511 | // Otherwise stream is a preserved stream and the behavior of it depends on |
| 512 | // derived class's own implementation. |
| 513 | virtual void HandleRstOnValidNonexistentStream( |
| 514 | const QuicRstStreamFrame& frame); |
| 515 | |
| 516 | // Returns a stateless reset token which will be included in the public reset |
| 517 | // packet. |
| 518 | virtual QuicUint128 GetStatelessResetToken() const; |
| 519 | |
| 520 | QuicControlFrameManager& control_frame_manager() { |
| 521 | return control_frame_manager_; |
| 522 | } |
| 523 | |
| 524 | const LegacyQuicStreamIdManager& stream_id_manager() const { |
| 525 | return stream_id_manager_; |
| 526 | } |
| 527 | |
| 528 | // A StreamHandler represents an object which can receive a STREAM or |
| 529 | // or RST_STREAM frame. |
| 530 | struct StreamHandler { |
| 531 | StreamHandler() : is_pending(false), stream(nullptr) {} |
| 532 | |
| 533 | // Creates a StreamHandler wrapping a QuicStream. |
| 534 | explicit StreamHandler(QuicStream* stream) |
| 535 | : is_pending(false), stream(stream) {} |
| 536 | |
| 537 | // Creates a StreamHandler wrapping a PendingStream. |
| 538 | explicit StreamHandler(PendingStream* pending) |
| 539 | : is_pending(true), pending(pending) { |
| 540 | DCHECK(pending != nullptr); |
| 541 | } |
| 542 | |
| 543 | // True if this handler contains a non-null PendingStream, false otherwise. |
| 544 | bool is_pending; |
| 545 | union { |
| 546 | QuicStream* stream; |
| 547 | PendingStream* pending; |
| 548 | }; |
| 549 | }; |
| 550 | |
| 551 | StreamHandler GetOrCreateStreamImpl(QuicStreamId stream_id, bool may_buffer); |
| 552 | |
| 553 | private: |
| 554 | friend class test::QuicSessionPeer; |
| 555 | |
| 556 | // Called in OnConfigNegotiated when we receive a new stream level flow |
| 557 | // control window in a negotiated config. Closes the connection if invalid. |
| 558 | void OnNewStreamFlowControlWindow(QuicStreamOffset new_window); |
| 559 | |
| 560 | // Called in OnConfigNegotiated when we receive a new connection level flow |
| 561 | // control window in a negotiated config. Closes the connection if invalid. |
| 562 | void OnNewSessionFlowControlWindow(QuicStreamOffset new_window); |
| 563 | |
| 564 | // Debug helper for |OnCanWrite()|, check that OnStreamWrite() makes |
| 565 | // forward progress. Returns false if busy loop detected. |
| 566 | bool CheckStreamNotBusyLooping(QuicStream* stream, |
| 567 | uint64_t previous_bytes_written, |
| 568 | bool previous_fin_sent); |
| 569 | |
| 570 | // Debug helper for OnCanWrite. Check that after QuicStream::OnCanWrite(), |
| 571 | // if stream has buffered data and is not stream level flow control blocked, |
| 572 | // it has to be in the write blocked list. |
| 573 | bool CheckStreamWriteBlocked(QuicStream* stream) const; |
| 574 | |
| 575 | // Called in OnConfigNegotiated for Finch trials to measure performance of |
| 576 | // starting with larger flow control receive windows. |
| 577 | void AdjustInitialFlowControlWindows(size_t stream_window); |
| 578 | |
| 579 | // Find stream with |id|, returns nullptr if the stream does not exist or |
| 580 | // closed. |
| 581 | QuicStream* GetStream(QuicStreamId id) const; |
| 582 | |
| 583 | StreamHandler GetOrCreateDynamicStreamImpl(QuicStreamId stream_id, |
| 584 | bool may_buffer); |
| 585 | |
| 586 | // Let streams and control frame managers retransmit lost data, returns true |
| 587 | // if all lost data is retransmitted. Returns false otherwise. |
| 588 | bool RetransmitLostData(); |
| 589 | |
| 590 | // Closes the pending stream |stream_id| before it has been created. |
| 591 | void ClosePendingStream(QuicStreamId stream_id); |
| 592 | |
| 593 | // Keep track of highest received byte offset of locally closed streams, while |
| 594 | // waiting for a definitive final highest offset from the peer. |
| 595 | std::map<QuicStreamId, QuicStreamOffset> |
| 596 | locally_closed_streams_highest_offset_; |
| 597 | |
| 598 | QuicConnection* connection_; |
| 599 | |
| 600 | // May be null. |
| 601 | Visitor* visitor_; |
| 602 | |
| 603 | // A list of streams which need to write more data. Stream register |
| 604 | // themselves in their constructor, and unregisterm themselves in their |
| 605 | // destructors, so the write blocked list must outlive all streams. |
| 606 | QuicWriteBlockedList write_blocked_streams_; |
| 607 | |
| 608 | ClosedStreams closed_streams_; |
| 609 | // Streams which are closed, but need to be kept alive. Currently, the only |
| 610 | // reason is the stream's sent data (including FIN) does not get fully acked. |
| 611 | ZombieStreamMap zombie_streams_; |
| 612 | |
| 613 | QuicConfig config_; |
| 614 | |
| 615 | // Static streams, such as crypto and header streams. Owned by child classes |
| 616 | // that create these streams. |
| 617 | StaticStreamMap static_stream_map_; |
| 618 | |
| 619 | // Map from StreamId to pointers to streams. Owns the streams. |
| 620 | DynamicStreamMap dynamic_stream_map_; |
| 621 | |
| 622 | // Map from StreamId to PendingStreams for peer-created unidirectional streams |
| 623 | // which are waiting for the first byte of payload to arrive. |
| 624 | PendingStreamMap pending_stream_map_; |
| 625 | |
| 626 | // Set of stream ids that are "draining" -- a FIN has been sent and received, |
| 627 | // but the stream object still exists because not all the received data has |
| 628 | // been consumed. |
| 629 | QuicUnorderedSet<QuicStreamId> draining_streams_; |
| 630 | |
| 631 | // TODO(fayang): Consider moving LegacyQuicStreamIdManager into |
| 632 | // UberQuicStreamIdManager. |
| 633 | // Manages stream IDs for Google QUIC. |
| 634 | LegacyQuicStreamIdManager stream_id_manager_; |
| 635 | |
| 636 | // Manages stream IDs for version99/IETF QUIC |
| 637 | UberQuicStreamIdManager v99_streamid_manager_; |
| 638 | |
| 639 | // A counter for peer initiated streams which are in the dynamic_stream_map_. |
| 640 | size_t num_dynamic_incoming_streams_; |
| 641 | |
| 642 | // A counter for peer initiated streams which are in the draining_streams_. |
| 643 | size_t num_draining_incoming_streams_; |
| 644 | |
| 645 | // A counter for peer initiated streams which are in the |
| 646 | // locally_closed_streams_highest_offset_. |
| 647 | size_t num_locally_closed_incoming_streams_highest_offset_; |
| 648 | |
| 649 | // The latched error with which the connection was closed. |
| 650 | QuicErrorCode error_; |
| 651 | |
| 652 | // Used for connection-level flow control. |
| 653 | QuicFlowController flow_controller_; |
| 654 | |
| 655 | // The stream id which was last popped in OnCanWrite, or 0, if not under the |
| 656 | // call stack of OnCanWrite. |
| 657 | QuicStreamId currently_writing_stream_id_; |
| 658 | |
| 659 | // The largest stream id in |static_stream_map_|. |
| 660 | QuicStreamId largest_static_stream_id_; |
| 661 | |
| 662 | // Cached value of whether the crypto handshake has been confirmed. |
| 663 | bool is_handshake_confirmed_; |
| 664 | |
| 665 | // Whether a GoAway has been sent. |
| 666 | bool goaway_sent_; |
| 667 | |
| 668 | // Whether a GoAway has been received. |
| 669 | bool goaway_received_; |
| 670 | |
| 671 | QuicControlFrameManager control_frame_manager_; |
| 672 | |
| 673 | // Id of latest successfully sent message. |
| 674 | QuicMessageId last_message_id_; |
| 675 | |
| 676 | // TODO(fayang): switch to linked_hash_set when chromium supports it. The bool |
| 677 | // is not used here. |
| 678 | // List of streams with pending retransmissions. |
| 679 | QuicLinkedHashMap<QuicStreamId, bool> streams_with_pending_retransmission_; |
| 680 | |
| 681 | // Clean up closed_streams_ when this alarm fires. |
| 682 | std::unique_ptr<QuicAlarm> closed_streams_clean_up_alarm_; |
| 683 | |
| 684 | // Supported version list used by the crypto handshake only. Please note, this |
| 685 | // list may be a superset of the connection framer's supported versions. |
| 686 | ParsedQuicVersionVector supported_versions_; |
| 687 | }; |
| 688 | |
| 689 | } // namespace quic |
| 690 | |
| 691 | #endif // QUICHE_QUIC_CORE_QUIC_SESSION_H_ |