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"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_connection.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_crypto_stream.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_data_stream.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_headers_stream.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_packet_creator.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/quic_protocol.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_write_blocked_list.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/quic/reliable_quic_stream.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class QuicCryptoStream;
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class QuicFlowController;
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)
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  QuicSession(QuicConnection* connection, const QuicConfig& config);
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void InitializeSession();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~QuicSession();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // QuicConnectionVisitorInterface methods:
615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual void OnStreamFrames(
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const std::vector<QuicStreamFrame>& frames) OVERRIDE;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnWindowUpdateFrames(
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<QuicWindowUpdateFrame>& frames) OVERRIDE;
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnBlockedFrames(
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<QuicBlockedFrame>& frames) OVERRIDE;
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnWriteBlocked() OVERRIDE {}
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual void OnSuccessfulVersionNegotiation(
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const QuicVersion& version) OVERRIDE;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnCanWrite() OVERRIDE;
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual void OnCongestionWindowChange(QuicTime now) OVERRIDE {}
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool WillingAndAbleToWrite() const OVERRIDE;
76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual bool HasPendingHandshake() const OVERRIDE;
770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual bool HasOpenDataStreams() const OVERRIDE;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the headers stream when headers have been received for a stream.
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeaders(QuicStreamId stream_id,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               base::StringPiece headers_data);
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the headers stream when headers with a priority have been
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // received for this stream.  This method will only be called for server
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // streams.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       QuicPriority priority);
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the headers stream when headers have been completely received
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for a stream.  |fin| will be true if the fin flag was set in the headers
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // frame.
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeadersComplete(QuicStreamId stream_id,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       bool fin,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       size_t frame_len);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by streams when they want to write data to the peer.
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a pair with the number of bytes consumed from data, and a boolean
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // indicating if the fin bit was consumed.  This does not indicate the data
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // has been sent on the wire: it may have been turned into a packet and queued
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // if the socket was unexpectedly blocked.  |fec_protection| indicates if
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // data is to be FEC protected. Note that data that is sent immediately
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // following MUST_FEC_PROTECT data may get protected by falling within the
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // same FEC group.
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If provided, |ack_notifier_delegate| will be registered to be notified when
10323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // we have seen ACKs for all packets resulting from this call.
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual QuicConsumedData WritevData(
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicStreamId id,
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const IOVector& data,
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicStreamOffset offset,
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool fin,
109f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      FecProtection fec_protection,
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Writes |headers| for the stream |id| to the dedicated headers stream.
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If |fin| is true, then no more data will be sent for the stream |id|.
11423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // If provided, |ack_notifier_delegate| will be registered to be notified when
11523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // we have seen ACKs for all packets resulting from this call.
11623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  size_t WriteHeaders(
11723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      QuicStreamId id,
11823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      const SpdyHeaderBlock& headers,
11923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      bool fin,
12023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by streams when they want to close the stream in both directions.
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SendRstStream(QuicStreamId id,
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             QuicRstStreamErrorCode error,
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             QuicStreamOffset bytes_written);
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when the session wants to go away and not accept any new streams.
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SendGoAway(QuicErrorCode error_code, const std::string& reason);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the stream associated with 'stream_id' from the active stream map.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void CloseStream(QuicStreamId stream_id);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if outgoing packets will be encrypted, even if the server
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // hasn't confirmed the handshake yet.
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsEncryptionEstablished();
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For a client, returns true if the server has confirmed our handshake. For
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a server, returns true if a full, valid client hello has been received.
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual bool IsCryptoHandshakeConfirmed();
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnConfigNegotiated();
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Called by the QuicCryptoStream when the handshake enters a new state.
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Clients will call this function in the order:
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   ENCRYPTION_FIRST_ESTABLISHED
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   zero or more ENCRYPTION_REESTABLISHED
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //   HANDSHAKE_CONFIRMED
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Servers will simply call it once with HANDSHAKE_CONFIRMED.
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called by the QuicCryptoStream when a handshake message is sent.
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnCryptoHandshakeMessageSent(
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const CryptoHandshakeMessage& message);
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Called by the QuicCryptoStream when a handshake message is received.
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void OnCryptoHandshakeMessageReceived(
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      const CryptoHandshakeMessage& message);
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns mutable config for this session. Returned config is owned
16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // by QuicSession.
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicConfig* config();
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the stream existed previously and has been closed.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns false if the stream is still active or if the stream has
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // not yet been created.
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsClosedStream(QuicStreamId id);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicConnection* connection() { return connection_.get(); }
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const QuicConnection* connection() const { return connection_.get(); }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t num_active_requests() const { return stream_map_.size(); }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const IPEndPoint& peer_address() const {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return connection_->peer_address();
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  QuicConnectionId connection_id() const {
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return connection_->connection_id();
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of currently open streams, including those which have
1820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // been implicitly created, but excluding the reserved headers and crypto
1830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // streams.
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual size_t GetNumOpenStreams() const;
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
186d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if the session has data to be sent, either queued in the
189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // connection, or in a write-blocked stream.
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool HasDataToWrite() const;
191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_received() const {
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return goaway_received_;
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_sent() const {
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return goaway_sent_;
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Gets the SSL connection information.
20123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  virtual bool GetSSLInfo(SSLInfo* ssl_info) const;
2027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicErrorCode error() const { return error_; }
204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool is_server() const { return connection_->is_server(); }
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  QuicFlowController* flow_controller() { return flow_controller_.get(); }
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  size_t get_max_open_streams() const { return max_open_streams_; }
2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap;
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new stream, owned by the caller, to handle a peer-initiated
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // stream.  Returns NULL and does error handling if the stream can not be
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // created.
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create a new stream, owned by the caller, to handle a locally-initiated
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stream.  Returns NULL if max streams have already been opened.
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual QuicDataStream* CreateOutgoingDataStream() = 0;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return the reserved crypto stream.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual QuicCryptoStream* GetCryptoStream() = 0;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds 'stream' to the active stream map.
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ActivateStream(QuicDataStream* stream);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the stream id for a new stream.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId GetNextStreamId();
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id);
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicDataStream* GetDataStream(const QuicStreamId stream_id);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  ReliableQuicStream* GetStream(const QuicStreamId stream_id);
2372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This is called after every call other than OnConnectionClose from the
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // QuicConnectionVisitor to allow post-processing once the work has been done.
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, it deletes streams given that it's safe to do so (no other
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // operations are being done on the streams at this time)
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void PostProcessAfterData();
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::hash_map<QuicStreamId, QuicDataStream*>* streams() {
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return &stream_map_;
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const {
249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return &stream_map_;
250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; }
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void set_max_open_streams(size_t max_open_streams);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<QuicHeadersStream> headers_stream_;
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class test::QuicSessionPeer;
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class VisitorShim;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
262424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Performs the work required to close |stream_id|.  If |locally_reset|
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // then the stream has been reset by this endpoint, not by the peer.
264424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
265424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
266f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // When a stream is closed locally, it may not yet know how many bytes the
267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // peer sent on that stream.
268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // When this data arrives (via stream frame w. FIN, or RST) this method
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // is called, and correctly updates the connection level flow controller.
270f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void UpdateFlowControlOnFinalReceivedByteOffset(
271f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      QuicStreamId id, QuicStreamOffset final_byte_offset);
272f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
273f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Called in OnConfigNegotiated when we receive a new stream level flow
274f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // control window in a negotiated config. Closes the connection if invalid.
275f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnNewStreamFlowControlWindow(uint32 new_window);
276f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
277f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Called in OnConfigNegotiated when we receive a new session level flow
278f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // control window in a negotiated config. Closes the connection if invalid.
279f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnNewSessionFlowControlWindow(uint32 new_window);
280f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
281f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Keep track of highest received byte offset of locally closed streams, while
282f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // waiting for a definitive final highest offset from the peer.
283f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::map<QuicStreamId, QuicStreamOffset>
284f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      locally_closed_streams_highest_offset_;
285f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<QuicConnection> connection_;
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A shim to stand between the connection and the session, to handle stream
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // deletions.
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<VisitorShim> visitor_shim_;
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<QuicDataStream*> closed_streams_;
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  QuicConfig config_;
29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the maximum number of streams this connection can open.
29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  size_t max_open_streams_;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Map from StreamId to pointers to streams that are owned by the caller.
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DataStreamMap stream_map_;
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId next_stream_id_;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set of stream ids that have been "implicitly created" by receipt
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of a stream id larger than the next expected stream id.
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::hash_set<QuicStreamId> implicitly_created_streams_;
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A list of streams which need to write more data.
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicWriteBlockedList write_blocked_streams_;
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  QuicStreamId largest_peer_created_stream_id_;
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The latched error with which the connection was closed.
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  QuicErrorCode error_;
314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Whether a GoAway has been received.
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_received_;
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Whether a GoAway has been sent.
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool goaway_sent_;
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
320d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Indicate if there is pending data for the crypto stream.
321d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool has_pending_handshake_;
322d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
323cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Used for session level flow control.
324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<QuicFlowController> flow_controller_;
325cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(QuicSession);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_QUIC_QUIC_SESSION_H_
332