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_packet_creator.h"
22 #include "net/quic/quic_protocol.h"
23 #include "net/quic/quic_write_blocked_list.h"
24 #include "net/quic/reliable_quic_stream.h"
28 class QuicCryptoStream
;
29 class QuicFlowController
;
30 class ReliableQuicStream
;
34 class QuicSessionPeer
;
37 class NET_EXPORT_PRIVATE QuicSession
: public QuicConnectionVisitorInterface
{
39 // CryptoHandshakeEvent enumerates the events generated by a QuicCryptoStream.
40 enum CryptoHandshakeEvent
{
41 // ENCRYPTION_FIRST_ESTABLISHED indicates that a full client hello has been
42 // sent by a client and that subsequent packets will be encrypted. (Client
44 ENCRYPTION_FIRST_ESTABLISHED
,
45 // ENCRYPTION_REESTABLISHED indicates that a client hello was rejected by
46 // the server and thus the encryption key has been updated. Therefore the
47 // connection should resend any packets that were sent under
48 // ENCRYPTION_INITIAL. (Client only.)
49 ENCRYPTION_REESTABLISHED
,
50 // HANDSHAKE_CONFIRMED, in a client, indicates the the server has accepted
51 // our handshake. In a server it indicates that a full, valid client hello
52 // has been received. (Client and server.)
56 QuicSession(QuicConnection
* connection
, const QuicConfig
& config
);
58 ~QuicSession() override
;
60 virtual void Initialize();
62 // QuicConnectionVisitorInterface methods:
63 void OnStreamFrame(const QuicStreamFrame
& frame
) override
;
64 void OnRstStream(const QuicRstStreamFrame
& frame
) override
;
65 void OnGoAway(const QuicGoAwayFrame
& frame
) override
;
66 void OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
;
67 void OnBlockedFrame(const QuicBlockedFrame
& frame
) override
;
68 void OnConnectionClosed(QuicErrorCode error
, bool from_peer
) override
;
69 void OnWriteBlocked() override
{}
70 void OnSuccessfulVersionNegotiation(const QuicVersion
& version
) override
;
71 void OnCanWrite() override
;
72 void OnCongestionWindowChange(QuicTime now
) override
{}
73 bool WillingAndAbleToWrite() const override
;
74 bool HasPendingHandshake() const override
;
75 bool HasOpenDynamicStreams() const override
;
77 // Called by streams when they want to write data to the peer.
78 // Returns a pair with the number of bytes consumed from data, and a boolean
79 // indicating if the fin bit was consumed. This does not indicate the data
80 // has been sent on the wire: it may have been turned into a packet and queued
81 // if the socket was unexpectedly blocked. |fec_protection| indicates if
82 // data is to be FEC protected. Note that data that is sent immediately
83 // following MUST_FEC_PROTECT data may get protected by falling within the
85 // If provided, |ack_notifier_delegate| will be registered to be notified when
86 // we have seen ACKs for all packets resulting from this call.
87 virtual QuicConsumedData
WritevData(
89 const QuicIOVector
& iov
,
90 QuicStreamOffset offset
,
92 FecProtection fec_protection
,
93 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
95 // Called by streams when they want to close the stream in both directions.
96 virtual void SendRstStream(QuicStreamId id
,
97 QuicRstStreamErrorCode error
,
98 QuicStreamOffset bytes_written
);
100 // Called when the session wants to go away and not accept any new streams.
101 void SendGoAway(QuicErrorCode error_code
, const std::string
& reason
);
103 // Removes the stream associated with 'stream_id' from the active stream map.
104 virtual void CloseStream(QuicStreamId stream_id
);
106 // Returns true if outgoing packets will be encrypted, even if the server
107 // hasn't confirmed the handshake yet.
108 virtual bool IsEncryptionEstablished();
110 // For a client, returns true if the server has confirmed our handshake. For
111 // a server, returns true if a full, valid client hello has been received.
112 virtual bool IsCryptoHandshakeConfirmed();
114 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated.
115 virtual void OnConfigNegotiated();
117 // Called by the QuicCryptoStream when the handshake enters a new state.
119 // Clients will call this function in the order:
120 // ENCRYPTION_FIRST_ESTABLISHED
121 // zero or more ENCRYPTION_REESTABLISHED
122 // HANDSHAKE_CONFIRMED
124 // Servers will simply call it once with HANDSHAKE_CONFIRMED.
125 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event
);
127 // Called by the QuicCryptoStream when a handshake message is sent.
128 virtual void OnCryptoHandshakeMessageSent(
129 const CryptoHandshakeMessage
& message
);
131 // Called by the QuicCryptoStream when a handshake message is received.
132 virtual void OnCryptoHandshakeMessageReceived(
133 const CryptoHandshakeMessage
& message
);
135 // Returns mutable config for this session. Returned config is owned
137 QuicConfig
* config();
139 // Returns true if the stream existed previously and has been closed.
140 // Returns false if the stream is still active or if the stream has
141 // not yet been created.
142 bool IsClosedStream(QuicStreamId id
);
144 QuicConnection
* connection() { return connection_
.get(); }
145 const QuicConnection
* connection() const { return connection_
.get(); }
146 size_t num_active_requests() const { return dynamic_stream_map_
.size(); }
147 const IPEndPoint
& peer_address() const {
148 return connection_
->peer_address();
150 QuicConnectionId
connection_id() const {
151 return connection_
->connection_id();
154 // Returns the number of currently open streams, including those which have
155 // been implicitly created, but excluding the reserved headers and crypto
157 virtual size_t GetNumOpenStreams() const;
159 // Add the stream to the session's write-blocked list because it is blocked by
160 // connection-level flow control but not by its own stream-level flow control.
161 // The stream will be given a chance to write when a connection-level
162 // WINDOW_UPDATE arrives.
163 void MarkConnectionLevelWriteBlocked(QuicStreamId id
, QuicPriority priority
);
165 // Returns true if the session has data to be sent, either queued in the
166 // connection, or in a write-blocked stream.
167 bool HasDataToWrite() const;
169 bool goaway_received() const { return goaway_received_
; }
171 bool goaway_sent() const { return goaway_sent_
; }
173 QuicErrorCode
error() const { return error_
; }
175 Perspective
perspective() const { return connection_
->perspective(); }
177 QuicFlowController
* flow_controller() { return &flow_controller_
; }
179 // Returns true if connection is flow controller blocked.
180 bool IsConnectionFlowControlBlocked() const;
182 // Returns true if any stream is flow controller blocked.
183 bool IsStreamFlowControlBlocked();
185 // Returns true if this is a secure QUIC session.
186 bool IsSecure() const { return connection()->is_secure(); }
188 size_t get_max_open_streams() const { return max_open_streams_
; }
190 ReliableQuicStream
* GetStream(const QuicStreamId stream_id
);
192 // Mark a stream as draining.
193 void StreamDraining(QuicStreamId id
);
196 typedef base::hash_map
<QuicStreamId
, ReliableQuicStream
*> StreamMap
;
198 // Creates a new stream, owned by the caller, to handle a peer-initiated
199 // stream. Returns nullptr and does error handling if the stream can not be
201 virtual ReliableQuicStream
* CreateIncomingDynamicStream(QuicStreamId id
) = 0;
203 // Create a new stream, owned by the caller, to handle a locally-initiated
204 // stream. Returns nullptr if max streams have already been opened.
205 virtual ReliableQuicStream
* CreateOutgoingDynamicStream() = 0;
207 // Return the reserved crypto stream.
208 virtual QuicCryptoStream
* GetCryptoStream() = 0;
210 // Adds 'stream' to the active stream map.
211 virtual void ActivateStream(ReliableQuicStream
* stream
);
213 // Returns the stream id for a new stream.
214 QuicStreamId
GetNextStreamId();
216 ReliableQuicStream
* GetIncomingDynamicStream(QuicStreamId stream_id
);
218 ReliableQuicStream
* GetDynamicStream(const QuicStreamId stream_id
);
220 // This is called after every call other than OnConnectionClose from the
221 // QuicConnectionVisitor to allow post-processing once the work has been done.
222 // In this case, it deletes streams given that it's safe to do so (no other
223 // operations are being done on the streams at this time)
224 virtual void PostProcessAfterData();
226 StreamMap
& static_streams() { return static_stream_map_
; }
227 const StreamMap
& static_streams() const { return static_stream_map_
; }
229 StreamMap
& dynamic_streams() { return dynamic_stream_map_
; }
230 const StreamMap
& dynamic_streams() const { return dynamic_stream_map_
; }
232 std::vector
<ReliableQuicStream
*>* closed_streams() {
233 return &closed_streams_
;
236 void set_max_open_streams(size_t max_open_streams
);
238 void set_largest_peer_created_stream_id(
239 QuicStreamId largest_peer_created_stream_id
) {
240 largest_peer_created_stream_id_
= largest_peer_created_stream_id
;
244 friend class test::QuicSessionPeer
;
245 friend class VisitorShim
;
247 // Performs the work required to close |stream_id|. If |locally_reset|
248 // then the stream has been reset by this endpoint, not by the peer.
249 void CloseStreamInner(QuicStreamId stream_id
, bool locally_reset
);
251 // When a stream is closed locally, it may not yet know how many bytes the
252 // peer sent on that stream.
253 // When this data arrives (via stream frame w. FIN, or RST) this method
254 // is called, and correctly updates the connection level flow controller.
255 void UpdateFlowControlOnFinalReceivedByteOffset(
256 QuicStreamId id
, QuicStreamOffset final_byte_offset
);
258 // Called in OnConfigNegotiated when we receive a new stream level flow
259 // control window in a negotiated config. Closes the connection if invalid.
260 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window
);
262 // Called in OnConfigNegotiated when we receive a new connection level flow
263 // control window in a negotiated config. Closes the connection if invalid.
264 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window
);
266 // Called in OnConfigNegotiated when auto-tuning is enabled for flow
267 // control receive windows.
268 void EnableAutoTuneReceiveWindow();
270 // Keep track of highest received byte offset of locally closed streams, while
271 // waiting for a definitive final highest offset from the peer.
272 std::map
<QuicStreamId
, QuicStreamOffset
>
273 locally_closed_streams_highest_offset_
;
275 scoped_ptr
<QuicConnection
> connection_
;
277 // A shim to stand between the connection and the session, to handle stream
279 scoped_ptr
<VisitorShim
> visitor_shim_
;
281 std::vector
<ReliableQuicStream
*> closed_streams_
;
285 // Returns the maximum number of streams this connection can open.
286 size_t max_open_streams_
;
288 // Static streams, such as crypto and header streams. Owned by child classes
289 // that create these streams.
290 StreamMap static_stream_map_
;
292 // Map from StreamId to pointers to streams that are owned by the caller.
293 StreamMap dynamic_stream_map_
;
294 QuicStreamId next_stream_id_
;
296 // Set of stream ids that have been "implicitly created" by receipt
297 // of a stream id larger than the next expected stream id.
298 base::hash_set
<QuicStreamId
> implicitly_created_streams_
;
300 // Set of stream ids that are "draining" -- a FIN has been sent and received,
301 // but the stream object still exists because not all the received data has
303 base::hash_set
<QuicStreamId
> draining_streams_
;
305 // A list of streams which need to write more data.
306 QuicWriteBlockedList write_blocked_streams_
;
308 QuicStreamId largest_peer_created_stream_id_
;
310 // The latched error with which the connection was closed.
311 QuicErrorCode error_
;
313 // Used for connection-level flow control.
314 QuicFlowController flow_controller_
;
316 // Whether a GoAway has been received.
317 bool goaway_received_
;
318 // Whether a GoAway has been sent.
321 // Indicate if there is pending data for the crypto stream.
322 bool has_pending_handshake_
;
324 DISALLOW_COPY_AND_ASSIGN(QuicSession
);
329 #endif // NET_QUIC_QUIC_SESSION_H_