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_
14 #include "base/compiler_specific.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_piece.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/quic/quic_connection.h"
20 #include "net/quic/quic_crypto_stream.h"
21 #include "net/quic/quic_data_stream.h"
22 #include "net/quic/quic_headers_stream.h"
23 #include "net/quic/quic_packet_creator.h"
24 #include "net/quic/quic_protocol.h"
25 #include "net/quic/quic_write_blocked_list.h"
26 #include "net/quic/reliable_quic_stream.h"
30 class QuicCryptoStream
;
31 class QuicFlowController
;
32 class ReliableQuicStream
;
36 class QuicSessionPeer
;
39 class NET_EXPORT_PRIVATE QuicSession
: public QuicConnectionVisitorInterface
{
41 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
42 enum CryptoHandshakeEvent
{
43 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
44 // sent by a client and that subsequent packets will be encrypted. (Client
46 ENCRYPTION_FIRST_ESTABLISHED
,
47 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
48 // the server and thus the encryption key has been updated. Therefore the
49 // connection should resend any packets that were sent under
50 // ENCRYPTION_INITIAL. (Client only.)
51 ENCRYPTION_REESTABLISHED
,
52 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
53 // our handshake. In a server it indicates that a full, valid client hello
54 // has been received. (Client and server.)
58 QuicSession(QuicConnection
* connection
, const QuicConfig
& config
);
59 virtual void Initialize();
61 ~QuicSession() override
;
63 // QuicConnectionVisitorInterface methods:
64 void OnStreamFrames(const std::vector
<QuicStreamFrame
>& frames
) override
;
65 void OnRstStream(const QuicRstStreamFrame
& frame
) override
;
66 void OnGoAway(const QuicGoAwayFrame
& frame
) override
;
67 void OnWindowUpdateFrames(
68 const std::vector
<QuicWindowUpdateFrame
>& frames
) override
;
69 void OnBlockedFrames(const std::vector
<QuicBlockedFrame
>& frames
) override
;
70 void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) override
;
71 void OnWriteBlocked() override
{}
72 void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) override
;
73 void OnCanWrite() override
;
74 void OnCongestionWindowChange(QuicTime now
) override
{}
75 bool WillingAndAbleToWrite() const override
;
76 bool HasPendingHandshake() const override
;
77 bool HasOpenDataStreams() 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
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
90 virtual void OnStreamHeadersComplete(QuicStreamId stream_id
,
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. |fec_protection| indicates if
99 // data is to be FEC protected. Note that data that is sent immediately
100 // following MUST_FEC_PROTECT data may get protected by falling within the
102 // If provided, |ack_notifier_delegate| will be registered to be notified when
103 // we have seen ACKs for all packets resulting from this call.
104 virtual QuicConsumedData
WritevData(
106 const IOVector
& data
,
107 QuicStreamOffset offset
,
109 FecProtection fec_protection
,
110 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
112 // Writes |headers| for the stream |id| to the dedicated headers stream.
113 // If |fin| is true, then no more data will be sent for the stream |id|.
114 // If provided, |ack_notifier_delegate| will be registered to be notified when
115 // we have seen ACKs for all packets resulting from this call.
118 const SpdyHeaderBlock
& headers
,
120 QuicPriority priority
,
121 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
123 // Called by streams when they want to close the stream in both directions.
124 virtual void SendRstStream(QuicStreamId id
,
125 QuicRstStreamErrorCode error
,
126 QuicStreamOffset bytes_written
);
128 // Called when the session wants to go away and not accept any new streams.
129 void SendGoAway(QuicErrorCode error_code
, const std::string
& reason
);
131 // Removes the stream associated with 'stream_id' from the active stream map.
132 virtual void CloseStream(QuicStreamId stream_id
);
134 // Returns true if outgoing packets will be encrypted, even if the server
135 // hasn't confirmed the handshake yet.
136 virtual bool IsEncryptionEstablished();
138 // For a client, returns true if the server has confirmed our handshake. For
139 // a server, returns true if a full, valid client hello has been received.
140 virtual bool IsCryptoHandshakeConfirmed();
142 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
143 virtual void OnConfigNegotiated();
145 // Called by the QuicCryptoStream when the handshake enters a new state.
147 // Clients will call this function in the order:
148 // ENCRYPTION_FIRST_ESTABLISHED
149 // zero or more ENCRYPTION_REESTABLISHED
150 // HANDSHAKE_CONFIRMED
152 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
153 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event
);
155 // Called by the QuicCryptoStream when a handshake message is sent.
156 virtual void OnCryptoHandshakeMessageSent(
157 const CryptoHandshakeMessage
& message
);
159 // Called by the QuicCryptoStream when a handshake message is received.
160 virtual void OnCryptoHandshakeMessageReceived(
161 const CryptoHandshakeMessage
& message
);
163 // Returns mutable config for this session. Returned config is owned
165 QuicConfig
* config();
167 // Returns true if the stream existed previously and has been closed.
168 // Returns false if the stream is still active or if the stream has
169 // not yet been created.
170 bool IsClosedStream(QuicStreamId id
);
172 QuicConnection
* connection() { return connection_
.get(); }
173 const QuicConnection
* connection() const { return connection_
.get(); }
174 size_t num_active_requests() const { return stream_map_
.size(); }
175 const IPEndPoint
& peer_address() const {
176 return connection_
->peer_address();
178 QuicConnectionId
connection_id() const {
179 return connection_
->connection_id();
182 // Returns the number of currently open streams, including those which have
183 // been implicitly created, but excluding the reserved headers and crypto
185 virtual size_t GetNumOpenStreams() const;
187 void MarkWriteBlocked(QuicStreamId id
, QuicPriority priority
);
189 // Returns true if the session has data to be sent, either queued in the
190 // connection, or in a write-blocked stream.
191 bool HasDataToWrite() const;
193 bool goaway_received() const { return goaway_received_
; }
195 bool goaway_sent() const { return goaway_sent_
; }
197 QuicErrorCode
error() const { return error_
; }
199 Perspective
perspective() const { return connection_
->perspective(); }
201 QuicFlowController
* flow_controller() { return &flow_controller_
; }
203 // Returns true if connection is flow controller blocked.
204 bool IsConnectionFlowControlBlocked() const;
206 // Returns true if any stream is flow controller blocked.
207 bool IsStreamFlowControlBlocked();
209 // Returns true if this is a secure QUIC session.
210 bool IsSecure() const { return connection()->is_secure(); }
212 size_t get_max_open_streams() const { return max_open_streams_
; }
215 const QuicHeadersStream
* headers_stream() { return headers_stream_
.get(); }
218 typedef base::hash_map
<QuicStreamId
, QuicDataStream
*> DataStreamMap
;
220 // Creates a new stream, owned by the caller, to handle a peer-initiated
221 // stream. Returns nullptr and does error handling if the stream can not be
223 virtual QuicDataStream
* CreateIncomingDataStream(QuicStreamId id
) = 0;
225 // Create a new stream, owned by the caller, to handle a locally-initiated
226 // stream. Returns nullptr if max streams have already been opened.
227 virtual QuicDataStream
* CreateOutgoingDataStream() = 0;
229 // Return the reserved crypto stream.
230 virtual QuicCryptoStream
* GetCryptoStream() = 0;
232 // Adds 'stream' to the active stream map.
233 virtual void ActivateStream(QuicDataStream
* stream
);
235 // Returns the stream id for a new stream.
236 QuicStreamId
GetNextStreamId();
238 QuicDataStream
* GetIncomingDataStream(QuicStreamId stream_id
);
240 QuicDataStream
* GetDataStream(const QuicStreamId stream_id
);
242 ReliableQuicStream
* GetStream(const QuicStreamId stream_id
);
244 // This is called after every call other than OnConnectionClose from the
245 // QuicConnectionVisitor to allow post-processing once the work has been done.
246 // In this case, it deletes streams given that it's safe to do so (no other
247 // operations are being done on the streams at this time)
248 virtual void PostProcessAfterData();
250 base::hash_map
<QuicStreamId
, QuicDataStream
*>* streams() {
254 const base::hash_map
<QuicStreamId
, QuicDataStream
*>* streams() const {
258 std::vector
<QuicDataStream
*>* closed_streams() { return &closed_streams_
; }
260 void set_max_open_streams(size_t max_open_streams
);
262 scoped_ptr
<QuicHeadersStream
> headers_stream_
;
265 friend class test::QuicSessionPeer
;
266 friend class VisitorShim
;
268 // Performs the work required to close |stream_id|. If |locally_reset|
269 // then the stream has been reset by this endpoint, not by the peer.
270 void CloseStreamInner(QuicStreamId stream_id
, bool locally_reset
);
272 // When a stream is closed locally, it may not yet know how many bytes the
273 // peer sent on that stream.
274 // When this data arrives (via stream frame w. FIN, or RST) this method
275 // is called, and correctly updates the connection level flow controller.
276 void UpdateFlowControlOnFinalReceivedByteOffset(
277 QuicStreamId id
, QuicStreamOffset final_byte_offset
);
279 // Called in OnConfigNegotiated when we receive a new stream level flow
280 // control window in a negotiated config. Closes the connection if invalid.
281 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window
);
283 // Called in OnConfigNegotiated when we receive a new session level flow
284 // control window in a negotiated config. Closes the connection if invalid.
285 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window
);
287 // Called in OnConfigNegotiated when auto-tuning is enabled for flow
288 // control receive windows.
289 void EnableAutoTuneReceiveWindow();
291 // Keep track of highest received byte offset of locally closed streams, while
292 // waiting for a definitive final highest offset from the peer.
293 std::map
<QuicStreamId
, QuicStreamOffset
>
294 locally_closed_streams_highest_offset_
;
296 scoped_ptr
<QuicConnection
> connection_
;
298 // A shim to stand between the connection and the session, to handle stream
300 scoped_ptr
<VisitorShim
> visitor_shim_
;
302 std::vector
<QuicDataStream
*> closed_streams_
;
306 // Returns the maximum number of streams this connection can open.
307 size_t max_open_streams_
;
309 // Map from StreamId to pointers to streams that are owned by the caller.
310 DataStreamMap stream_map_
;
311 QuicStreamId next_stream_id_
;
313 // Set of stream ids that have been "implicitly created" by receipt
314 // of a stream id larger than the next expected stream id.
315 base::hash_set
<QuicStreamId
> implicitly_created_streams_
;
317 // A list of streams which need to write more data.
318 QuicWriteBlockedList write_blocked_streams_
;
320 QuicStreamId largest_peer_created_stream_id_
;
322 // The latched error with which the connection was closed.
323 QuicErrorCode error_
;
325 // Used for session level flow control.
326 QuicFlowController flow_controller_
;
328 // Whether a GoAway has been received.
329 bool goaway_received_
;
330 // Whether a GoAway has been sent.
333 // Indicate if there is pending data for the crypto stream.
334 bool has_pending_handshake_
;
336 DISALLOW_COPY_AND_ASSIGN(QuicSession
);
341 #endif // NET_QUIC_QUIC_SESSION_H_