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_data_stream.h"
19 #include "net/quic/quic_headers_stream.h"
20 #include "net/quic/quic_packet_creator.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_spdy_compressor.h"
23 #include "net/quic/quic_spdy_decompressor.h"
24 #include "net/quic/quic_write_blocked_list.h"
25 #include "net/quic/reliable_quic_stream.h"
29 class QuicCryptoStream;
30 class ReliableQuicStream;
35 class QuicSessionPeer;
38 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
40 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
41 enum CryptoHandshakeEvent {
42 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
43 // sent by a client and that subsequent packets will be encrypted. (Client
45 ENCRYPTION_FIRST_ESTABLISHED,
46 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
47 // the server and thus the encryption key has been updated. Therefore the
48 // connection should resend any packets that were sent under
49 // ENCRYPTION_INITIAL. (Client only.)
50 ENCRYPTION_REESTABLISHED,
51 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
52 // our handshake. In a server it indicates that a full, valid client hello
53 // has been received. (Client and server.)
57 QuicSession(QuicConnection* connection,
58 const QuicConfig& config);
60 virtual ~QuicSession();
62 // QuicConnectionVisitorInterface methods:
63 virtual bool OnStreamFrames(
64 const std::vector<QuicStreamFrame>& frames) OVERRIDE;
65 virtual void OnRstStream(const QuicRstStreamFrame& frame) OVERRIDE;
66 virtual void OnGoAway(const QuicGoAwayFrame& frame) OVERRIDE;
67 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
68 virtual void OnWriteBlocked() OVERRIDE {}
69 virtual void OnSuccessfulVersionNegotiation(
70 const QuicVersion& version) OVERRIDE {}
71 virtual bool OnCanWrite() OVERRIDE;
72 virtual bool HasPendingHandshake() const OVERRIDE;
74 // Called by the headers stream when headers have been received for a stream.
75 virtual void OnStreamHeaders(QuicStreamId stream_id,
76 base::StringPiece headers_data);
77 // Called by the headers stream when headers with a priority have been
78 // received for this stream. This method will only be called for server
80 virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
81 QuicPriority priority);
82 // Called by the headers stream when headers have been completely received
83 // for a stream. |fin| will be true if the fin flag was set in the headers
85 virtual void OnStreamHeadersComplete(QuicStreamId stream_id,
89 // Called by streams when they want to write data to the peer.
90 // Returns a pair with the number of bytes consumed from data, and a boolean
91 // indicating if the fin bit was consumed. This does not indicate the data
92 // has been sent on the wire: it may have been turned into a packet and queued
93 // if the socket was unexpectedly blocked.
94 // If provided, |ack_notifier_delegate| will be registered to be notified when
95 // we have seen ACKs for all packets resulting from this call. Not owned by
97 virtual QuicConsumedData WritevData(
99 const struct iovec* iov,
101 QuicStreamOffset offset,
103 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
105 // Writes |headers| for the stream |id| to the dedicated headers stream.
106 // If |fin| is true, then no more data will be sent for the stream |id|.
107 size_t WriteHeaders(QuicStreamId id,
108 const SpdyHeaderBlock& headers,
111 // Called by streams when they want to close the stream in both directions.
112 virtual void SendRstStream(QuicStreamId id,
113 QuicRstStreamErrorCode error,
114 QuicStreamOffset bytes_written);
116 // Called when the session wants to go away and not accept any new streams.
117 void SendGoAway(QuicErrorCode error_code, const std::string& reason);
119 // Removes the stream associated with 'stream_id' from the active stream map.
120 virtual void CloseStream(QuicStreamId stream_id);
122 // Returns true if outgoing packets will be encrypted, even if the server
123 // hasn't confirmed the handshake yet.
124 virtual bool IsEncryptionEstablished();
126 // For a client, returns true if the server has confirmed our handshake. For
127 // a server, returns true if a full, valid client hello has been received.
128 virtual bool IsCryptoHandshakeConfirmed();
130 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
131 virtual void OnConfigNegotiated();
133 // Called by the QuicCryptoStream when the handshake enters a new state.
135 // Clients will call this function in the order:
136 // ENCRYPTION_FIRST_ESTABLISHED
137 // zero or more ENCRYPTION_REESTABLISHED
138 // HANDSHAKE_CONFIRMED
140 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
141 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
143 // Called by the QuicCryptoStream when a handshake message is sent.
144 virtual void OnCryptoHandshakeMessageSent(
145 const CryptoHandshakeMessage& message);
147 // Called by the QuicCryptoStream when a handshake message is received.
148 virtual void OnCryptoHandshakeMessageReceived(
149 const CryptoHandshakeMessage& message);
151 // Returns mutable config for this session. Returned config is owned
153 QuicConfig* config();
155 // Returns true if the stream existed previously and has been closed.
156 // Returns false if the stream is still active or if the stream has
157 // not yet been created.
158 bool IsClosedStream(QuicStreamId id);
160 QuicConnection* connection() { return connection_.get(); }
161 const QuicConnection* connection() const { return connection_.get(); }
162 size_t num_active_requests() const { return stream_map_.size(); }
163 const IPEndPoint& peer_address() const {
164 return connection_->peer_address();
166 QuicGuid guid() const { return connection_->guid(); }
168 QuicPacketCreator::Options* options() { return connection()->options(); }
170 // Returns the number of currently open streams, including those which have
171 // been implicitly created.
172 virtual size_t GetNumOpenStreams() const;
174 void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
176 // Returns true if the session has data to be sent, either queued in the
177 // connection, or in a write-blocked stream.
178 bool HasDataToWrite() const;
180 // Marks that |stream_id| is blocked waiting to decompress the
181 // headers identified by |decompression_id|.
182 void MarkDecompressionBlocked(QuicHeaderId decompression_id,
183 QuicStreamId stream_id);
185 bool goaway_received() const {
186 return goaway_received_;
189 bool goaway_sent() const {
193 QuicSpdyDecompressor* decompressor() { return &decompressor_; }
194 QuicSpdyCompressor* compressor() { return &compressor_; }
196 // Gets the SSL connection information.
197 virtual bool GetSSLInfo(SSLInfo* ssl_info);
199 QuicErrorCode error() const { return error_; }
201 bool is_server() const { return connection_->is_server(); }
204 typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap;
206 // Creates a new stream, owned by the caller, to handle a peer-initiated
207 // stream. Returns NULL and does error handling if the stream can not be
209 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0;
211 // Create a new stream, owned by the caller, to handle a locally-initiated
212 // stream. Returns NULL if max streams have already been opened.
213 virtual QuicDataStream* CreateOutgoingDataStream() = 0;
215 // Return the reserved crypto stream.
216 virtual QuicCryptoStream* GetCryptoStream() = 0;
218 // Adds 'stream' to the active stream map.
219 virtual void ActivateStream(QuicDataStream* stream);
221 // Returns the stream id for a new stream.
222 QuicStreamId GetNextStreamId();
224 QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id);
226 QuicDataStream* GetDataStream(const QuicStreamId stream_id);
228 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
230 // This is called after every call other than OnConnectionClose from the
231 // QuicConnectionVisitor to allow post-processing once the work has been done.
232 // In this case, it deletes streams given that it's safe to do so (no other
233 // operations are being done on the streams at this time)
234 virtual void PostProcessAfterData();
236 base::hash_map<QuicStreamId, QuicDataStream*>* streams() {
240 const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const {
244 std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; }
246 size_t get_max_open_streams() const {
247 return max_open_streams_;
251 friend class test::QuicSessionPeer;
252 friend class VisitorShim;
254 // Performs the work required to close |stream_id|. If |locally_reset|
255 // then the stream has been reset by this endpoint, not by the peer. This
256 // means the stream may become a zombie stream which needs to stay
257 // around until headers have been decompressed.
258 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
260 // Adds |stream_id| to the zobmie stream map, closing the oldest
261 // zombie stream if the set is full.
262 void AddZombieStream(QuicStreamId stream_id);
264 // Closes the zombie stream |stream_id| and removes it from the zombie
266 void CloseZombieStream(QuicStreamId stream_id);
268 // Adds |stream_id| to the prematurely closed stream map, removing the
269 // oldest prematurely closed stream if the set is full.
270 void AddPrematurelyClosedStream(QuicStreamId stream_id);
272 scoped_ptr<QuicConnection> connection_;
274 scoped_ptr<QuicHeadersStream> headers_stream_;
276 // Tracks the last 20 streams which closed without decompressing headers.
277 // This is for best-effort detection of an unrecoverable compression context.
278 // Ideally this would be a linked_hash_set as the boolean is unused.
279 linked_hash_map<QuicStreamId, bool> prematurely_closed_streams_;
281 // Streams which have been locally reset before decompressing headers
282 // from the peer. These streams need to stay open long enough to
283 // process any headers from the peer.
284 // Ideally this would be a linked_hash_set as the boolean is unused.
285 linked_hash_map<QuicStreamId, bool> zombie_streams_;
287 // A shim to stand between the connection and the session, to handle stream
289 scoped_ptr<VisitorShim> visitor_shim_;
291 std::vector<QuicDataStream*> closed_streams_;
293 QuicSpdyDecompressor decompressor_;
294 QuicSpdyCompressor compressor_;
298 // Returns the maximum number of streams this connection can open.
299 size_t max_open_streams_;
301 // Map from StreamId to pointers to streams that are owned by the caller.
302 DataStreamMap stream_map_;
303 QuicStreamId next_stream_id_;
305 // Set of stream ids that have been "implicitly created" by receipt
306 // of a stream id larger than the next expected stream id.
307 base::hash_set<QuicStreamId> implicitly_created_streams_;
309 // A list of streams which need to write more data.
310 QuicWriteBlockedList write_blocked_streams_;
312 // A map of headers waiting to be compressed, and the streams
313 // they are associated with.
314 map<uint32, QuicStreamId> decompression_blocked_streams_;
316 QuicStreamId largest_peer_created_stream_id_;
318 // The latched error with which the connection was closed.
319 QuicErrorCode error_;
321 // Whether a GoAway has been received.
322 bool goaway_received_;
323 // Whether a GoAway has been sent.
326 // Indicate if there is pending data for the crypto stream.
327 bool has_pending_handshake_;
329 DISALLOW_COPY_AND_ASSIGN(QuicSession);
334 #endif // NET_QUIC_QUIC_SESSION_H_