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.
5 // A QuicSession, which demuxes a single connection to individual streams.
7 #ifndef NET_QUIC_QUIC_SESSION_H_
8 #define NET_QUIC_QUIC_SESSION_H_
12 #include "base/compiler_specific.h"
13 #include "base/containers/hash_tables.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/quic_connection.h"
17 #include "net/quic/quic_crypto_stream.h"
18 #include "net/quic/quic_packet_creator.h"
19 #include "net/quic/quic_protocol.h"
20 #include "net/quic/quic_spdy_compressor.h"
21 #include "net/quic/quic_spdy_decompressor.h"
22 #include "net/quic/reliable_quic_stream.h"
23 #include "net/spdy/write_blocked_list.h"
27 class QuicCryptoStream;
28 class ReliableQuicStream;
33 class QuicSessionPeer;
36 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
38 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
39 enum CryptoHandshakeEvent {
40 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
41 // sent by a client and that subsequent packets will be encrypted. (Client
43 ENCRYPTION_FIRST_ESTABLISHED,
44 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
45 // the server and thus the encryption key has been updated. Therefore the
46 // connection should resend any packets that were sent under
47 // ENCRYPTION_INITIAL. (Client only.)
48 ENCRYPTION_REESTABLISHED,
49 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
50 // our handshake. In a server it indicates that a full, valid client hello
51 // has been received. (Client and server.)
55 QuicSession(QuicConnection* connection,
56 const QuicConfig& config,
59 virtual ~QuicSession();
61 // QuicConnectionVisitorInterface methods:
62 virtual bool OnStreamFrames(
63 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
64 virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
65 virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
66 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
67 virtual void OnSuccessfulVersionNegotiation(
68 const QuicVersion& version) OVERRIDE{}
69 virtual void OnConfigNegotiated() OVERRIDE;
70 // Not needed for HTTP.
71 virtual bool OnCanWrite() OVERRIDE;
72 virtual bool HasPendingHandshake() const OVERRIDE;
74 // Called by streams when they want to write data to the peer.
75 // Returns a pair with the number of bytes consumed from data, and a boolean
76 // indicating if the fin bit was consumed. This does not indicate the data
77 // has been sent on the wire: it may have been turned into a packet and queued
78 // if the socket was unexpectedly blocked.
79 virtual QuicConsumedData WritevData(QuicStreamId id,
80 const struct iovec* iov,
82 QuicStreamOffset offset,
85 // Called by streams when they want to close the stream in both directions.
86 virtual void SendRstStream(QuicStreamId id, QuicRstStreamErrorCode error);
88 // Called when the session wants to go away and not accept any new streams.
89 void SendGoAway(QuicErrorCode error_code, const std::string& reason);
91 // Removes the stream associated with 'stream_id' from the active stream map.
92 virtual void CloseStream(QuicStreamId stream_id);
94 // Returns true if outgoing packets will be encrypted, even if the server
95 // hasn't confirmed the handshake yet.
96 virtual bool IsEncryptionEstablished();
98 // For a client, returns true if the server has confirmed our handshake. For
99 // a server, returns true if a full, valid client hello has been received.
100 virtual bool IsCryptoHandshakeConfirmed();
102 // Called by the QuicCryptoStream when the handshake enters a new state.
104 // Clients will call this function in the order:
105 // ENCRYPTION_FIRST_ESTABLISHED
106 // zero or more ENCRYPTION_REESTABLISHED
107 // HANDSHAKE_CONFIRMED
109 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
110 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
112 // Called by the QuicCryptoStream when a handshake message is sent.
113 virtual void OnCryptoHandshakeMessageSent(
114 const CryptoHandshakeMessage& message);
116 // Called by the QuicCryptoStream when a handshake message is received.
117 virtual void OnCryptoHandshakeMessageReceived(
118 const CryptoHandshakeMessage& message);
120 // Returns mutable config for this session. Returned config is owned
122 QuicConfig* config();
124 // Returns true if the stream existed previously and has been closed.
125 // Returns false if the stream is still active or if the stream has
126 // not yet been created.
127 bool IsClosedStream(QuicStreamId id);
129 QuicConnection* connection() { return connection_.get(); }
130 const QuicConnection* connection() const { return connection_.get(); }
131 size_t num_active_requests() const { return stream_map_.size(); }
132 const IPEndPoint& peer_address() const {
133 return connection_->peer_address();
135 QuicGuid guid() const { return connection_->guid(); }
137 QuicPacketCreator::Options* options() { return connection()->options(); }
139 // Returns the number of currently open streams, including those which have
140 // been implicitly created.
141 virtual size_t GetNumOpenStreams() const;
143 void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
145 // Marks that |stream_id| is blocked waiting to decompress the
146 // headers identified by |decompression_id|.
147 void MarkDecompressionBlocked(QuicHeaderId decompression_id,
148 QuicStreamId stream_id);
150 bool goaway_received() const {
151 return goaway_received_;
154 bool goaway_sent() const {
158 QuicSpdyDecompressor* decompressor() { return &decompressor_; }
159 QuicSpdyCompressor* compressor() { return &compressor_; }
161 // Gets the SSL connection information.
162 virtual bool GetSSLInfo(SSLInfo* ssl_info);
164 QuicErrorCode error() const { return error_; }
166 bool is_server() const { return is_server_; }
169 // Creates a new stream, owned by the caller, to handle a peer-initiated
170 // stream. Returns NULL and does error handling if the stream can not be
172 virtual ReliableQuicStream* CreateIncomingReliableStream(QuicStreamId id) = 0;
174 // Create a new stream, owned by the caller, to handle a locally-initiated
175 // stream. Returns NULL if max streams have already been opened.
176 virtual ReliableQuicStream* CreateOutgoingReliableStream() = 0;
178 // Return the reserved crypto stream.
179 virtual QuicCryptoStream* GetCryptoStream() = 0;
181 // Adds 'stream' to the active stream map.
182 virtual void ActivateStream(ReliableQuicStream* stream);
184 // Returns the stream id for a new stream.
185 QuicStreamId GetNextStreamId();
187 ReliableQuicStream* GetIncomingReliableStream(QuicStreamId stream_id);
189 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
191 // This is called after every call other than OnConnectionClose from the
192 // QuicConnectionVisitor to allow post-processing once the work has been done.
193 // In this case, it deletes streams given that it's safe to do so (no other
194 // operations are being done on the streams at this time)
195 virtual void PostProcessAfterData();
197 base::hash_map<QuicStreamId, ReliableQuicStream*>* streams() {
201 const base::hash_map<QuicStreamId, ReliableQuicStream*>* streams() const {
205 std::vector<ReliableQuicStream*>* closed_streams() {
206 return &closed_streams_;
209 size_t get_max_open_streams() const {
210 return max_open_streams_;
214 friend class test::QuicSessionPeer;
215 friend class VisitorShim;
217 typedef base::hash_map<QuicStreamId, ReliableQuicStream*> ReliableStreamMap;
219 // Performs the work required to close |stream_id|. If |locally_reset|
220 // then the stream has been reset by this endpoint, not by the peer. This
221 // means the stream may become a zombie stream which needs to stay
222 // around until headers have been decompressed.
223 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
225 // Adds |stream_id| to the zobmie stream map, closing the oldest
226 // zombie stream if the set is full.
227 void AddZombieStream(QuicStreamId stream_id);
229 // Closes the zombie stream |stream_id| and removes it from the zombie
231 void CloseZombieStream(QuicStreamId stream_id);
233 // Adds |stream_id| to the prematurely closed stream map, removing the
234 // oldest prematurely closed stream if the set is full.
235 void AddPrematurelyClosedStream(QuicStreamId stream_id);
237 scoped_ptr<QuicConnection> connection_;
239 // Tracks the last 20 streams which closed without decompressing headers.
240 // This is for best-effort detection of an unrecoverable compression context.
241 // Ideally this would be a linked_hash_set as the boolean is unused.
242 linked_hash_map<QuicStreamId, bool> prematurely_closed_streams_;
244 // Streams which have been locally reset before decompressing headers
245 // from the peer. These streams need to stay open long enough to
246 // process any headers from the peer.
247 // Ideally this would be a linked_hash_set as the boolean is unused.
248 linked_hash_map<QuicStreamId, bool> zombie_streams_;
250 // A shim to stand between the connection and the session, to handle stream
252 scoped_ptr<VisitorShim> visitor_shim_;
254 std::vector<ReliableQuicStream*> closed_streams_;
256 QuicSpdyDecompressor decompressor_;
257 QuicSpdyCompressor compressor_;
261 // Returns the maximum number of streams this connection can open.
262 size_t max_open_streams_;
264 // Map from StreamId to pointers to streams that are owned by the caller.
265 ReliableStreamMap stream_map_;
266 QuicStreamId next_stream_id_;
269 // Set of stream ids that have been "implicitly created" by receipt
270 // of a stream id larger than the next expected stream id.
271 base::hash_set<QuicStreamId> implicitly_created_streams_;
273 // A list of streams which need to write more data.
274 WriteBlockedList<QuicStreamId> write_blocked_streams_;
276 // A map of headers waiting to be compressed, and the streams
277 // they are associated with.
278 map<uint32, QuicStreamId> decompression_blocked_streams_;
280 QuicStreamId largest_peer_created_stream_id_;
282 // The latched error with which the connection was closed.
283 QuicErrorCode error_;
285 // Whether a GoAway has been received.
286 bool goaway_received_;
287 // Whether a GoAway has been sent.
290 // Indicate if there is pending data for the crypto stream.
291 bool has_pending_handshake_;
293 DISALLOW_COPY_AND_ASSIGN(QuicSession);
298 #endif // NET_QUIC_QUIC_SESSION_H_