OLD | NEW |
| (Empty) |
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. | |
6 | |
7 #ifndef NET_QUIC_QUIC_SESSION_H_ | |
8 #define NET_QUIC_QUIC_SESSION_H_ | |
9 | |
10 #include <map> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/compiler_specific.h" | |
15 #include "base/containers/hash_tables.h" | |
16 #include "net/base/ip_endpoint.h" | |
17 #include "net/quic/quic_connection.h" | |
18 #include "net/quic/quic_crypto_stream.h" | |
19 #include "net/quic/quic_data_stream.h" | |
20 #include "net/quic/quic_headers_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" | |
25 | |
26 namespace net { | |
27 | |
28 class QuicCryptoStream; | |
29 class QuicFlowController; | |
30 class ReliableQuicStream; | |
31 class SSLInfo; | |
32 class VisitorShim; | |
33 | |
34 namespace test { | |
35 class QuicSessionPeer; | |
36 } // namespace test | |
37 | |
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, | |
55 }; | |
56 | |
57 QuicSession(QuicConnection* connection, const QuicConfig& config); | |
58 void InitializeSession(); | |
59 | |
60 ~QuicSession() override; | |
61 | |
62 // QuicConnectionVisitorInterface methods: | |
63 void OnStreamFrames(const std::vector<QuicStreamFrame>& frames) override; | |
64 void OnRstStream(const QuicRstStreamFrame& frame) override; | |
65 void OnGoAway(const QuicGoAwayFrame& frame) override; | |
66 void OnWindowUpdateFrames( | |
67 const std::vector<QuicWindowUpdateFrame>& frames) override; | |
68 void OnBlockedFrames(const std::vector<QuicBlockedFrame>& frames) override; | |
69 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override; | |
70 void OnWriteBlocked() override {} | |
71 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override; | |
72 void OnCanWrite() override; | |
73 void OnCongestionWindowChange(QuicTime now) override {} | |
74 bool WillingAndAbleToWrite() const override; | |
75 bool HasPendingHandshake() const override; | |
76 bool HasOpenDataStreams() const override; | |
77 | |
78 // Called by the headers stream when headers have been received for a stream. | |
79 virtual void OnStreamHeaders(QuicStreamId stream_id, | |
80 base::StringPiece headers_data); | |
81 // Called by the headers stream when headers with a priority have been | |
82 // received for this stream. This method will only be called for server | |
83 // streams. | |
84 virtual void OnStreamHeadersPriority(QuicStreamId stream_id, | |
85 QuicPriority priority); | |
86 // Called by the headers stream when headers have been completely received | |
87 // for a stream. |fin| will be true if the fin flag was set in the headers | |
88 // frame. | |
89 virtual void OnStreamHeadersComplete(QuicStreamId stream_id, | |
90 bool fin, | |
91 size_t frame_len); | |
92 | |
93 // Called by streams when they want to write data to the peer. | |
94 // Returns a pair with the number of bytes consumed from data, and a boolean | |
95 // indicating if the fin bit was consumed. This does not indicate the data | |
96 // has been sent on the wire: it may have been turned into a packet and queued | |
97 // if the socket was unexpectedly blocked. |fec_protection| indicates if | |
98 // data is to be FEC protected. Note that data that is sent immediately | |
99 // following MUST_FEC_PROTECT data may get protected by falling within the | |
100 // same FEC group. | |
101 // If provided, |ack_notifier_delegate| will be registered to be notified when | |
102 // we have seen ACKs for all packets resulting from this call. | |
103 virtual QuicConsumedData WritevData( | |
104 QuicStreamId id, | |
105 const IOVector& data, | |
106 QuicStreamOffset offset, | |
107 bool fin, | |
108 FecProtection fec_protection, | |
109 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); | |
110 | |
111 // Writes |headers| for the stream |id| to the dedicated headers stream. | |
112 // If |fin| is true, then no more data will be sent for the stream |id|. | |
113 // If provided, |ack_notifier_delegate| will be registered to be notified when | |
114 // we have seen ACKs for all packets resulting from this call. | |
115 size_t WriteHeaders( | |
116 QuicStreamId id, | |
117 const SpdyHeaderBlock& headers, | |
118 bool fin, | |
119 QuicPriority priority, | |
120 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); | |
121 | |
122 // Called by streams when they want to close the stream in both directions. | |
123 virtual void SendRstStream(QuicStreamId id, | |
124 QuicRstStreamErrorCode error, | |
125 QuicStreamOffset bytes_written); | |
126 | |
127 // Called when the session wants to go away and not accept any new streams. | |
128 void SendGoAway(QuicErrorCode error_code, const std::string& reason); | |
129 | |
130 // Removes the stream associated with 'stream_id' from the active stream map. | |
131 virtual void CloseStream(QuicStreamId stream_id); | |
132 | |
133 // Returns true if outgoing packets will be encrypted, even if the server | |
134 // hasn't confirmed the handshake yet. | |
135 virtual bool IsEncryptionEstablished(); | |
136 | |
137 // For a client, returns true if the server has confirmed our handshake. For | |
138 // a server, returns true if a full, valid client hello has been received. | |
139 virtual bool IsCryptoHandshakeConfirmed(); | |
140 | |
141 // Called by the QuicCryptoStream when a new QuicConfig has been negotiated. | |
142 virtual void OnConfigNegotiated(); | |
143 | |
144 // Called by the QuicCryptoStream when the handshake enters a new state. | |
145 // | |
146 // Clients will call this function in the order: | |
147 // ENCRYPTION_FIRST_ESTABLISHED | |
148 // zero or more ENCRYPTION_REESTABLISHED | |
149 // HANDSHAKE_CONFIRMED | |
150 // | |
151 // Servers will simply call it once with HANDSHAKE_CONFIRMED. | |
152 virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event); | |
153 | |
154 // Called by the QuicCryptoStream when a handshake message is sent. | |
155 virtual void OnCryptoHandshakeMessageSent( | |
156 const CryptoHandshakeMessage& message); | |
157 | |
158 // Called by the QuicCryptoStream when a handshake message is received. | |
159 virtual void OnCryptoHandshakeMessageReceived( | |
160 const CryptoHandshakeMessage& message); | |
161 | |
162 // Returns mutable config for this session. Returned config is owned | |
163 // by QuicSession. | |
164 QuicConfig* config(); | |
165 | |
166 // Returns true if the stream existed previously and has been closed. | |
167 // Returns false if the stream is still active or if the stream has | |
168 // not yet been created. | |
169 bool IsClosedStream(QuicStreamId id); | |
170 | |
171 QuicConnection* connection() { return connection_.get(); } | |
172 const QuicConnection* connection() const { return connection_.get(); } | |
173 size_t num_active_requests() const { return stream_map_.size(); } | |
174 const IPEndPoint& peer_address() const { | |
175 return connection_->peer_address(); | |
176 } | |
177 QuicConnectionId connection_id() const { | |
178 return connection_->connection_id(); | |
179 } | |
180 | |
181 // Returns the number of currently open streams, including those which have | |
182 // been implicitly created, but excluding the reserved headers and crypto | |
183 // streams. | |
184 virtual size_t GetNumOpenStreams() const; | |
185 | |
186 void MarkWriteBlocked(QuicStreamId id, QuicPriority priority); | |
187 | |
188 // Returns true if the session has data to be sent, either queued in the | |
189 // connection, or in a write-blocked stream. | |
190 bool HasDataToWrite() const; | |
191 | |
192 bool goaway_received() const { | |
193 return goaway_received_; | |
194 } | |
195 | |
196 bool goaway_sent() const { | |
197 return goaway_sent_; | |
198 } | |
199 | |
200 // Gets the SSL connection information. | |
201 virtual bool GetSSLInfo(SSLInfo* ssl_info) const; | |
202 | |
203 QuicErrorCode error() const { return error_; } | |
204 | |
205 bool is_server() const { return connection_->is_server(); } | |
206 | |
207 QuicFlowController* flow_controller() { return flow_controller_.get(); } | |
208 | |
209 // Returns true if connection is flow controller blocked. | |
210 bool IsConnectionFlowControlBlocked() const; | |
211 | |
212 // Returns true if any stream is flow controller blocked. | |
213 bool IsStreamFlowControlBlocked(); | |
214 | |
215 // Returns true if this is a secure QUIC session. | |
216 bool IsSecure() const { | |
217 return connection()->is_secure(); | |
218 } | |
219 | |
220 size_t get_max_open_streams() const { return max_open_streams_; } | |
221 | |
222 protected: | |
223 typedef base::hash_map<QuicStreamId, QuicDataStream*> DataStreamMap; | |
224 | |
225 // Creates a new stream, owned by the caller, to handle a peer-initiated | |
226 // stream. Returns nullptr and does error handling if the stream can not be | |
227 // created. | |
228 virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) = 0; | |
229 | |
230 // Create a new stream, owned by the caller, to handle a locally-initiated | |
231 // stream. Returns nullptr if max streams have already been opened. | |
232 virtual QuicDataStream* CreateOutgoingDataStream() = 0; | |
233 | |
234 // Return the reserved crypto stream. | |
235 virtual QuicCryptoStream* GetCryptoStream() = 0; | |
236 | |
237 // Adds 'stream' to the active stream map. | |
238 virtual void ActivateStream(QuicDataStream* stream); | |
239 | |
240 // Returns the stream id for a new stream. | |
241 QuicStreamId GetNextStreamId(); | |
242 | |
243 QuicDataStream* GetIncomingDataStream(QuicStreamId stream_id); | |
244 | |
245 QuicDataStream* GetDataStream(const QuicStreamId stream_id); | |
246 | |
247 ReliableQuicStream* GetStream(const QuicStreamId stream_id); | |
248 | |
249 // This is called after every call other than OnConnectionClose from the | |
250 // QuicConnectionVisitor to allow post-processing once the work has been done. | |
251 // In this case, it deletes streams given that it's safe to do so (no other | |
252 // operations are being done on the streams at this time) | |
253 virtual void PostProcessAfterData(); | |
254 | |
255 base::hash_map<QuicStreamId, QuicDataStream*>* streams() { | |
256 return &stream_map_; | |
257 } | |
258 | |
259 const base::hash_map<QuicStreamId, QuicDataStream*>* streams() const { | |
260 return &stream_map_; | |
261 } | |
262 | |
263 std::vector<QuicDataStream*>* closed_streams() { return &closed_streams_; } | |
264 | |
265 void set_max_open_streams(size_t max_open_streams); | |
266 | |
267 scoped_ptr<QuicHeadersStream> headers_stream_; | |
268 | |
269 private: | |
270 friend class test::QuicSessionPeer; | |
271 friend class VisitorShim; | |
272 | |
273 // Performs the work required to close |stream_id|. If |locally_reset| | |
274 // then the stream has been reset by this endpoint, not by the peer. | |
275 void CloseStreamInner(QuicStreamId stream_id, bool locally_reset); | |
276 | |
277 // When a stream is closed locally, it may not yet know how many bytes the | |
278 // peer sent on that stream. | |
279 // When this data arrives (via stream frame w. FIN, or RST) this method | |
280 // is called, and correctly updates the connection level flow controller. | |
281 void UpdateFlowControlOnFinalReceivedByteOffset( | |
282 QuicStreamId id, QuicStreamOffset final_byte_offset); | |
283 | |
284 // Called in OnConfigNegotiated when we receive a new stream level flow | |
285 // control window in a negotiated config. Closes the connection if invalid. | |
286 void OnNewStreamFlowControlWindow(QuicStreamOffset new_window); | |
287 | |
288 // Called in OnConfigNegotiated when we receive a new session level flow | |
289 // control window in a negotiated config. Closes the connection if invalid. | |
290 void OnNewSessionFlowControlWindow(QuicStreamOffset new_window); | |
291 | |
292 // Keep track of highest received byte offset of locally closed streams, while | |
293 // waiting for a definitive final highest offset from the peer. | |
294 std::map<QuicStreamId, QuicStreamOffset> | |
295 locally_closed_streams_highest_offset_; | |
296 | |
297 scoped_ptr<QuicConnection> connection_; | |
298 | |
299 // A shim to stand between the connection and the session, to handle stream | |
300 // deletions. | |
301 scoped_ptr<VisitorShim> visitor_shim_; | |
302 | |
303 std::vector<QuicDataStream*> closed_streams_; | |
304 | |
305 QuicConfig config_; | |
306 | |
307 // Returns the maximum number of streams this connection can open. | |
308 size_t max_open_streams_; | |
309 | |
310 // Map from StreamId to pointers to streams that are owned by the caller. | |
311 DataStreamMap stream_map_; | |
312 QuicStreamId next_stream_id_; | |
313 | |
314 // Set of stream ids that have been "implicitly created" by receipt | |
315 // of a stream id larger than the next expected stream id. | |
316 base::hash_set<QuicStreamId> implicitly_created_streams_; | |
317 | |
318 // A list of streams which need to write more data. | |
319 QuicWriteBlockedList write_blocked_streams_; | |
320 | |
321 QuicStreamId largest_peer_created_stream_id_; | |
322 | |
323 // The latched error with which the connection was closed. | |
324 QuicErrorCode error_; | |
325 | |
326 // Used for session level flow control. | |
327 scoped_ptr<QuicFlowController> flow_controller_; | |
328 | |
329 // Whether a GoAway has been received. | |
330 bool goaway_received_; | |
331 // Whether a GoAway has been sent. | |
332 bool goaway_sent_; | |
333 | |
334 // Indicate if there is pending data for the crypto stream. | |
335 bool has_pending_handshake_; | |
336 | |
337 DISALLOW_COPY_AND_ASSIGN(QuicSession); | |
338 }; | |
339 | |
340 } // namespace net | |
341 | |
342 #endif // NET_QUIC_QUIC_SESSION_H_ | |
OLD | NEW |