rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / net / quic / quic_session.h
blobfa551bfab004641d016aded85a572be15c012b85
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.
7 #ifndef NET_QUIC_QUIC_SESSION_H_
8 #define NET_QUIC_QUIC_SESSION_H_
10 #include <vector>
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"
27 namespace net {
29 class QuicCryptoStream;
30 class ReliableQuicStream;
31 class SSLInfo;
32 class VisitorShim;
34 namespace test {
35 class QuicSessionPeer;
36 } // namespace test
38 class NET_EXPORT_PRIVATE QuicSession : public QuicConnectionVisitorInterface {
39 public:
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
44 // only.)
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.)
54 HANDSHAKE_CONFIRMED,
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 OnWindowUpdateFrames(
68 const std::vector<QuicWindowUpdateFrame>& frames) OVERRIDE;
69 virtual void OnBlockedFrames(
70 const std::vector<QuicBlockedFrame>& frames) OVERRIDE;
71 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer) OVERRIDE;
72 virtual void OnWriteBlocked() OVERRIDE {}
73 virtual void OnSuccessfulVersionNegotiation(
74 const QuicVersion& version) OVERRIDE {}
75 virtual void OnCanWrite() OVERRIDE;
76 virtual bool HasPendingWrites() const OVERRIDE;
77 virtual bool HasPendingHandshake() const OVERRIDE;
79 // Called by the headers stream when headers have been received for a stream.
80 virtual void OnStreamHeaders(QuicStreamId stream_id,
81 base::StringPiece headers_data);
82 // Called by the headers stream when headers with a priority have been
83 // received for this stream. This method will only be called for server
84 // streams.
85 virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
86 QuicPriority priority);
87 // Called by the headers stream when headers have been completely received
88 // for a stream. |fin| will be true if the fin flag was set in the headers
89 // frame.
90 virtual void OnStreamHeadersComplete(QuicStreamId stream_id,
91 bool fin,
92 size_t frame_len);
94 // Called by streams when they want to write data to the peer.
95 // Returns a pair with the number of bytes consumed from data, and a boolean
96 // indicating if the fin bit was consumed. This does not indicate the data
97 // has been sent on the wire: it may have been turned into a packet and queued
98 // if the socket was unexpectedly blocked.
99 // If provided, |ack_notifier_delegate| will be registered to be notified when
100 // we have seen ACKs for all packets resulting from this call. Not owned by
101 // this class.
102 virtual QuicConsumedData WritevData(
103 QuicStreamId id,
104 const IOVector& data,
105 QuicStreamOffset offset,
106 bool fin,
107 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
109 // Writes |headers| for the stream |id| to the dedicated headers stream.
110 // If |fin| is true, then no more data will be sent for the stream |id|.
111 size_t WriteHeaders(QuicStreamId id,
112 const SpdyHeaderBlock& headers,
113 bool fin);
115 // Called by streams when they want to close the stream in both directions.
116 virtual void SendRstStream(QuicStreamId id,
117 QuicRstStreamErrorCode error,
118 QuicStreamOffset bytes_written);
120 // Called when the session wants to go away and not accept any new streams.
121 void SendGoAway(QuicErrorCode error_code, const std::string& reason);
123 // Removes the stream associated with 'stream_id' from the active stream map.
124 virtual void CloseStream(QuicStreamId stream_id);
126 // Returns true if outgoing packets will be encrypted, even if the server
127 // hasn't confirmed the handshake yet.
128 virtual bool IsEncryptionEstablished();
130 // For a client, returns true if the server has confirmed our handshake. For
131 // a server, returns true if a full, valid client hello has been received.
132 virtual bool IsCryptoHandshakeConfirmed();
134 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
135 virtual void OnConfigNegotiated();
137 // Called by the QuicCryptoStream when the handshake enters a new state.
139 // Clients will call this function in the order:
140 // ENCRYPTION_FIRST_ESTABLISHED
141 // zero or more ENCRYPTION_REESTABLISHED
142 // HANDSHAKE_CONFIRMED
144 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
145 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event);
147 // Called by the QuicCryptoStream when a handshake message is sent.
148 virtual void OnCryptoHandshakeMessageSent(
149 const CryptoHandshakeMessage& message);
151 // Called by the QuicCryptoStream when a handshake message is received.
152 virtual void OnCryptoHandshakeMessageReceived(
153 const CryptoHandshakeMessage& message);
155 // Returns mutable config for this session. Returned config is owned
156 // by QuicSession.
157 QuicConfig* config();
159 // Returns true if the stream existed previously and has been closed.
160 // Returns false if the stream is still active or if the stream has
161 // not yet been created.
162 bool IsClosedStream(QuicStreamId id);
164 QuicConnection* connection() { return connection_.get(); }
165 const QuicConnection* connection() const { return connection_.get(); }
166 size_t num_active_requests() const { return stream_map_.size(); }
167 const IPEndPoint& peer_address() const {
168 return connection_->peer_address();
170 QuicGuid guid() const { return connection_->guid(); }
172 QuicPacketCreator::Options* options() { return connection()->options(); }
174 // Returns the number of currently open streams, including those which have
175 // been implicitly created.
176 virtual size_t GetNumOpenStreams() const;
178 void MarkWriteBlocked(QuicStreamId id, QuicPriority priority);
180 // Returns true if the session has data to be sent, either queued in the
181 // connection, or in a write-blocked stream.
182 bool HasDataToWrite() const;
184 // Marks that |stream_id| is blocked waiting to decompress the
185 // headers identified by |decompression_id|.
186 void MarkDecompressionBlocked(QuicHeaderId decompression_id,
187 QuicStreamId stream_id);
189 bool goaway_received() const {
190 return goaway_received_;
193 bool goaway_sent() const {
194 return goaway_sent_;
197 QuicSpdyDecompressor* decompressor() { return &decompressor_; }
198 QuicSpdyCompressor* compressor() { return &compressor_; }
200 // Gets the SSL connection information.
201 virtual bool GetSSLInfo(SSLInfo* ssl_info);
203 QuicErrorCode error() const { return error_; }
205 bool is_server() const { return connection_->is_server(); }
207 protected:
208 typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap;
210 // Creates a new stream, owned by the caller, to handle a peer-initiated
211 // stream. Returns NULL and does error handling if the stream can not be
212 // created.
213 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0;
215 // Create a new stream, owned by the caller, to handle a locally-initiated
216 // stream. Returns NULL if max streams have already been opened.
217 virtual QuicDataStream* CreateOutgoingDataStream() = 0;
219 // Return the reserved crypto stream.
220 virtual QuicCryptoStream* GetCryptoStream() = 0;
222 // Adds 'stream' to the active stream map.
223 virtual void ActivateStream(QuicDataStream* stream);
225 // Returns the stream id for a new stream.
226 QuicStreamId GetNextStreamId();
228 QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id);
230 QuicDataStream* GetDataStream(const QuicStreamId stream_id);
232 ReliableQuicStream* GetStream(const QuicStreamId stream_id);
234 // This is called after every call other than OnConnectionClose from the
235 // QuicConnectionVisitor to allow post-processing once the work has been done.
236 // In this case, it deletes streams given that it's safe to do so (no other
237 // operations are being done on the streams at this time)
238 virtual void PostProcessAfterData();
240 base::hash_map<QuicStreamId, QuicDataStream*>* streams() {
241 return &stream_map_;
244 const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const {
245 return &stream_map_;
248 std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; }
250 size_t get_max_open_streams() const {
251 return max_open_streams_;
254 private:
255 friend class test::QuicSessionPeer;
256 friend class VisitorShim;
258 // Performs the work required to close |stream_id|. If |locally_reset|
259 // then the stream has been reset by this endpoint, not by the peer. This
260 // means the stream may become a zombie stream which needs to stay
261 // around until headers have been decompressed.
262 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset);
264 // Adds |stream_id| to the zobmie stream map, closing the oldest
265 // zombie stream if the set is full.
266 void AddZombieStream(QuicStreamId stream_id);
268 // Closes the zombie stream |stream_id| and removes it from the zombie
269 // stream map.
270 void CloseZombieStream(QuicStreamId stream_id);
272 // Adds |stream_id| to the prematurely closed stream map, removing the
273 // oldest prematurely closed stream if the set is full.
274 void AddPrematurelyClosedStream(QuicStreamId stream_id);
276 scoped_ptr<QuicConnection> connection_;
278 scoped_ptr<QuicHeadersStream> headers_stream_;
280 // Tracks the last 20 streams which closed without decompressing headers.
281 // This is for best-effort detection of an unrecoverable compression context.
282 // Ideally this would be a linked_hash_set as the boolean is unused.
283 linked_hash_map<QuicStreamId, bool> prematurely_closed_streams_;
285 // Streams which have been locally reset before decompressing headers
286 // from the peer. These streams need to stay open long enough to
287 // process any headers from the peer.
288 // Ideally this would be a linked_hash_set as the boolean is unused.
289 linked_hash_map<QuicStreamId, bool> zombie_streams_;
291 // A shim to stand between the connection and the session, to handle stream
292 // deletions.
293 scoped_ptr<VisitorShim> visitor_shim_;
295 std::vector<QuicDataStream*> closed_streams_;
297 QuicSpdyDecompressor decompressor_;
298 QuicSpdyCompressor compressor_;
300 QuicConfig config_;
302 // Returns the maximum number of streams this connection can open.
303 size_t max_open_streams_;
305 // Map from StreamId to pointers to streams that are owned by the caller.
306 DataStreamMap stream_map_;
307 QuicStreamId next_stream_id_;
309 // Set of stream ids that have been "implicitly created" by receipt
310 // of a stream id larger than the next expected stream id.
311 base::hash_set<QuicStreamId> implicitly_created_streams_;
313 // A list of streams which need to write more data.
314 QuicWriteBlockedList write_blocked_streams_;
316 // A map of headers waiting to be compressed, and the streams
317 // they are associated with.
318 map<uint32, QuicStreamId> decompression_blocked_streams_;
320 QuicStreamId largest_peer_created_stream_id_;
322 // The latched error with which the connection was closed.
323 QuicErrorCode error_;
325 // Whether a GoAway has been received.
326 bool goaway_received_;
327 // Whether a GoAway has been sent.
328 bool goaway_sent_;
330 // Indicate if there is pending data for the crypto stream.
331 bool has_pending_handshake_;
333 DISALLOW_COPY_AND_ASSIGN(QuicSession);
336 } // namespace net
338 #endif // NET_QUIC_QUIC_SESSION_H_