OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SPDY_SPDY_SESSION_H_ | 5 #ifndef NET_SPDY_SPDY_SESSION_H_ |
6 #define NET_SPDY_SPDY_SESSION_H_ | 6 #define NET_SPDY_SPDY_SESSION_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <list> | 10 #include <list> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 SpdyProtocolErrorDetails_SpdyErrors_mismatch); | 87 SpdyProtocolErrorDetails_SpdyErrors_mismatch); |
88 | 88 |
89 COMPILE_ASSERT(PROTOCOL_ERROR_UNEXPECTED_PING == | 89 COMPILE_ASSERT(PROTOCOL_ERROR_UNEXPECTED_PING == |
90 static_cast<SpdyProtocolErrorDetails>(NUM_STATUS_CODES + | 90 static_cast<SpdyProtocolErrorDetails>(NUM_STATUS_CODES + |
91 STATUS_CODE_INVALID), | 91 STATUS_CODE_INVALID), |
92 SpdyProtocolErrorDetails_SpdyErrors_mismatch); | 92 SpdyProtocolErrorDetails_SpdyErrors_mismatch); |
93 | 93 |
94 class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, | 94 class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, |
95 public BufferedSpdyFramerVisitorInterface { | 95 public BufferedSpdyFramerVisitorInterface { |
96 public: | 96 public: |
| 97 // Defines an interface for producing SpdyIOBuffers. |
| 98 class NET_EXPORT_PRIVATE SpdyIOBufferProducer { |
| 99 public: |
| 100 SpdyIOBufferProducer() {} |
| 101 |
| 102 // Returns a newly created SpdyIOBuffer, owned by the caller, or NULL |
| 103 // if not buffer is ready to be produced. |
| 104 virtual SpdyIOBuffer* ProduceNextBuffer(SpdySession* session) = 0; |
| 105 |
| 106 virtual RequestPriority GetPriority() const = 0; |
| 107 |
| 108 virtual ~SpdyIOBufferProducer() {} |
| 109 |
| 110 protected: |
| 111 // Activates |spdy_stream| in |spdy_session|. |
| 112 static void ActivateStream(SpdySession* spdy_session, |
| 113 SpdyStream* spdy_stream); |
| 114 |
| 115 static SpdyIOBuffer* CreateIOBuffer(SpdyFrame* frame, |
| 116 RequestPriority priority, |
| 117 SpdyStream* spdy_stream); |
| 118 |
| 119 private: |
| 120 DISALLOW_COPY_AND_ASSIGN(SpdyIOBufferProducer); |
| 121 }; |
| 122 |
97 // Create a new SpdySession. | 123 // Create a new SpdySession. |
98 // |host_port_proxy_pair| is the host/port that this session connects to, and | 124 // |host_port_proxy_pair| is the host/port that this session connects to, and |
99 // the proxy configuration settings that it's using. | 125 // the proxy configuration settings that it's using. |
100 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must | 126 // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must |
101 // strictly be greater than |this|. | 127 // strictly be greater than |this|. |
102 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log | 128 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log |
103 // network events to. | 129 // network events to. |
104 SpdySession(const HostPortProxyPair& host_port_proxy_pair, | 130 SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
105 SpdySessionPool* spdy_session_pool, | 131 SpdySessionPool* spdy_session_pool, |
106 HttpServerProperties* http_server_properties, | 132 HttpServerProperties* http_server_properties, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // If the session is un-authenticated, then this call always returns true. | 176 // If the session is un-authenticated, then this call always returns true. |
151 // For SSL-based sessions, verifies that the server certificate in use by | 177 // For SSL-based sessions, verifies that the server certificate in use by |
152 // this session provides authentication for the domain and no client | 178 // this session provides authentication for the domain and no client |
153 // certificate was sent to the original server during the SSL handshake. | 179 // certificate was sent to the original server during the SSL handshake. |
154 // NOTE: This function can have false negatives on some platforms. | 180 // NOTE: This function can have false negatives on some platforms. |
155 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch | 181 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch |
156 // histogram because this function does more than verifying domain | 182 // histogram because this function does more than verifying domain |
157 // authentication now. | 183 // authentication now. |
158 bool VerifyDomainAuthentication(const std::string& domain); | 184 bool VerifyDomainAuthentication(const std::string& domain); |
159 | 185 |
| 186 // Records that |stream| has a write available. |
| 187 void SetStreamHasWriteAvailable(SpdyStream* stream); |
| 188 |
160 // Send the SYN frame for |stream_id|. This also sends PING message to check | 189 // Send the SYN frame for |stream_id|. This also sends PING message to check |
161 // the status of the connection. | 190 // the status of the connection. |
162 int WriteSynStream( | 191 SpdySynStreamControlFrame* CreateSynStream( |
163 SpdyStreamId stream_id, | 192 SpdyStreamId stream_id, |
164 RequestPriority priority, | 193 RequestPriority priority, |
165 uint8 credential_slot, | 194 uint8 credential_slot, |
166 SpdyControlFlags flags, | 195 SpdyControlFlags flags, |
167 const linked_ptr<SpdyHeaderBlock>& headers); | 196 const linked_ptr<SpdyHeaderBlock>& headers); |
168 | 197 |
169 // Write a CREDENTIAL frame to the session. | 198 // Write a CREDENTIAL frame to the session. |
170 int WriteCredentialFrame(const std::string& origin, | 199 SpdyCredentialControlFrame* CreateCredentialFrame(const std::string& origin, |
171 SSLClientCertType type, | 200 SSLClientCertType type, |
172 const std::string& key, | 201 const std::string& key, |
173 const std::string& cert, | 202 const std::string& cert, |
174 RequestPriority priority); | 203 RequestPriority priority); |
175 | 204 |
176 // Write a data frame to the stream. | 205 // Write a data frame to the stream. |
177 // Used to create and queue a data frame for the given stream. | 206 // Used to create and queue a data frame for the given stream. |
178 int WriteStreamData(SpdyStreamId stream_id, net::IOBuffer* data, | 207 SpdyDataFrame* CreateDataFrame(SpdyStreamId stream_id, |
179 int len, | 208 net::IOBuffer* data, int len, |
180 SpdyDataFlags flags); | 209 SpdyDataFlags flags); |
181 | 210 |
182 // Close a stream. | 211 // Close a stream. |
183 void CloseStream(SpdyStreamId stream_id, int status); | 212 void CloseStream(SpdyStreamId stream_id, int status); |
184 | 213 |
| 214 // Close a stream that has been created but is not yet active. |
| 215 void CloseCreatedStream(SpdyStream* stream, int status); |
| 216 |
185 // Reset a stream by sending a RST_STREAM frame with given status code. | 217 // Reset a stream by sending a RST_STREAM frame with given status code. |
186 // Also closes the stream. Was not piggybacked to CloseStream since not | 218 // Also closes the stream. Was not piggybacked to CloseStream since not |
187 // all of the calls to CloseStream necessitate sending a RST_STREAM. | 219 // all of the calls to CloseStream necessitate sending a RST_STREAM. |
188 void ResetStream(SpdyStreamId stream_id, | 220 void ResetStream(SpdyStreamId stream_id, |
189 SpdyStatusCodes status, | 221 SpdyStatusCodes status, |
190 const std::string& description); | 222 const std::string& description); |
191 | 223 |
192 // Check if a stream is active. | 224 // Check if a stream is active. |
193 bool IsStreamActive(SpdyStreamId stream_id) const; | 225 bool IsStreamActive(SpdyStreamId stream_id) const; |
194 | 226 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 bool WasEverUsed() const { | 297 bool WasEverUsed() const { |
266 return connection_->socket()->WasEverUsed(); | 298 return connection_->socket()->WasEverUsed(); |
267 } | 299 } |
268 | 300 |
269 void set_spdy_session_pool(SpdySessionPool* pool) { | 301 void set_spdy_session_pool(SpdySessionPool* pool) { |
270 spdy_session_pool_ = NULL; | 302 spdy_session_pool_ = NULL; |
271 } | 303 } |
272 | 304 |
273 // Returns true if session is not currently active | 305 // Returns true if session is not currently active |
274 bool is_active() const { | 306 bool is_active() const { |
275 return !active_streams_.empty(); | 307 return !active_streams_.empty() || !created_streams_.empty(); |
276 } | 308 } |
277 | 309 |
278 // Access to the number of active and pending streams. These are primarily | 310 // Access to the number of active and pending streams. These are primarily |
279 // available for testing and diagnostics. | 311 // available for testing and diagnostics. |
280 size_t num_active_streams() const { return active_streams_.size(); } | 312 size_t num_active_streams() const { return active_streams_.size(); } |
281 size_t num_unclaimed_pushed_streams() const { | 313 size_t num_unclaimed_pushed_streams() const { |
282 return unclaimed_pushed_streams_.size(); | 314 return unclaimed_pushed_streams_.size(); |
283 } | 315 } |
| 316 size_t num_created_streams() const { return created_streams_.size(); } |
284 | 317 |
285 // Returns true if flow control is enabled for the session. | 318 // Returns true if flow control is enabled for the session. |
286 bool is_flow_control_enabled() const { | 319 bool is_flow_control_enabled() const { |
287 return flow_control_; | 320 return flow_control_; |
288 } | 321 } |
289 | 322 |
290 // Returns the current |initial_send_window_size_|. | 323 // Returns the current |initial_send_window_size_|. |
291 int32 initial_send_window_size() const { | 324 int32 initial_send_window_size() const { |
292 return initial_send_window_size_; | 325 return initial_send_window_size_; |
293 } | 326 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 RequestPriority priority; | 382 RequestPriority priority; |
350 scoped_refptr<SpdyStream>* spdy_stream; | 383 scoped_refptr<SpdyStream>* spdy_stream; |
351 const BoundNetLog* stream_net_log; | 384 const BoundNetLog* stream_net_log; |
352 CompletionCallback callback; | 385 CompletionCallback callback; |
353 }; | 386 }; |
354 typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > | 387 typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > |
355 PendingCreateStreamQueue; | 388 PendingCreateStreamQueue; |
356 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; | 389 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
357 // Only HTTP push a stream. | 390 // Only HTTP push a stream. |
358 typedef std::map<std::string, scoped_refptr<SpdyStream> > PushedStreamMap; | 391 typedef std::map<std::string, scoped_refptr<SpdyStream> > PushedStreamMap; |
359 typedef std::priority_queue<SpdyIOBuffer> OutputQueue; | 392 typedef std::set<scoped_refptr<SpdyStream> > CreatedStreamSet; |
| 393 typedef std::map<SpdyIOBufferProducer*, SpdyStream*> StreamProducerMap; |
| 394 class SpdyIOBufferProducerCompare { |
| 395 public: |
| 396 bool operator() (const SpdyIOBufferProducer* lhs, |
| 397 const SpdyIOBufferProducer* rhs) const { |
| 398 return lhs->GetPriority() < rhs->GetPriority(); |
| 399 } |
| 400 }; |
| 401 typedef std::priority_queue<SpdyIOBufferProducer*, |
| 402 std::vector<SpdyIOBufferProducer*>, |
| 403 SpdyIOBufferProducerCompare> WriteQueue; |
360 | 404 |
361 struct CallbackResultPair { | 405 struct CallbackResultPair { |
362 CallbackResultPair(const CompletionCallback& callback_in, int result_in) | 406 CallbackResultPair(const CompletionCallback& callback_in, int result_in) |
363 : callback(callback_in), result(result_in) {} | 407 : callback(callback_in), result(result_in) {} |
364 ~CallbackResultPair(); | 408 ~CallbackResultPair(); |
365 | 409 |
366 CompletionCallback callback; | 410 CompletionCallback callback; |
367 int result; | 411 int result; |
368 }; | 412 }; |
369 | 413 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // Write current data to the socket. | 471 // Write current data to the socket. |
428 void WriteSocketLater(); | 472 void WriteSocketLater(); |
429 void WriteSocket(); | 473 void WriteSocket(); |
430 | 474 |
431 // Get a new stream id. | 475 // Get a new stream id. |
432 int GetNewStreamId(); | 476 int GetNewStreamId(); |
433 | 477 |
434 // Queue a frame for sending. | 478 // Queue a frame for sending. |
435 // |frame| is the frame to send. | 479 // |frame| is the frame to send. |
436 // |priority| is the priority for insertion into the queue. | 480 // |priority| is the priority for insertion into the queue. |
437 // |stream| is the stream which this IO is associated with (or NULL). | 481 void QueueFrame(SpdyFrame* frame, RequestPriority priority); |
438 void QueueFrame(SpdyFrame* frame, RequestPriority priority, | |
439 SpdyStream* stream); | |
440 | 482 |
441 // Track active streams in the active stream list. | 483 // Track active streams in the active stream list. |
442 void ActivateStream(SpdyStream* stream); | 484 void ActivateStream(SpdyStream* stream); |
443 void DeleteStream(SpdyStreamId id, int status); | 485 void DeleteStream(SpdyStreamId id, int status); |
444 | 486 |
445 // Removes this session from the session pool. | 487 // Removes this session from the session pool. |
446 void RemoveFromPool(); | 488 void RemoveFromPool(); |
447 | 489 |
448 // Check if we have a pending pushed-stream for this url | 490 // Check if we have a pending pushed-stream for this url |
449 // Returns the stream if found (and returns it from the pending | 491 // Returns the stream if found (and returns it from the pending |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 // the server to start pushing the stream]) or there are still network events | 601 // the server to start pushing the stream]) or there are still network events |
560 // incoming even though the consumer has already gone away (cancellation). | 602 // incoming even though the consumer has already gone away (cancellation). |
561 // TODO(willchan): Perhaps we should separate out cancelled streams and move | 603 // TODO(willchan): Perhaps we should separate out cancelled streams and move |
562 // them into a separate ActiveStreamMap, and not deliver network events to | 604 // them into a separate ActiveStreamMap, and not deliver network events to |
563 // them? | 605 // them? |
564 ActiveStreamMap active_streams_; | 606 ActiveStreamMap active_streams_; |
565 // Map of all the streams that have already started to be pushed by the | 607 // Map of all the streams that have already started to be pushed by the |
566 // server, but do not have consumers yet. | 608 // server, but do not have consumers yet. |
567 PushedStreamMap unclaimed_pushed_streams_; | 609 PushedStreamMap unclaimed_pushed_streams_; |
568 | 610 |
569 // As we gather data to be sent, we put it into the output queue. | 611 // Set of all created streams but that have not yet sent any frames. |
570 OutputQueue queue_; | 612 CreatedStreamSet created_streams_; |
| 613 |
| 614 // As streams have data to be sent, we put them into the write queue. |
| 615 WriteQueue write_queue_; |
| 616 |
| 617 // Mapping from SpdyIOBufferProducers to their corresponding SpdyStream |
| 618 // so that when a stream is destroyed, we can remove the corresponding |
| 619 // producer from |write_queue_| |
| 620 StreamProducerMap stream_producers_; |
571 | 621 |
572 // The packet we are currently sending. | 622 // The packet we are currently sending. |
573 bool write_pending_; // Will be true when a write is in progress. | 623 bool write_pending_; // Will be true when a write is in progress. |
574 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. | 624 SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. |
575 | 625 |
576 // Flag if we have a pending message scheduled for WriteSocket. | 626 // Flag if we have a pending message scheduled for WriteSocket. |
577 bool delayed_write_pending_; | 627 bool delayed_write_pending_; |
578 | 628 |
579 // Flag if we're using an SSL connection for this SpdySession. | 629 // Flag if we're using an SSL connection for this SpdySession. |
580 bool is_secure_; | 630 bool is_secure_; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 base::TimeDelta hung_interval_; | 716 base::TimeDelta hung_interval_; |
667 | 717 |
668 // This SPDY proxy is allowed to push resources from origins that are | 718 // This SPDY proxy is allowed to push resources from origins that are |
669 // different from those of their associated streams. | 719 // different from those of their associated streams. |
670 HostPortPair trusted_spdy_proxy_; | 720 HostPortPair trusted_spdy_proxy_; |
671 }; | 721 }; |
672 | 722 |
673 } // namespace net | 723 } // namespace net |
674 | 724 |
675 #endif // NET_SPDY_SPDY_SESSION_H_ | 725 #endif // NET_SPDY_SPDY_SESSION_H_ |
OLD | NEW |