[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 5 | #ifndef NET_SPDY_SPDY_SESSION_H_ |
| 6 | #define NET_SPDY_SPDY_SESSION_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 8 | |
| 9 | #include <deque> |
| 10 | #include <list> |
| 11 | #include <map> |
| 12 | #include <queue> |
| 13 | #include <string> |
| 14 | |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 16 | #include "base/memory/linked_ptr.h" |
| 17 | #include "base/memory/ref_counted.h" |
[email protected] | 023cee77 | 2010-09-29 20:59:40 | [diff] [blame] | 18 | #include "base/task.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 19 | #include "net/base/io_buffer.h" |
| 20 | #include "net/base/load_states.h" |
| 21 | #include "net/base/net_errors.h" |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 22 | #include "net/base/net_log.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 23 | #include "net/base/request_priority.h" |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 24 | #include "net/base/ssl_config_service.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 25 | #include "net/base/upload_data_stream.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 26 | #include "net/socket/client_socket.h" |
| 27 | #include "net/socket/client_socket_handle.h" |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 28 | #include "net/socket/tcp_client_socket_pool.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 29 | #include "net/spdy/spdy_framer.h" |
| 30 | #include "net/spdy/spdy_io_buffer.h" |
| 31 | #include "net/spdy/spdy_protocol.h" |
| 32 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 33 | |
| 34 | namespace net { |
| 35 | |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 36 | // This is somewhat arbitrary and not really fixed, but it will always work |
| 37 | // reasonably with ethernet. Chop the world into 2-packet chunks. This is |
| 38 | // somewhat arbitrary, but is reasonably small and ensures that we elicit |
| 39 | // ACKs quickly from TCP (because TCP tries to only ACK every other packet). |
| 40 | const int kMss = 1430; |
| 41 | const int kMaxSpdyFrameChunkSize = (2 * kMss) - spdy::SpdyFrame::size(); |
| 42 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 43 | class BoundNetLog; |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 44 | class SpdySettingsStorage; |
| 45 | class SpdyStream; |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 46 | class SSLInfo; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 47 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 48 | class SpdySession : public base::RefCounted<SpdySession>, |
| 49 | public spdy::SpdyFramerVisitorInterface { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 50 | public: |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 51 | // Create a new SpdySession. |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 52 | // |host_port_proxy_pair| is the host/port that this session connects to, and |
| 53 | // the proxy configuration settings that it's using. |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 54 | // |spdy_session_pool| is the SpdySessionPool that owns us. Its lifetime must |
| 55 | // strictly be greater than |this|. |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 56 | // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log |
| 57 | // network events to. |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 58 | SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 59 | SpdySessionPool* spdy_session_pool, |
| 60 | SpdySettingsStorage* spdy_settings, |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 61 | NetLog* net_log); |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 62 | |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 63 | const HostPortPair& host_port_pair() const { |
| 64 | return host_port_proxy_pair_.first; |
| 65 | } |
| 66 | const HostPortProxyPair& host_port_proxy_pair() const { |
| 67 | return host_port_proxy_pair_; |
| 68 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 69 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 70 | // Get a pushed stream for a given |url|. |
| 71 | // If the server initiates a stream, it might already exist for a given path. |
| 72 | // The server might also not have initiated the stream yet, but indicated it |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 73 | // will via X-Associated-Content. Writes the stream out to |spdy_stream|. |
| 74 | // Returns a net error code. |
| 75 | int GetPushStream( |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 76 | const GURL& url, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 77 | scoped_refptr<SpdyStream>* spdy_stream, |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 78 | const BoundNetLog& stream_net_log); |
| 79 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 80 | // Create a new stream for a given |url|. Writes it out to |spdy_stream|. |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 81 | // Returns a net error code, possibly ERR_IO_PENDING. |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 82 | int CreateStream( |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 83 | const GURL& url, |
| 84 | RequestPriority priority, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 85 | scoped_refptr<SpdyStream>* spdy_stream, |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 86 | const BoundNetLog& stream_net_log, |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 87 | CompletionCallback* callback); |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 88 | |
| 89 | // Remove PendingCreateStream objects on transaction deletion |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 90 | void CancelPendingCreateStreams(const scoped_refptr<SpdyStream>* spdy_stream); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 91 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 92 | // Used by SpdySessionPool to initialize with a pre-existing SSL socket. For |
| 93 | // testing, setting is_secure to false allows initialization with a |
| 94 | // pre-existing TCP socket. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 95 | // Returns OK on success, or an error on failure. |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 96 | net::Error InitializeWithSocket(ClientSocketHandle* connection, |
| 97 | bool is_secure, |
| 98 | int certificate_error_code); |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 99 | |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 100 | // Check to see if this SPDY session can support an additional domain. |
| 101 | // If the session is un-authenticated, then this call always returns true. |
| 102 | // For SSL-based sessions, verifies that the certificate in use by this |
| 103 | // session provides authentication for the domain. |
| 104 | // NOTE: This function can have false negatives on some platforms. |
| 105 | bool VerifyDomainAuthentication(const std::string& domain); |
| 106 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 107 | // Send the SYN frame for |stream_id|. |
| 108 | int WriteSynStream( |
| 109 | spdy::SpdyStreamId stream_id, |
| 110 | RequestPriority priority, |
| 111 | spdy::SpdyControlFlags flags, |
| 112 | const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
| 113 | |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 114 | // Write a data frame to the stream. |
| 115 | // Used to create and queue a data frame for the given stream. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 116 | int WriteStreamData(spdy::SpdyStreamId stream_id, net::IOBuffer* data, |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 117 | int len, |
| 118 | spdy::SpdyDataFlags flags); |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 119 | |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 120 | // Close a stream. |
| 121 | void CloseStream(spdy::SpdyStreamId stream_id, int status); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 122 | |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 123 | // Reset a stream by sending a RST_STREAM frame with given status code. |
| 124 | // Also closes the stream. Was not piggybacked to CloseStream since not |
| 125 | // all of the calls to CloseStream necessitate sending a RST_STREAM. |
| 126 | void ResetStream(spdy::SpdyStreamId stream_id, spdy::SpdyStatusCodes status); |
| 127 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 128 | // Check if a stream is active. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 129 | bool IsStreamActive(spdy::SpdyStreamId stream_id) const; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 130 | |
| 131 | // The LoadState is used for informing the user of the current network |
| 132 | // status, such as "resolving host", "connecting", etc. |
| 133 | LoadState GetLoadState() const; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 134 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 135 | // Fills SSL info in |ssl_info| and returns true when SSL is in use. |
| 136 | bool GetSSLInfo(SSLInfo* ssl_info, bool* was_npn_negotiated); |
| 137 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 138 | // Fills SSL Certificate Request info |cert_request_info| and returns |
| 139 | // true when SSL is in use. |
| 140 | bool GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info); |
| 141 | |
[email protected] | 34437af8 | 2009-11-06 02:28:49 | [diff] [blame] | 142 | // Enable or disable SSL. |
| 143 | static void SetSSLMode(bool enable) { use_ssl_ = enable; } |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 144 | static bool SSLMode() { return use_ssl_; } |
[email protected] | 34437af8 | 2009-11-06 02:28:49 | [diff] [blame] | 145 | |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 146 | // Enable or disable flow control. |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 147 | static void set_flow_control(bool enable) { use_flow_control_ = enable; } |
| 148 | static bool flow_control() { return use_flow_control_; } |
| 149 | |
[email protected] | f90a3a5e | 2010-10-25 05:17:24 | [diff] [blame] | 150 | // Sets the max concurrent streams per session. |
| 151 | static void set_max_concurrent_streams(size_t value) { |
| 152 | max_concurrent_stream_limit_ = value; |
| 153 | } |
| 154 | static size_t max_concurrent_streams() { |
| 155 | return max_concurrent_stream_limit_; |
| 156 | } |
| 157 | |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 158 | // Send WINDOW_UPDATE frame, called by a stream whenever receive window |
| 159 | // size is increased. |
| 160 | void SendWindowUpdate(spdy::SpdyStreamId stream_id, int delta_window_size); |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 161 | |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 162 | // If session is closed, no new streams/transactions should be created. |
| 163 | bool IsClosed() const { return state_ == CLOSED; } |
| 164 | |
| 165 | // Closes this session. This will close all active streams and mark |
| 166 | // the session as permanently closed. |
| 167 | // |err| should not be OK; this function is intended to be called on |
| 168 | // error. |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 169 | // |remove_from_pool| indicates whether to also remove the session from the |
| 170 | // session pool. |
| 171 | void CloseSessionOnError(net::Error err, bool remove_from_pool); |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 172 | |
[email protected] | 1ce7b66b | 2010-10-12 20:32:44 | [diff] [blame] | 173 | // Retrieves information on the current state of the SPDY session as a |
| 174 | // Value. Caller takes possession of the returned value. |
| 175 | Value* GetInfoAsValue() const; |
| 176 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 177 | // Indicates whether the session is being reused after having successfully |
| 178 | // used to send/receive data in the past. |
| 179 | bool IsReused() const { |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 180 | return frames_received_ > 0; |
| 181 | } |
| 182 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 183 | // Returns true if the underlying transport socket ever had any reads or |
| 184 | // writes. |
| 185 | bool WasEverUsed() const { |
| 186 | return connection_->socket()->WasEverUsed(); |
| 187 | } |
| 188 | |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 189 | void set_spdy_session_pool(SpdySessionPool* pool) { |
| 190 | spdy_session_pool_ = NULL; |
| 191 | } |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 192 | |
[email protected] | 19ec8a7 | 2010-08-23 03:38:23 | [diff] [blame] | 193 | // Access to the number of active and pending streams. These are primarily |
| 194 | // available for testing and diagnostics. |
| 195 | size_t num_active_streams() const { return active_streams_.size(); } |
| 196 | size_t num_unclaimed_pushed_streams() const { |
| 197 | return unclaimed_pushed_streams_.size(); |
| 198 | } |
| 199 | |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 200 | const BoundNetLog& net_log() const { return net_log_; } |
| 201 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 202 | int GetPeerAddress(AddressList* address) const { |
| 203 | return connection_->socket()->GetPeerAddress(address); |
| 204 | } |
| 205 | |
[email protected] | 807c01a | 2010-04-21 16:57:45 | [diff] [blame] | 206 | private: |
| 207 | friend class base::RefCounted<SpdySession>; |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 208 | FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, GetActivePushStream); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 209 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 210 | struct PendingCreateStream { |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 211 | PendingCreateStream(const GURL& url, RequestPriority priority, |
| 212 | scoped_refptr<SpdyStream>* spdy_stream, |
| 213 | const BoundNetLog& stream_net_log, |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 214 | CompletionCallback* callback) |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 215 | : url(&url), priority(priority), spdy_stream(spdy_stream), |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 216 | stream_net_log(&stream_net_log), callback(callback) { } |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 217 | |
| 218 | const GURL* url; |
| 219 | RequestPriority priority; |
| 220 | scoped_refptr<SpdyStream>* spdy_stream; |
| 221 | const BoundNetLog* stream_net_log; |
| 222 | CompletionCallback* callback; |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 223 | }; |
| 224 | typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > |
| 225 | PendingCreateStreamQueue; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 226 | typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 227 | // Only HTTP push a stream. |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 228 | typedef std::map<std::string, scoped_refptr<SpdyStream> > PushedStreamMap; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 229 | typedef std::priority_queue<SpdyIOBuffer> OutputQueue; |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 230 | |
[email protected] | 06bdc8d | 2010-11-18 17:37:14 | [diff] [blame] | 231 | struct CallbackResultPair { |
| 232 | CallbackResultPair() : callback(NULL), result(OK) {} |
| 233 | CallbackResultPair(CompletionCallback* callback_in, int result_in) |
| 234 | : callback(callback_in), result(result_in) {} |
| 235 | |
| 236 | CompletionCallback* callback; |
| 237 | int result; |
| 238 | }; |
| 239 | |
| 240 | typedef std::map<const scoped_refptr<SpdyStream>*, CallbackResultPair> |
| 241 | PendingCallbackMap; |
| 242 | |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 243 | enum State { |
| 244 | IDLE, |
| 245 | CONNECTING, |
| 246 | CONNECTED, |
| 247 | CLOSED |
| 248 | }; |
| 249 | |
| 250 | enum { kDefaultMaxConcurrentStreams = 10 }; |
| 251 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 252 | virtual ~SpdySession(); |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 253 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 254 | void ProcessPendingCreateStreams(); |
| 255 | int CreateStreamImpl( |
| 256 | const GURL& url, |
| 257 | RequestPriority priority, |
| 258 | scoped_refptr<SpdyStream>* spdy_stream, |
| 259 | const BoundNetLog& stream_net_log); |
| 260 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 261 | // Control frame handlers. |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 262 | void OnSyn(const spdy::SpdySynStreamControlFrame& frame, |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 263 | const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 264 | void OnSynReply(const spdy::SpdySynReplyControlFrame& frame, |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 265 | const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 266 | void OnHeaders(const spdy::SpdyHeadersControlFrame& frame, |
| 267 | const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 268 | void OnRst(const spdy::SpdyRstStreamControlFrame& frame); |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 269 | void OnGoAway(const spdy::SpdyGoAwayControlFrame& frame); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 270 | void OnSettings(const spdy::SpdySettingsControlFrame& frame); |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 271 | void OnWindowUpdate(const spdy::SpdyWindowUpdateControlFrame& frame); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 272 | |
| 273 | // IO Callbacks |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 274 | void OnReadComplete(int result); |
| 275 | void OnWriteComplete(int result); |
| 276 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 277 | // Send relevant SETTINGS. This is generally called on connection setup. |
| 278 | void SendSettings(); |
| 279 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 280 | // Handle SETTINGS. Either when we send settings, or when we receive a |
| 281 | // SETTINGS ontrol frame, update our SpdySession accordingly. |
| 282 | void HandleSettings(const spdy::SpdySettings& settings); |
| 283 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 284 | // Start reading from the socket. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 285 | // Returns OK on success, or an error on failure. |
| 286 | net::Error ReadSocket(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 287 | |
| 288 | // Write current data to the socket. |
| 289 | void WriteSocketLater(); |
| 290 | void WriteSocket(); |
| 291 | |
| 292 | // Get a new stream id. |
| 293 | int GetNewStreamId(); |
| 294 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 295 | // Queue a frame for sending. |
| 296 | // |frame| is the frame to send. |
| 297 | // |priority| is the priority for insertion into the queue. |
| 298 | // |stream| is the stream which this IO is associated with (or NULL). |
| 299 | void QueueFrame(spdy::SpdyFrame* frame, spdy::SpdyPriority priority, |
| 300 | SpdyStream* stream); |
| 301 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 302 | // Track active streams in the active stream list. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 303 | void ActivateStream(SpdyStream* stream); |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 304 | void DeleteStream(spdy::SpdyStreamId id, int status); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 305 | |
[email protected] | 4b4762a | 2010-04-23 16:04:14 | [diff] [blame] | 306 | // Removes this session from the session pool. |
| 307 | void RemoveFromPool(); |
| 308 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 309 | // Check if we have a pending pushed-stream for this url |
| 310 | // Returns the stream if found (and returns it from the pending |
| 311 | // list), returns NULL otherwise. |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 312 | scoped_refptr<SpdyStream> GetActivePushStream(const std::string& url); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 313 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 314 | // Calls OnResponseReceived(). |
[email protected] | 3f662f1 | 2010-03-25 19:56:12 | [diff] [blame] | 315 | // Returns true if successful. |
| 316 | bool Respond(const spdy::SpdyHeaderBlock& headers, |
| 317 | const scoped_refptr<SpdyStream> stream); |
| 318 | |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 319 | void RecordHistograms(); |
| 320 | |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 321 | // Closes all streams. Used as part of shutdown. |
| 322 | void CloseAllStreams(net::Error status); |
| 323 | |
[email protected] | 2f898c4 | 2010-10-07 23:30:57 | [diff] [blame] | 324 | // Invokes a user callback for stream creation. We provide this method so it |
| 325 | // can be deferred to the MessageLoop, so we avoid re-entrancy problems. |
[email protected] | 06bdc8d | 2010-11-18 17:37:14 | [diff] [blame] | 326 | void InvokeUserStreamCreationCallback(scoped_refptr<SpdyStream>* stream); |
[email protected] | 2f898c4 | 2010-10-07 23:30:57 | [diff] [blame] | 327 | |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 328 | // SpdyFramerVisitorInterface: |
| 329 | virtual void OnError(spdy::SpdyFramer*); |
| 330 | virtual void OnStreamFrameData(spdy::SpdyStreamId stream_id, |
| 331 | const char* data, |
| 332 | size_t len); |
| 333 | virtual void OnControl(const spdy::SpdyControlFrame* frame); |
| 334 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 335 | // Callbacks for the Spdy session. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 336 | CompletionCallbackImpl<SpdySession> read_callback_; |
| 337 | CompletionCallbackImpl<SpdySession> write_callback_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 338 | |
[email protected] | 023cee77 | 2010-09-29 20:59:40 | [diff] [blame] | 339 | // Used for posting asynchronous IO tasks. We use this even though |
| 340 | // SpdySession is refcounted because we don't need to keep the SpdySession |
| 341 | // alive if the last reference is within a RunnableMethod. Just revoke the |
| 342 | // method. |
| 343 | ScopedRunnableMethodFactory<SpdySession> method_factory_; |
| 344 | |
[email protected] | 06bdc8d | 2010-11-18 17:37:14 | [diff] [blame] | 345 | // Map of the SpdyStreams for which we have a pending Task to invoke a |
| 346 | // callback. This is necessary since, before we invoke said callback, it's |
| 347 | // possible that the request is cancelled. |
| 348 | PendingCallbackMap pending_callback_map_; |
| 349 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 350 | // The domain this session is connected to. |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 351 | const HostPortProxyPair host_port_proxy_pair_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 352 | |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 353 | // |spdy_session_pool_| owns us, therefore its lifetime must exceed ours. We |
| 354 | // set this to NULL after we are removed from the pool. |
| 355 | SpdySessionPool* spdy_session_pool_; |
[email protected] | 2f898c4 | 2010-10-07 23:30:57 | [diff] [blame] | 356 | SpdySettingsStorage* const spdy_settings_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 357 | |
| 358 | // The socket handle for this session. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 359 | scoped_ptr<ClientSocketHandle> connection_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 360 | |
| 361 | // The read buffer used to read data from the socket. |
[email protected] | 230cadd | 2009-09-22 16:33:59 | [diff] [blame] | 362 | scoped_refptr<IOBuffer> read_buffer_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 363 | bool read_pending_; |
| 364 | |
| 365 | int stream_hi_water_mark_; // The next stream id to use. |
| 366 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 367 | // Queue, for each priority, of pending Create Streams that have not |
| 368 | // yet been satisfied |
| 369 | PendingCreateStreamQueue create_stream_queues_[NUM_PRIORITIES]; |
| 370 | |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 371 | // Map from stream id to all active streams. Streams are active in the sense |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 372 | // that they have a consumer (typically SpdyNetworkTransaction and regardless |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 373 | // of whether or not there is currently any ongoing IO [might be waiting for |
| 374 | // the server to start pushing the stream]) or there are still network events |
| 375 | // incoming even though the consumer has already gone away (cancellation). |
| 376 | // TODO(willchan): Perhaps we should separate out cancelled streams and move |
| 377 | // them into a separate ActiveStreamMap, and not deliver network events to |
| 378 | // them? |
| 379 | ActiveStreamMap active_streams_; |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 380 | // Map of all the streams that have already started to be pushed by the |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 381 | // server, but do not have consumers yet. |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 382 | PushedStreamMap unclaimed_pushed_streams_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 383 | |
| 384 | // As we gather data to be sent, we put it into the output queue. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 385 | OutputQueue queue_; |
| 386 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 387 | // The packet we are currently sending. |
[email protected] | e124535 | 2009-11-30 19:42:42 | [diff] [blame] | 388 | bool write_pending_; // Will be true when a write is in progress. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 389 | SpdyIOBuffer in_flight_write_; // This is the write buffer in progress. |
[email protected] | e124535 | 2009-11-30 19:42:42 | [diff] [blame] | 390 | |
| 391 | // Flag if we have a pending message scheduled for WriteSocket. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 392 | bool delayed_write_pending_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 393 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 394 | // Flag if we're using an SSL connection for this SpdySession. |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 395 | bool is_secure_; |
| 396 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 397 | // Certificate error code when using a secure connection. |
| 398 | int certificate_error_code_; |
| 399 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 400 | // Spdy Frame state. |
| 401 | spdy::SpdyFramer spdy_framer_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 402 | |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 403 | // If an error has occurred on the session, the session is effectively |
| 404 | // dead. Record this error here. When no error has occurred, |error_| will |
| 405 | // be OK. |
| 406 | net::Error error_; |
| 407 | State state_; |
| 408 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 409 | // Limits |
| 410 | size_t max_concurrent_streams_; // 0 if no limit |
| 411 | |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 412 | // Some statistics counters for the session. |
| 413 | int streams_initiated_count_; |
| 414 | int streams_pushed_count_; |
| 415 | int streams_pushed_and_claimed_count_; |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 416 | int streams_abandoned_count_; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 417 | int frames_received_; |
[email protected] | 13b3a8e | 2010-12-10 01:53:33 | [diff] [blame] | 418 | int bytes_received_; |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 419 | bool sent_settings_; // Did this session send settings when it started. |
| 420 | bool received_settings_; // Did this session receive at least one settings |
| 421 | // frame. |
[email protected] | a6187e1 | 2010-12-06 19:31:59 | [diff] [blame] | 422 | int stalled_streams_; // Count of streams that were ever stalled. |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 423 | |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 424 | // Initial send window size for the session; can be changed by an |
| 425 | // arriving SETTINGS frame; newly created streams use this value for the |
| 426 | // initial send window size. |
| 427 | int initial_send_window_size_; |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 428 | |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 429 | // Initial receive window size for the session; there are plans to add a |
| 430 | // command line switch that would cause a SETTINGS frame with window size |
| 431 | // announcement to be sent on startup; newly created streams will use |
| 432 | // this value for the initial receive window size. |
| 433 | int initial_recv_window_size_; |
| 434 | |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 435 | BoundNetLog net_log_; |
| 436 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 437 | static bool use_ssl_; |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 438 | static bool use_flow_control_; |
[email protected] | f90a3a5e | 2010-10-25 05:17:24 | [diff] [blame] | 439 | static size_t max_concurrent_stream_limit_; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 440 | }; |
| 441 | |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 442 | class NetLogSpdySynParameter : public NetLog::EventParameters { |
| 443 | public: |
| 444 | NetLogSpdySynParameter(const linked_ptr<spdy::SpdyHeaderBlock>& headers, |
| 445 | spdy::SpdyControlFlags flags, |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 446 | spdy::SpdyStreamId id, |
| 447 | spdy::SpdyStreamId associated_stream); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 448 | |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 449 | const linked_ptr<spdy::SpdyHeaderBlock>& GetHeaders() const { |
| 450 | return headers_; |
| 451 | } |
| 452 | |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 453 | virtual Value* ToValue() const; |
| 454 | |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 455 | private: |
| 456 | virtual ~NetLogSpdySynParameter(); |
| 457 | |
| 458 | const linked_ptr<spdy::SpdyHeaderBlock> headers_; |
| 459 | const spdy::SpdyControlFlags flags_; |
| 460 | const spdy::SpdyStreamId id_; |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 461 | const spdy::SpdyStreamId associated_stream_; |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 462 | |
| 463 | DISALLOW_COPY_AND_ASSIGN(NetLogSpdySynParameter); |
| 464 | }; |
| 465 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 466 | } // namespace net |
| 467 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 468 | #endif // NET_SPDY_SPDY_SESSION_H_ |