quic_session.h revision f2477e01787aa58f445919b809d89e252beef54f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A QuicSession, which demuxes a single connection to individual streams.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_QUIC_QUIC_SESSION_H_
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_QUIC_QUIC_SESSION_H_
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/ip_endpoint.h"
152385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "net/base/linked_hash_map.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_connection.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_crypto_stream.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_packet_creator.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_protocol.h"
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/quic/quic_spdy_compressor.h"
21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/quic/quic_spdy_decompressor.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/reliable_quic_stream.h"
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "net/spdy/write_blocked_list.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class QuicCryptoStream;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ReliableQuicStream;
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochclass SSLInfo;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class VisitorShim;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace test {
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class QuicSessionPeer;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace test
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  enum CryptoHandshakeEvent {
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // sent by a client and that subsequent packets will be encrypted. (Client
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // only.)
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ENCRYPTION_FIRST_ESTABLISHED,
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // the server and thus the encryption key has been updated. Therefore the
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // connection should resend any packets that were sent under
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // ENCRYPTION_INITIAL. (Client only.)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ENCRYPTION_REESTABLISHED,
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // our handshake. In a server it indicates that a full, valid client hello
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // has been received. (Client and server.)
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HANDSHAKE_CONFIRMED,
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  QuicSession(QuicConnection* connection,
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              const QuicConfig& config,
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              bool is_server);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~QuicSession();
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // QuicConnectionVisitorInterface methods:
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual bool OnStreamFrames(
63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const std::vector<QuicStreamFrame>& frames) OVERRIDE;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnSuccessfulVersionNegotiation(
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const QuicVersion& version) OVERRIDE{}
690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual void OnConfigNegotiated() OVERRIDE;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Not needed for HTTP.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool OnCanWrite() OVERRIDE;
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual bool HasPendingHandshake() const OVERRIDE;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by streams when they want to write data to the peer.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a pair with the number of bytes consumed from data, and a boolean
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // indicating if the fin bit was consumed.  This does not indicate the data
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // has been sent on the wire: it may have been turned into a packet and queued
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // if the socket was unexpectedly blocked.
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If provided, |ack_notifier_delegate| will be registered to be notified when
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // we have seen ACKs for all packets resulting from this call. Not owned by
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // this class.
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual QuicConsumedData WritevData(
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicStreamId id,
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const struct iovec* iov,
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      int iov_count,
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicStreamOffset offset,
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool fin,
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by streams when they want to close the stream in both directions.
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SendRstStream(QuicStreamId id, QuicRstStreamErrorCode error);
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when the session wants to go away and not accept any new streams.
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SendGoAway(QuicErrorCode error_code, const std::string& reason);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the stream associated with 'stream_id' from the active stream map.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CloseStream(QuicStreamId stream_id);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if outgoing packets will be encrypted, even if the server
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // hasn't confirmed the handshake yet.
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsEncryptionEstablished();
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For a client, returns true if the server has confirmed our handshake. For
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a server, returns true if a full, valid client hello has been received.
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsCryptoHandshakeConfirmed();
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Called by the QuicCryptoStream when the handshake enters a new state.
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Clients will call this function in the order:
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   ENCRYPTION_FIRST_ESTABLISHED
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   zero or more ENCRYPTION_REESTABLISHED
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   HANDSHAKE_CONFIRMED
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Servers will simply call it once with HANDSHAKE_CONFIRMED.
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called by the QuicCryptoStream when a handshake message is sent.
1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnCryptoHandshakeMessageSent(
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const CryptoHandshakeMessage& message);
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called by the QuicCryptoStream when a handshake message is received.
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnCryptoHandshakeMessageReceived(
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const CryptoHandshakeMessage& message);
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns mutable config for this session. Returned config is owned
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // by QuicSession.
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicConfig* config();
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the stream existed previously and has been closed.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns false if the stream is still active or if the stream has
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // not yet been created.
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsClosedStream(QuicStreamId id);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicConnection* connection() { return connection_.get(); }
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const QuicConnection* connection() const { return connection_.get(); }
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t num_active_requests() const { return stream_map_.size(); }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const IPEndPoint& peer_address() const {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return connection_->peer_address();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  QuicGuid guid() const { return connection_->guid(); }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicPacketCreator::Options* options() { return connection()->options(); }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of currently open streams, including those which have
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // been implicitly created.
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual size_t GetNumOpenStreams() const;
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
148d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the session has data to be sent, either queued in the
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // connection, or in a write-blocked stream.
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool HasQueuedData() const;
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
154b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Marks that |stream_id| is blocked waiting to decompress the
155b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // headers identified by |decompression_id|.
156b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  void MarkDecompressionBlocked(QuicHeaderId decompression_id,
157b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                QuicStreamId stream_id);
158b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_received() const {
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return goaway_received_;
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_sent() const {
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return goaway_sent_;
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
167b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  QuicSpdyDecompressor* decompressor() { return &decompressor_; }
168b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  QuicSpdyCompressor* compressor() { return &compressor_; }
169b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Gets the SSL connection information.
1717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual bool GetSSLInfo(SSLInfo* ssl_info);
1727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicErrorCode error() const { return error_; }
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool is_server() const { return is_server_; }
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new stream, owned by the caller, to handle a peer-initiated
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // stream.  Returns NULL and does error handling if the stream can not be
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // created.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ReliableQuicStream* CreateIncomingReliableStream(QuicStreamId id) = 0;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create a new stream, owned by the caller, to handle a locally-initiated
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stream.  Returns NULL if max streams have already been opened.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ReliableQuicStream* CreateOutgoingReliableStream() = 0;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the reserved crypto stream.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual QuicCryptoStream* GetCryptoStream() = 0;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds 'stream' to the active stream map.
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ActivateStream(ReliableQuicStream* stream);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the stream id for a new stream.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId GetNextStreamId();
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReliableQuicStream* GetIncomingReliableStream(QuicStreamId stream_id);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1982385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  ReliableQuicStream* GetStream(const QuicStreamId stream_id);
1992385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This is called after every call other than OnConnectionClose from the
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // QuicConnectionVisitor to allow post-processing once the work has been done.
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, it deletes streams given that it's safe to do so (no other
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // operations are being done on the streams at this time)
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void PostProcessAfterData();
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::hash_map<QuicStreamId, ReliableQuicStream*>* streams() {
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return &stream_map_;
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const base::hash_map<QuicStreamId, ReliableQuicStream*>* streams() const {
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return &stream_map_;
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<ReliableQuicStream*>* closed_streams() {
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return &closed_streams_;
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t get_max_open_streams() const {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return max_open_streams_;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class test::QuicSessionPeer;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class VisitorShim;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::hash_map<QuicStreamId, ReliableQuicStream*> ReliableStreamMap;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
228424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Performs the work required to close |stream_id|.  If |locally_reset|
229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // then the stream has been reset by this endpoint, not by the peer.  This
230424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // means the stream may become a zombie stream which needs to stay
231424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // around until headers have been decompressed.
232424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
233424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
234424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Adds |stream_id| to the zobmie stream map, closing the oldest
235424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // zombie stream if the set is full.
236424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void AddZombieStream(QuicStreamId stream_id);
237424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
238424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Closes the zombie stream |stream_id| and removes it from the zombie
239424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // stream map.
240424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void CloseZombieStream(QuicStreamId stream_id);
241424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
242424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Adds |stream_id| to the prematurely closed stream map, removing the
243424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // oldest prematurely closed stream if the set is full.
244424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void AddPrematurelyClosedStream(QuicStreamId stream_id);
245424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<QuicConnection> connection_;
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // Tracks the last 20 streams which closed without decompressing headers.
2492385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // This is for best-effort detection of an unrecoverable compression context.
2502385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // Ideally this would be a linked_hash_set as the boolean is unused.
2512385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  linked_hash_map<QuicStreamId, bool> prematurely_closed_streams_;
2522385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
253424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Streams which have been locally reset before decompressing headers
254424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // from the peer.  These streams need to stay open long enough to
255424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // process any headers from the peer.
256424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Ideally this would be a linked_hash_set as the boolean is unused.
257424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  linked_hash_map<QuicStreamId, bool> zombie_streams_;
258424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A shim to stand between the connection and the session, to handle stream
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // deletions.
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<VisitorShim> visitor_shim_;
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<ReliableQuicStream*> closed_streams_;
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
265b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  QuicSpdyDecompressor decompressor_;
266b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  QuicSpdyCompressor compressor_;
267b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  QuicConfig config_;
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the maximum number of streams this connection can open.
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t max_open_streams_;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Map from StreamId to pointers to streams that are owned by the caller.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReliableStreamMap stream_map_;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId next_stream_id_;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_server_;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set of stream ids that have been "implicitly created" by receipt
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of a stream id larger than the next expected stream id.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::hash_set<QuicStreamId> implicitly_created_streams_;
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A list of streams which need to write more data.
2833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  WriteBlockedList<QuicStreamId> write_blocked_streams_;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
285b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // A map of headers waiting to be compressed, and the streams
286b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // they are associated with.
287b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  map<uint32, QuicStreamId> decompression_blocked_streams_;
288b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId largest_peer_created_stream_id_;
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The latched error with which the connection was closed.
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicErrorCode error_;
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Whether a GoAway has been received.
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_received_;
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Whether a GoAway has been sent.
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_sent_;
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
299d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Indicate if there is pending data for the crypto stream.
300d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool has_pending_handshake_;
301d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(QuicSession);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_QUIC_QUIC_SESSION_H_
308