| 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 #include "net/quic/quic_session.h" | 5 #include "net/quic/quic_session.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "net/quic/quic_connection.h" | 8 #include "net/quic/quic_connection.h" |
| 9 | 9 |
| 10 using base::StringPiece; | 10 using base::StringPiece; |
| 11 using base::hash_map; | 11 using base::hash_map; |
| 12 using base::hash_set; | 12 using base::hash_set; |
| 13 using std::vector; | 13 using std::vector; |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 18 |
| 17 // We want to make sure we delete any closed streams in a safe manner. | 19 // We want to make sure we delete any closed streams in a safe manner. |
| 18 // To avoid deleting a stream in mid-operation, we have a simple shim between | 20 // To avoid deleting a stream in mid-operation, we have a simple shim between |
| 19 // us and the stream, so we can delete any streams when we return from | 21 // us and the stream, so we can delete any streams when we return from |
| 20 // processing. | 22 // processing. |
| 21 // | 23 // |
| 22 // We could just override the base methods, but this makes it easier to make | 24 // We could just override the base methods, but this makes it easier to make |
| 23 // sure we don't miss any. | 25 // sure we don't miss any. |
| 24 class VisitorShim : public QuicConnectionVisitorInterface { | 26 class VisitorShim : public QuicConnectionVisitorInterface { |
| 25 public: | 27 public: |
| 26 explicit VisitorShim(QuicSession* session) : session_(session) {} | 28 explicit VisitorShim(QuicSession* session) : session_(session) {} |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 QuicSession::QuicSession(QuicConnection* connection, | 69 QuicSession::QuicSession(QuicConnection* connection, |
| 68 const QuicConfig& config, | 70 const QuicConfig& config, |
| 69 bool is_server) | 71 bool is_server) |
| 70 : connection_(connection), | 72 : connection_(connection), |
| 71 visitor_shim_(new VisitorShim(this)), | 73 visitor_shim_(new VisitorShim(this)), |
| 72 config_(config), | 74 config_(config), |
| 73 max_open_streams_(kDefaultMaxStreamsPerConnection), | 75 max_open_streams_(kDefaultMaxStreamsPerConnection), |
| 74 next_stream_id_(is_server ? 2 : 3), | 76 next_stream_id_(is_server ? 2 : 3), |
| 75 is_server_(is_server), | 77 is_server_(is_server), |
| 76 largest_peer_created_stream_id_(0), | 78 largest_peer_created_stream_id_(0), |
| 79 error_(QUIC_NO_ERROR), |
| 77 goaway_received_(false), | 80 goaway_received_(false), |
| 78 goaway_sent_(false) { | 81 goaway_sent_(false) { |
| 79 set_max_open_streams(config_.max_streams_per_connection()); | 82 set_max_open_streams(config_.max_streams_per_connection()); |
| 80 | 83 |
| 81 connection_->set_visitor(visitor_shim_.get()); | 84 connection_->set_visitor(visitor_shim_.get()); |
| 82 connection_->SetIdleNetworkTimeout(config_.idle_connection_state_lifetime()); | 85 connection_->SetIdleNetworkTimeout(config_.idle_connection_state_lifetime()); |
| 83 connection_->SetOverallConnectionTimeout( | 86 connection_->SetOverallConnectionTimeout( |
| 84 config_.max_time_before_crypto_handshake()); | 87 config_.max_time_before_crypto_handshake()); |
| 85 // TODO(satyamshekhar): Set congestion control and ICSL also. | 88 // TODO(satyamshekhar): Set congestion control and ICSL also. |
| 86 } | 89 } |
| 87 | 90 |
| 88 QuicSession::~QuicSession() { | 91 QuicSession::~QuicSession() { |
| 89 STLDeleteElements(&closed_streams_); | 92 STLDeleteElements(&closed_streams_); |
| 90 STLDeleteValues(&stream_map_); | 93 STLDeleteValues(&stream_map_); |
| 91 } | 94 } |
| 92 | 95 |
| 93 bool QuicSession::OnPacket(const IPEndPoint& self_address, | 96 bool QuicSession::OnPacket(const IPEndPoint& self_address, |
| 94 const IPEndPoint& peer_address, | 97 const IPEndPoint& peer_address, |
| 95 const QuicPacketHeader& header, | 98 const QuicPacketHeader& header, |
| 96 const vector<QuicStreamFrame>& frames) { | 99 const vector<QuicStreamFrame>& frames) { |
| 97 if (header.public_header.guid != connection()->guid()) { | 100 if (header.public_header.guid != connection()->guid()) { |
| 98 DLOG(INFO) << "Got packet header for invalid GUID: " | 101 DLOG(INFO) << ENDPOINT << "Got packet header for invalid GUID: " |
| 99 << header.public_header.guid; | 102 << header.public_header.guid; |
| 100 return false; | 103 return false; |
| 101 } | 104 } |
| 102 for (size_t i = 0; i < frames.size(); ++i) { | 105 for (size_t i = 0; i < frames.size(); ++i) { |
| 103 // TODO(rch) deal with the error case of stream id 0 | 106 // TODO(rch) deal with the error case of stream id 0 |
| 104 if (IsClosedStream(frames[i].stream_id)) continue; | 107 if (IsClosedStream(frames[i].stream_id)) continue; |
| 105 | 108 |
| 106 ReliableQuicStream* stream = GetStream(frames[i].stream_id); | 109 ReliableQuicStream* stream = GetStream(frames[i].stream_id); |
| 107 if (stream == NULL) return false; | 110 if (stream == NULL) return false; |
| 108 if (!stream->WillAcceptStreamFrame(frames[i])) return false; | 111 if (!stream->WillAcceptStreamFrame(frames[i])) return false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 145 } |
| 143 stream->OnStreamReset(frame.error_code); | 146 stream->OnStreamReset(frame.error_code); |
| 144 } | 147 } |
| 145 | 148 |
| 146 void QuicSession::OnGoAway(const QuicGoAwayFrame& frame) { | 149 void QuicSession::OnGoAway(const QuicGoAwayFrame& frame) { |
| 147 DCHECK(frame.last_good_stream_id < next_stream_id_); | 150 DCHECK(frame.last_good_stream_id < next_stream_id_); |
| 148 goaway_received_ = true; | 151 goaway_received_ = true; |
| 149 } | 152 } |
| 150 | 153 |
| 151 void QuicSession::ConnectionClose(QuicErrorCode error, bool from_peer) { | 154 void QuicSession::ConnectionClose(QuicErrorCode error, bool from_peer) { |
| 155 if (error_ == QUIC_NO_ERROR) { |
| 156 error_ = error; |
| 157 } |
| 158 |
| 152 while (stream_map_.size() != 0) { | 159 while (stream_map_.size() != 0) { |
| 153 ReliableStreamMap::iterator it = stream_map_.begin(); | 160 ReliableStreamMap::iterator it = stream_map_.begin(); |
| 154 QuicStreamId id = it->first; | 161 QuicStreamId id = it->first; |
| 155 it->second->ConnectionClose(error, from_peer); | 162 it->second->ConnectionClose(error, from_peer); |
| 156 // The stream should call CloseStream as part of ConnectionClose. | 163 // The stream should call CloseStream as part of ConnectionClose. |
| 157 if (stream_map_.find(id) != stream_map_.end()) { | 164 if (stream_map_.find(id) != stream_map_.end()) { |
| 158 LOG(DFATAL) << "Stream failed to close under ConnectionClose"; | 165 LOG(DFATAL) << ENDPOINT << "Stream failed to close under ConnectionClose"; |
| 159 CloseStream(id); | 166 CloseStream(id); |
| 160 } | 167 } |
| 161 } | 168 } |
| 162 } | 169 } |
| 163 | 170 |
| 164 bool QuicSession::OnCanWrite() { | 171 bool QuicSession::OnCanWrite() { |
| 165 // We latch this here rather than doing a traditional loop, because streams | 172 // We latch this here rather than doing a traditional loop, because streams |
| 166 // may be modifying the list as we loop. | 173 // may be modifying the list as we loop. |
| 167 int remaining_writes = write_blocked_streams_.NumObjects(); | 174 int remaining_writes = write_blocked_streams_.NumObjects(); |
| 168 | 175 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 194 connection_->SendRstStream(id, error); | 201 connection_->SendRstStream(id, error); |
| 195 CloseStream(id); | 202 CloseStream(id); |
| 196 } | 203 } |
| 197 | 204 |
| 198 void QuicSession::SendGoAway(QuicErrorCode error_code, const string& reason) { | 205 void QuicSession::SendGoAway(QuicErrorCode error_code, const string& reason) { |
| 199 goaway_sent_ = true; | 206 goaway_sent_ = true; |
| 200 connection_->SendGoAway(error_code, largest_peer_created_stream_id_, reason); | 207 connection_->SendGoAway(error_code, largest_peer_created_stream_id_, reason); |
| 201 } | 208 } |
| 202 | 209 |
| 203 void QuicSession::CloseStream(QuicStreamId stream_id) { | 210 void QuicSession::CloseStream(QuicStreamId stream_id) { |
| 204 DLOG(INFO) << "Closing stream " << stream_id; | 211 DLOG(INFO) << ENDPOINT << "Closing stream " << stream_id; |
| 205 | 212 |
| 206 ReliableStreamMap::iterator it = stream_map_.find(stream_id); | 213 ReliableStreamMap::iterator it = stream_map_.find(stream_id); |
| 207 if (it == stream_map_.end()) { | 214 if (it == stream_map_.end()) { |
| 208 DLOG(INFO) << "Stream is already closed: " << stream_id; | 215 DLOG(INFO) << ENDPOINT << "Stream is already closed: " << stream_id; |
| 209 return; | 216 return; |
| 210 } | 217 } |
| 211 ReliableQuicStream* stream = it->second; | 218 ReliableQuicStream* stream = it->second; |
| 212 closed_streams_.push_back(it->second); | 219 closed_streams_.push_back(it->second); |
| 213 stream_map_.erase(it); | 220 stream_map_.erase(it); |
| 214 stream->OnClose(); | 221 stream->OnClose(); |
| 215 } | 222 } |
| 216 | 223 |
| 217 bool QuicSession::IsEncryptionEstablished() { | 224 bool QuicSession::IsEncryptionEstablished() { |
| 218 return GetCryptoStream()->encryption_established(); | 225 return GetCryptoStream()->encryption_established(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 230 break; | 237 break; |
| 231 | 238 |
| 232 case ENCRYPTION_REESTABLISHED: | 239 case ENCRYPTION_REESTABLISHED: |
| 233 // Retransmit originally packets that were sent, since they can't be | 240 // Retransmit originally packets that were sent, since they can't be |
| 234 // decrypted by the peer. | 241 // decrypted by the peer. |
| 235 connection_->RetransmitUnackedPackets( | 242 connection_->RetransmitUnackedPackets( |
| 236 QuicConnection::INITIAL_ENCRYPTION_ONLY); | 243 QuicConnection::INITIAL_ENCRYPTION_ONLY); |
| 237 break; | 244 break; |
| 238 | 245 |
| 239 case HANDSHAKE_CONFIRMED: | 246 case HANDSHAKE_CONFIRMED: |
| 240 LOG_IF(DFATAL, !config_.negotiated()) | 247 LOG_IF(DFATAL, !config_.negotiated()) << ENDPOINT |
| 241 << "Handshake confirmed without parameter negotiation."; | 248 << "Handshake confirmed without parameter negotiation."; |
| 242 connection_->SetIdleNetworkTimeout( | 249 connection_->SetIdleNetworkTimeout( |
| 243 config_.idle_connection_state_lifetime()); | 250 config_.idle_connection_state_lifetime()); |
| 244 connection_->SetOverallConnectionTimeout(QuicTime::Delta::Infinite()); | 251 connection_->SetOverallConnectionTimeout(QuicTime::Delta::Infinite()); |
| 245 max_open_streams_ = config_.max_streams_per_connection(); | 252 max_open_streams_ = config_.max_streams_per_connection(); |
| 246 break; | 253 break; |
| 247 | 254 |
| 248 default: | 255 default: |
| 249 LOG(ERROR) << "Got unknown handshake event: " << event; | 256 LOG(ERROR) << ENDPOINT << "Got unknown handshake event: " << event; |
| 250 } | 257 } |
| 251 } | 258 } |
| 252 | 259 |
| 253 QuicConfig* QuicSession::config() { | 260 QuicConfig* QuicSession::config() { |
| 254 return &config_; | 261 return &config_; |
| 255 } | 262 } |
| 256 | 263 |
| 257 void QuicSession::ActivateStream(ReliableQuicStream* stream) { | 264 void QuicSession::ActivateStream(ReliableQuicStream* stream) { |
| 258 DLOG(INFO) << "num_streams: " << stream_map_.size() | 265 DLOG(INFO) << ENDPOINT << "num_streams: " << stream_map_.size() |
| 259 << ". activating " << stream->id(); | 266 << ". activating " << stream->id(); |
| 260 DCHECK(stream_map_.count(stream->id()) == 0); | 267 DCHECK(stream_map_.count(stream->id()) == 0); |
| 261 stream_map_[stream->id()] = stream; | 268 stream_map_[stream->id()] = stream; |
| 262 } | 269 } |
| 263 | 270 |
| 264 QuicStreamId QuicSession::GetNextStreamId() { | 271 QuicStreamId QuicSession::GetNextStreamId() { |
| 265 QuicStreamId id = next_stream_id_; | 272 QuicStreamId id = next_stream_id_; |
| 266 next_stream_id_ += 2; | 273 next_stream_id_ += 2; |
| 267 return id; | 274 return id; |
| 268 } | 275 } |
| 269 | 276 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 QuicStreamId stream_id) { | 366 QuicStreamId stream_id) { |
| 360 decompression_blocked_streams_[header_id] = stream_id; | 367 decompression_blocked_streams_[header_id] = stream_id; |
| 361 } | 368 } |
| 362 | 369 |
| 363 void QuicSession::PostProcessAfterData() { | 370 void QuicSession::PostProcessAfterData() { |
| 364 STLDeleteElements(&closed_streams_); | 371 STLDeleteElements(&closed_streams_); |
| 365 closed_streams_.clear(); | 372 closed_streams_.clear(); |
| 366 } | 373 } |
| 367 | 374 |
| 368 } // namespace net | 375 } // namespace net |
| OLD | NEW |