| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
| 25 #include "net/socket/tcp_client_socket_pool.h" | 25 #include "net/socket/tcp_client_socket_pool.h" |
| 26 #include "net/spdy/spdy_framer.h" | 26 #include "net/spdy/spdy_framer.h" |
| 27 #include "net/spdy/spdy_io_buffer.h" | 27 #include "net/spdy/spdy_io_buffer.h" |
| 28 #include "net/spdy/spdy_protocol.h" | 28 #include "net/spdy/spdy_protocol.h" |
| 29 #include "net/spdy/spdy_session_pool.h" | 29 #include "net/spdy/spdy_session_pool.h" |
| 30 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 30 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 31 | 31 |
| 32 namespace net { | 32 namespace net { |
| 33 | 33 |
| 34 class SpdyHttpStream; |
| 34 class SpdyStream; | 35 class SpdyStream; |
| 35 class HttpNetworkSession; | 36 class HttpNetworkSession; |
| 36 class BoundNetLog; | 37 class BoundNetLog; |
| 37 class SSLInfo; | 38 class SSLInfo; |
| 38 | 39 |
| 39 class SpdySession : public base::RefCounted<SpdySession>, | 40 class SpdySession : public base::RefCounted<SpdySession>, |
| 40 public spdy::SpdyFramerVisitorInterface { | 41 public spdy::SpdyFramerVisitorInterface { |
| 41 public: | 42 public: |
| 42 // Create a new SpdySession. | 43 // Create a new SpdySession. |
| 43 // |host_port_pair| is the host/port that this session connects to. | 44 // |host_port_pair| is the host/port that this session connects to. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 60 // If the server initiates a stream, it might already exist for a given path. | 61 // If the server initiates a stream, it might already exist for a given path. |
| 61 // The server might also not have initiated the stream yet, but indicated it | 62 // The server might also not have initiated the stream yet, but indicated it |
| 62 // will via X-Associated-Content. Writes the stream out to |spdy_stream|. | 63 // will via X-Associated-Content. Writes the stream out to |spdy_stream|. |
| 63 // Returns a net error code. | 64 // Returns a net error code. |
| 64 int GetPushStream( | 65 int GetPushStream( |
| 65 const GURL& url, | 66 const GURL& url, |
| 66 scoped_refptr<SpdyStream>* spdy_stream, | 67 scoped_refptr<SpdyStream>* spdy_stream, |
| 67 const BoundNetLog& stream_net_log); | 68 const BoundNetLog& stream_net_log); |
| 68 | 69 |
| 69 // Create a new stream for a given |url|. Writes it out to |spdy_stream|. | 70 // Create a new stream for a given |url|. Writes it out to |spdy_stream|. |
| 70 // Returns a net error code. | 71 // Returns a net error code, possibly ERR_IO_PENDING. |
| 71 int CreateStream( | 72 int CreateStream( |
| 72 const GURL& url, | 73 const GURL& url, |
| 73 RequestPriority priority, | 74 RequestPriority priority, |
| 74 scoped_refptr<SpdyStream>* spdy_stream, | 75 scoped_refptr<SpdyStream>* spdy_stream, |
| 75 const BoundNetLog& stream_net_log); | 76 const BoundNetLog& stream_net_log, |
| 77 CompletionCallback* callback, |
| 78 const SpdyHttpStream* spdy_http_stream); |
| 79 |
| 80 // Remove PendingCreateStream objects on transaction deletion |
| 81 void CancelPendingCreateStreams(const SpdyHttpStream* trans); |
| 76 | 82 |
| 77 // Used by SpdySessionPool to initialize with a pre-existing SSL socket. | 83 // Used by SpdySessionPool to initialize with a pre-existing SSL socket. |
| 78 // Returns OK on success, or an error on failure. | 84 // Returns OK on success, or an error on failure. |
| 79 net::Error InitializeWithSSLSocket(ClientSocketHandle* connection, | 85 net::Error InitializeWithSSLSocket(ClientSocketHandle* connection, |
| 80 int certificate_error_code); | 86 int certificate_error_code); |
| 81 | 87 |
| 82 // Send the SYN frame for |stream_id|. | 88 // Send the SYN frame for |stream_id|. |
| 83 int WriteSynStream( | 89 int WriteSynStream( |
| 84 spdy::SpdyStreamId stream_id, | 90 spdy::SpdyStreamId stream_id, |
| 85 RequestPriority priority, | 91 RequestPriority priority, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 friend class base::RefCounted<SpdySession>; | 127 friend class base::RefCounted<SpdySession>; |
| 122 FRIEND_TEST(SpdySessionTest, GetActivePushStream); | 128 FRIEND_TEST(SpdySessionTest, GetActivePushStream); |
| 123 | 129 |
| 124 enum State { | 130 enum State { |
| 125 IDLE, | 131 IDLE, |
| 126 CONNECTING, | 132 CONNECTING, |
| 127 CONNECTED, | 133 CONNECTED, |
| 128 CLOSED | 134 CLOSED |
| 129 }; | 135 }; |
| 130 | 136 |
| 137 enum { kDefaultMaxConcurrentStreams = 100 }; // TODO(mbelshe) remove this |
| 138 |
| 139 struct PendingCreateStream { |
| 140 const GURL* url; |
| 141 RequestPriority priority; |
| 142 scoped_refptr<SpdyStream>* spdy_stream; |
| 143 const BoundNetLog* stream_net_log; |
| 144 CompletionCallback* callback; |
| 145 |
| 146 const SpdyHttpStream* spdy_http_stream; |
| 147 |
| 148 PendingCreateStream(const GURL& url, RequestPriority priority, |
| 149 scoped_refptr<SpdyStream>* spdy_stream, |
| 150 const BoundNetLog& stream_net_log, |
| 151 CompletionCallback* callback, |
| 152 const SpdyHttpStream* spdy_http_stream) |
| 153 : url(&url), priority(priority), spdy_stream(spdy_stream), |
| 154 stream_net_log(&stream_net_log), callback(callback), |
| 155 spdy_http_stream(spdy_http_stream) { } |
| 156 }; |
| 157 typedef std::queue<PendingCreateStream, std::list< PendingCreateStream> > |
| 158 PendingCreateStreamQueue; |
| 131 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; | 159 typedef std::map<int, scoped_refptr<SpdyStream> > ActiveStreamMap; |
| 132 // Only HTTP push a stream. | 160 // Only HTTP push a stream. |
| 133 typedef std::list<scoped_refptr<SpdyStream> > ActivePushedStreamList; | 161 typedef std::list<scoped_refptr<SpdyStream> > ActivePushedStreamList; |
| 134 typedef std::map<std::string, scoped_refptr<SpdyStream> > PendingStreamMap; | 162 typedef std::map<std::string, scoped_refptr<SpdyStream> > PendingStreamMap; |
| 135 typedef std::priority_queue<SpdyIOBuffer> OutputQueue; | 163 typedef std::priority_queue<SpdyIOBuffer> OutputQueue; |
| 136 | 164 |
| 137 virtual ~SpdySession(); | 165 virtual ~SpdySession(); |
| 138 | 166 |
| 167 void ProcessPendingCreateStreams(); |
| 168 int CreateStreamImpl( |
| 169 const GURL& url, |
| 170 RequestPriority priority, |
| 171 scoped_refptr<SpdyStream>* spdy_stream, |
| 172 const BoundNetLog& stream_net_log); |
| 173 |
| 139 // SpdyFramerVisitorInterface | 174 // SpdyFramerVisitorInterface |
| 140 virtual void OnError(spdy::SpdyFramer*); | 175 virtual void OnError(spdy::SpdyFramer*); |
| 141 virtual void OnStreamFrameData(spdy::SpdyStreamId stream_id, | 176 virtual void OnStreamFrameData(spdy::SpdyStreamId stream_id, |
| 142 const char* data, | 177 const char* data, |
| 143 size_t len); | 178 size_t len); |
| 144 virtual void OnControl(const spdy::SpdyControlFrame* frame); | 179 virtual void OnControl(const spdy::SpdyControlFrame* frame); |
| 145 | 180 |
| 146 // Control frame handlers. | 181 // Control frame handlers. |
| 147 void OnSyn(const spdy::SpdySynStreamControlFrame& frame, | 182 void OnSyn(const spdy::SpdySynStreamControlFrame& frame, |
| 148 const linked_ptr<spdy::SpdyHeaderBlock>& headers); | 183 const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
| 149 void OnSynReply(const spdy::SpdySynReplyControlFrame& frame, | 184 void OnSynReply(const spdy::SpdySynReplyControlFrame& frame, |
| 150 const linked_ptr<spdy::SpdyHeaderBlock>& headers); | 185 const linked_ptr<spdy::SpdyHeaderBlock>& headers); |
| 151 void OnFin(const spdy::SpdyRstStreamControlFrame& frame); | 186 void OnFin(const spdy::SpdyRstStreamControlFrame& frame); |
| 152 void OnGoAway(const spdy::SpdyGoAwayControlFrame& frame); | 187 void OnGoAway(const spdy::SpdyGoAwayControlFrame& frame); |
| 153 void OnSettings(const spdy::SpdySettingsControlFrame& frame); | 188 void OnSettings(const spdy::SpdySettingsControlFrame& frame); |
| 154 | 189 |
| 155 // IO Callbacks | 190 // IO Callbacks |
| 156 void OnTCPConnect(int result); | 191 void OnTCPConnect(int result); |
| 157 void OnSSLConnect(int result); | 192 void OnSSLConnect(int result); |
| 158 void OnReadComplete(int result); | 193 void OnReadComplete(int result); |
| 159 void OnWriteComplete(int result); | 194 void OnWriteComplete(int result); |
| 160 | 195 |
| 161 // Send relevant SETTINGS. This is generally called on connection setup. | 196 // Send relevant SETTINGS. This is generally called on connection setup. |
| 162 void SendSettings(); | 197 void SendSettings(); |
| 163 | 198 |
| 199 // Handle SETTINGS. Either when we send settings, or when we receive a |
| 200 // SETTINGS ontrol frame, update our SpdySession accordingly. |
| 201 void HandleSettings(const spdy::SpdySettings& settings); |
| 202 |
| 164 // Start reading from the socket. | 203 // Start reading from the socket. |
| 165 // Returns OK on success, or an error on failure. | 204 // Returns OK on success, or an error on failure. |
| 166 net::Error ReadSocket(); | 205 net::Error ReadSocket(); |
| 167 | 206 |
| 168 // Write current data to the socket. | 207 // Write current data to the socket. |
| 169 void WriteSocketLater(); | 208 void WriteSocketLater(); |
| 170 void WriteSocket(); | 209 void WriteSocket(); |
| 171 | 210 |
| 172 // Get a new stream id. | 211 // Get a new stream id. |
| 173 int GetNewStreamId(); | 212 int GetNewStreamId(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 255 |
| 217 // The socket handle for this session. | 256 // The socket handle for this session. |
| 218 scoped_ptr<ClientSocketHandle> connection_; | 257 scoped_ptr<ClientSocketHandle> connection_; |
| 219 | 258 |
| 220 // The read buffer used to read data from the socket. | 259 // The read buffer used to read data from the socket. |
| 221 scoped_refptr<IOBuffer> read_buffer_; | 260 scoped_refptr<IOBuffer> read_buffer_; |
| 222 bool read_pending_; | 261 bool read_pending_; |
| 223 | 262 |
| 224 int stream_hi_water_mark_; // The next stream id to use. | 263 int stream_hi_water_mark_; // The next stream id to use. |
| 225 | 264 |
| 265 // Queue, for each priority, of pending Create Streams that have not |
| 266 // yet been satisfied |
| 267 PendingCreateStreamQueue create_stream_queues_[NUM_PRIORITIES]; |
| 268 |
| 226 // TODO(mbelshe): We need to track these stream lists better. | 269 // TODO(mbelshe): We need to track these stream lists better. |
| 227 // I suspect it is possible to remove a stream from | 270 // I suspect it is possible to remove a stream from |
| 228 // one list, but not the other. | 271 // one list, but not the other. |
| 229 | 272 |
| 230 // Map from stream id to all active streams. Streams are active in the sense | 273 // Map from stream id to all active streams. Streams are active in the sense |
| 231 // that they have a consumer (typically SpdyNetworkTransaction and regardless | 274 // that they have a consumer (typically SpdyNetworkTransaction and regardless |
| 232 // of whether or not there is currently any ongoing IO [might be waiting for | 275 // of whether or not there is currently any ongoing IO [might be waiting for |
| 233 // the server to start pushing the stream]) or there are still network events | 276 // the server to start pushing the stream]) or there are still network events |
| 234 // incoming even though the consumer has already gone away (cancellation). | 277 // incoming even though the consumer has already gone away (cancellation). |
| 235 // TODO(willchan): Perhaps we should separate out cancelled streams and move | 278 // TODO(willchan): Perhaps we should separate out cancelled streams and move |
| (...skipping 26 matching lines...) Expand all Loading... |
| 262 | 305 |
| 263 // Spdy Frame state. | 306 // Spdy Frame state. |
| 264 spdy::SpdyFramer spdy_framer_; | 307 spdy::SpdyFramer spdy_framer_; |
| 265 | 308 |
| 266 // If an error has occurred on the session, the session is effectively | 309 // If an error has occurred on the session, the session is effectively |
| 267 // dead. Record this error here. When no error has occurred, |error_| will | 310 // dead. Record this error here. When no error has occurred, |error_| will |
| 268 // be OK. | 311 // be OK. |
| 269 net::Error error_; | 312 net::Error error_; |
| 270 State state_; | 313 State state_; |
| 271 | 314 |
| 315 // Limits |
| 316 size_t max_concurrent_streams_; // 0 if no limit |
| 317 |
| 272 // Some statistics counters for the session. | 318 // Some statistics counters for the session. |
| 273 int streams_initiated_count_; | 319 int streams_initiated_count_; |
| 274 int streams_pushed_count_; | 320 int streams_pushed_count_; |
| 275 int streams_pushed_and_claimed_count_; | 321 int streams_pushed_and_claimed_count_; |
| 276 int streams_abandoned_count_; | 322 int streams_abandoned_count_; |
| 277 bool sent_settings_; // Did this session send settings when it started. | 323 bool sent_settings_; // Did this session send settings when it started. |
| 278 bool received_settings_; // Did this session receive at least one settings | 324 bool received_settings_; // Did this session receive at least one settings |
| 279 // frame. | 325 // frame. |
| 280 | 326 |
| 281 bool in_session_pool_; // True if the session is currently in the pool. | 327 bool in_session_pool_; // True if the session is currently in the pool. |
| 282 | 328 |
| 283 BoundNetLog net_log_; | 329 BoundNetLog net_log_; |
| 284 | 330 |
| 285 static bool use_ssl_; | 331 static bool use_ssl_; |
| 286 }; | 332 }; |
| 287 | 333 |
| 288 } // namespace net | 334 } // namespace net |
| 289 | 335 |
| 290 #endif // NET_SPDY_SPDY_SESSION_H_ | 336 #endif // NET_SPDY_SPDY_SESSION_H_ |
| OLD | NEW |