Landing Recent QUIC changes until 06/07/2015.
[chromium-blink-merge.git] / net / quic / quic_session.cc
blobfc0365bf1eb8a35cd859deb79fe7185ffecc41ac
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.
5 #include "net/quic/quic_session.h"
7 #include "base/stl_util.h"
8 #include "net/quic/crypto/proof_verifier.h"
9 #include "net/quic/quic_connection.h"
10 #include "net/quic/quic_flow_controller.h"
11 #include "net/quic/quic_headers_stream.h"
12 #include "net/ssl/ssl_info.h"
14 using base::StringPiece;
15 using base::hash_map;
16 using base::hash_set;
17 using std::make_pair;
18 using std::map;
19 using std::max;
20 using std::string;
21 using std::vector;
23 namespace net {
25 #define ENDPOINT \
26 (perspective() == Perspective::IS_SERVER ? "Server: " : " Client: ")
28 // We want to make sure we delete any closed streams in a safe manner.
29 // To avoid deleting a stream in mid-operation, we have a simple shim between
30 // us and the stream, so we can delete any streams when we return from
31 // processing.
33 // We could just override the base methods, but this makes it easier to make
34 // sure we don't miss any.
35 class VisitorShim : public QuicConnectionVisitorInterface {
36 public:
37 explicit VisitorShim(QuicSession* session) : session_(session) {}
39 void OnStreamFrames(const vector<QuicStreamFrame>& frames) override {
40 session_->OnStreamFrames(frames);
41 session_->PostProcessAfterData();
43 void OnRstStream(const QuicRstStreamFrame& frame) override {
44 session_->OnRstStream(frame);
45 session_->PostProcessAfterData();
48 void OnGoAway(const QuicGoAwayFrame& frame) override {
49 session_->OnGoAway(frame);
50 session_->PostProcessAfterData();
53 void OnWindowUpdateFrames(
54 const vector<QuicWindowUpdateFrame>& frames) override {
55 session_->OnWindowUpdateFrames(frames);
56 session_->PostProcessAfterData();
59 void OnBlockedFrames(const vector<QuicBlockedFrame>& frames) override {
60 session_->OnBlockedFrames(frames);
61 session_->PostProcessAfterData();
64 void OnCanWrite() override {
65 session_->OnCanWrite();
66 session_->PostProcessAfterData();
69 void OnCongestionWindowChange(QuicTime now) override {
70 session_->OnCongestionWindowChange(now);
73 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override {
74 session_->OnSuccessfulVersionNegotiation(version);
77 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override {
78 session_->OnConnectionClosed(error, from_peer);
79 // The session will go away, so don't bother with cleanup.
82 void OnWriteBlocked() override { session_->OnWriteBlocked(); }
84 bool WillingAndAbleToWrite() const override {
85 return session_->WillingAndAbleToWrite();
88 bool HasPendingHandshake() const override {
89 return session_->HasPendingHandshake();
92 bool HasOpenDataStreams() const override {
93 return session_->HasOpenDataStreams();
96 private:
97 QuicSession* session_;
100 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config)
101 : connection_(connection),
102 visitor_shim_(new VisitorShim(this)),
103 config_(config),
104 max_open_streams_(config_.MaxStreamsPerConnection()),
105 next_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 5),
106 largest_peer_created_stream_id_(0),
107 error_(QUIC_NO_ERROR),
108 flow_controller_(connection_.get(),
110 perspective(),
111 kMinimumFlowControlSendWindow,
112 config_.GetInitialSessionFlowControlWindowToSend(),
113 false),
114 goaway_received_(false),
115 goaway_sent_(false),
116 has_pending_handshake_(false) {
119 void QuicSession::Initialize() {
120 // Crypto stream must exist when Initialize is called.
121 DCHECK(GetCryptoStream());
123 connection_->set_visitor(visitor_shim_.get());
124 connection_->SetFromConfig(config_);
125 headers_stream_.reset(new QuicHeadersStream(this));
128 QuicSession::~QuicSession() {
129 STLDeleteElements(&closed_streams_);
130 STLDeleteValues(&stream_map_);
132 DLOG_IF(WARNING,
133 locally_closed_streams_highest_offset_.size() > max_open_streams_)
134 << "Surprisingly high number of locally closed streams still waiting for "
135 "final byte offset: " << locally_closed_streams_highest_offset_.size();
138 void QuicSession::OnStreamFrames(const vector<QuicStreamFrame>& frames) {
139 for (size_t i = 0; i < frames.size() && connection_->connected(); ++i) {
140 // TODO(rch) deal with the error case of stream id 0.
141 const QuicStreamFrame& frame = frames[i];
142 QuicStreamId stream_id = frame.stream_id;
143 ReliableQuicStream* stream = GetStream(stream_id);
144 if (!stream) {
145 // The stream no longer exists, but we may still be interested in the
146 // final stream byte offset sent by the peer. A frame with a FIN can give
147 // us this offset.
148 if (frame.fin) {
149 QuicStreamOffset final_byte_offset = frame.offset + frame.data.size();
150 UpdateFlowControlOnFinalReceivedByteOffset(stream_id,
151 final_byte_offset);
154 continue;
156 stream->OnStreamFrame(frames[i]);
160 void QuicSession::OnStreamHeaders(QuicStreamId stream_id,
161 StringPiece headers_data) {
162 QuicDataStream* stream = GetDataStream(stream_id);
163 if (!stream) {
164 // It's quite possible to receive headers after a stream has been reset.
165 return;
167 stream->OnStreamHeaders(headers_data);
170 void QuicSession::OnStreamHeadersPriority(QuicStreamId stream_id,
171 QuicPriority priority) {
172 QuicDataStream* stream = GetDataStream(stream_id);
173 if (!stream) {
174 // It's quite possible to receive headers after a stream has been reset.
175 return;
177 stream->OnStreamHeadersPriority(priority);
180 void QuicSession::OnStreamHeadersComplete(QuicStreamId stream_id,
181 bool fin,
182 size_t frame_len) {
183 QuicDataStream* stream = GetDataStream(stream_id);
184 if (!stream) {
185 // It's quite possible to receive headers after a stream has been reset.
186 return;
188 stream->OnStreamHeadersComplete(fin, frame_len);
191 void QuicSession::OnRstStream(const QuicRstStreamFrame& frame) {
192 if (frame.stream_id == kCryptoStreamId) {
193 connection()->SendConnectionCloseWithDetails(
194 QUIC_INVALID_STREAM_ID,
195 "Attempt to reset the crypto stream");
196 return;
198 if (frame.stream_id == kHeadersStreamId) {
199 connection()->SendConnectionCloseWithDetails(
200 QUIC_INVALID_STREAM_ID,
201 "Attempt to reset the headers stream");
202 return;
205 QuicDataStream* stream = GetDataStream(frame.stream_id);
206 if (!stream) {
207 // The RST frame contains the final byte offset for the stream: we can now
208 // update the connection level flow controller if needed.
209 UpdateFlowControlOnFinalReceivedByteOffset(frame.stream_id,
210 frame.byte_offset);
211 return; // Errors are handled by GetStream.
214 stream->OnStreamReset(frame);
217 void QuicSession::OnGoAway(const QuicGoAwayFrame& frame) {
218 DCHECK(frame.last_good_stream_id < next_stream_id_);
219 goaway_received_ = true;
222 void QuicSession::OnConnectionClosed(QuicErrorCode error, bool from_peer) {
223 DCHECK(!connection_->connected());
224 if (error_ == QUIC_NO_ERROR) {
225 error_ = error;
228 while (!stream_map_.empty()) {
229 DataStreamMap::iterator it = stream_map_.begin();
230 QuicStreamId id = it->first;
231 it->second->OnConnectionClosed(error, from_peer);
232 // The stream should call CloseStream as part of OnConnectionClosed.
233 if (stream_map_.find(id) != stream_map_.end()) {
234 LOG(DFATAL) << ENDPOINT
235 << "Stream failed to close under OnConnectionClosed";
236 CloseStream(id);
241 void QuicSession::OnSuccessfulVersionNegotiation(const QuicVersion& version) {
244 void QuicSession::OnWindowUpdateFrames(
245 const vector<QuicWindowUpdateFrame>& frames) {
246 bool connection_window_updated = false;
247 for (size_t i = 0; i < frames.size(); ++i) {
248 // Stream may be closed by the time we receive a WINDOW_UPDATE, so we can't
249 // assume that it still exists.
250 QuicStreamId stream_id = frames[i].stream_id;
251 if (stream_id == kConnectionLevelId) {
252 // This is a window update that applies to the connection, rather than an
253 // individual stream.
254 DVLOG(1) << ENDPOINT
255 << "Received connection level flow control window update with "
256 "byte offset: " << frames[i].byte_offset;
257 if (flow_controller_.UpdateSendWindowOffset(frames[i].byte_offset)) {
258 connection_window_updated = true;
260 continue;
263 ReliableQuicStream* stream = GetStream(stream_id);
264 if (stream) {
265 stream->OnWindowUpdateFrame(frames[i]);
269 // Connection level flow control window has increased, so blocked streams can
270 // write again.
271 if (connection_window_updated) {
272 OnCanWrite();
276 void QuicSession::OnBlockedFrames(const vector<QuicBlockedFrame>& frames) {
277 for (size_t i = 0; i < frames.size(); ++i) {
278 // TODO(rjshade): Compare our flow control receive windows for specified
279 // streams: if we have a large window then maybe something
280 // had gone wrong with the flow control accounting.
281 DVLOG(1) << ENDPOINT << "Received BLOCKED frame with stream id: "
282 << frames[i].stream_id;
286 void QuicSession::OnCanWrite() {
287 // We limit the number of writes to the number of pending streams. If more
288 // streams become pending, WillingAndAbleToWrite will be true, which will
289 // cause the connection to request resumption before yielding to other
290 // connections.
291 size_t num_writes = write_blocked_streams_.NumBlockedStreams();
292 if (flow_controller_.IsBlocked()) {
293 // If we are connection level flow control blocked, then only allow the
294 // crypto and headers streams to try writing as all other streams will be
295 // blocked.
296 num_writes = 0;
297 if (write_blocked_streams_.crypto_stream_blocked()) {
298 num_writes += 1;
300 if (write_blocked_streams_.headers_stream_blocked()) {
301 num_writes += 1;
304 if (num_writes == 0) {
305 return;
308 QuicConnection::ScopedPacketBundler ack_bundler(
309 connection_.get(), QuicConnection::NO_ACK);
310 for (size_t i = 0; i < num_writes; ++i) {
311 if (!(write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
312 write_blocked_streams_.HasWriteBlockedDataStreams())) {
313 // Writing one stream removed another!? Something's broken.
314 LOG(DFATAL) << "WriteBlockedStream is missing";
315 connection_->CloseConnection(QUIC_INTERNAL_ERROR, false);
316 return;
318 if (!connection_->CanWriteStreamData()) {
319 return;
321 QuicStreamId stream_id = write_blocked_streams_.PopFront();
322 if (stream_id == kCryptoStreamId) {
323 has_pending_handshake_ = false; // We just popped it.
325 ReliableQuicStream* stream = GetStream(stream_id);
326 if (stream != nullptr && !stream->flow_controller()->IsBlocked()) {
327 // If the stream can't write all bytes, it'll re-add itself to the blocked
328 // list.
329 stream->OnCanWrite();
334 bool QuicSession::WillingAndAbleToWrite() const {
335 // If the crypto or headers streams are blocked, we want to schedule a write -
336 // they don't get blocked by connection level flow control. Otherwise only
337 // schedule a write if we are not flow control blocked at the connection
338 // level.
339 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
340 (!flow_controller_.IsBlocked() &&
341 write_blocked_streams_.HasWriteBlockedDataStreams());
344 bool QuicSession::HasPendingHandshake() const {
345 return has_pending_handshake_;
348 bool QuicSession::HasOpenDataStreams() const {
349 return GetNumOpenStreams() > 0;
352 QuicConsumedData QuicSession::WritevData(
353 QuicStreamId id,
354 const IOVector& data,
355 QuicStreamOffset offset,
356 bool fin,
357 FecProtection fec_protection,
358 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
359 return connection_->SendStreamData(id, data, offset, fin, fec_protection,
360 ack_notifier_delegate);
363 size_t QuicSession::WriteHeaders(
364 QuicStreamId id,
365 const SpdyHeaderBlock& headers,
366 bool fin,
367 QuicPriority priority,
368 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
369 return headers_stream_->WriteHeaders(id, headers, fin, priority,
370 ack_notifier_delegate);
373 void QuicSession::SendRstStream(QuicStreamId id,
374 QuicRstStreamErrorCode error,
375 QuicStreamOffset bytes_written) {
376 if (connection()->connected()) {
377 // Only send a RST_STREAM frame if still connected.
378 connection_->SendRstStream(id, error, bytes_written);
380 CloseStreamInner(id, true);
383 void QuicSession::SendGoAway(QuicErrorCode error_code, const string& reason) {
384 if (goaway_sent_) {
385 return;
387 goaway_sent_ = true;
388 connection_->SendGoAway(error_code, largest_peer_created_stream_id_, reason);
391 void QuicSession::CloseStream(QuicStreamId stream_id) {
392 CloseStreamInner(stream_id, false);
395 void QuicSession::CloseStreamInner(QuicStreamId stream_id,
396 bool locally_reset) {
397 DVLOG(1) << ENDPOINT << "Closing stream " << stream_id;
399 DataStreamMap::iterator it = stream_map_.find(stream_id);
400 if (it == stream_map_.end()) {
401 DVLOG(1) << ENDPOINT << "Stream is already closed: " << stream_id;
402 return;
404 QuicDataStream* stream = it->second;
406 // Tell the stream that a RST has been sent.
407 if (locally_reset) {
408 stream->set_rst_sent(true);
411 closed_streams_.push_back(it->second);
413 // If we haven't received a FIN or RST for this stream, we need to keep track
414 // of the how many bytes the stream's flow controller believes it has
415 // received, for accurate connection level flow control accounting.
416 if (!stream->HasFinalReceivedByteOffset()) {
417 locally_closed_streams_highest_offset_[stream_id] =
418 stream->flow_controller()->highest_received_byte_offset();
421 stream_map_.erase(it);
422 stream->OnClose();
423 // Decrease the number of streams being emulated when a new one is opened.
424 connection_->SetNumOpenStreams(stream_map_.size());
427 void QuicSession::UpdateFlowControlOnFinalReceivedByteOffset(
428 QuicStreamId stream_id, QuicStreamOffset final_byte_offset) {
429 map<QuicStreamId, QuicStreamOffset>::iterator it =
430 locally_closed_streams_highest_offset_.find(stream_id);
431 if (it == locally_closed_streams_highest_offset_.end()) {
432 return;
435 DVLOG(1) << ENDPOINT << "Received final byte offset " << final_byte_offset
436 << " for stream " << stream_id;
437 QuicByteCount offset_diff = final_byte_offset - it->second;
438 if (flow_controller_.UpdateHighestReceivedOffset(
439 flow_controller_.highest_received_byte_offset() + offset_diff)) {
440 // If the final offset violates flow control, close the connection now.
441 if (flow_controller_.FlowControlViolation()) {
442 connection_->SendConnectionClose(
443 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA);
444 return;
448 flow_controller_.AddBytesConsumed(offset_diff);
449 locally_closed_streams_highest_offset_.erase(it);
452 bool QuicSession::IsEncryptionEstablished() {
453 return GetCryptoStream()->encryption_established();
456 bool QuicSession::IsCryptoHandshakeConfirmed() {
457 return GetCryptoStream()->handshake_confirmed();
460 void QuicSession::OnConfigNegotiated() {
461 connection_->SetFromConfig(config_);
463 uint32 max_streams = config_.MaxStreamsPerConnection();
464 if (perspective() == Perspective::IS_SERVER) {
465 // A server should accept a small number of additional streams beyond the
466 // limit sent to the client. This helps avoid early connection termination
467 // when FIN/RSTs for old streams are lost or arrive out of order.
468 // Use a minimum number of additional streams, or a percentage increase,
469 // whichever is larger.
470 max_streams =
471 max(max_streams + kMaxStreamsMinimumIncrement,
472 static_cast<uint32>(max_streams * kMaxStreamsMultiplier));
474 if (config_.HasReceivedConnectionOptions() &&
475 ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) {
476 EnableAutoTuneReceiveWindow();
479 set_max_open_streams(max_streams);
481 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) {
482 // Streams which were created before the SHLO was received (0-RTT
483 // requests) are now informed of the peer's initial flow control window.
484 OnNewStreamFlowControlWindow(
485 config_.ReceivedInitialStreamFlowControlWindowBytes());
487 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) {
488 OnNewSessionFlowControlWindow(
489 config_.ReceivedInitialSessionFlowControlWindowBytes());
493 void QuicSession::EnableAutoTuneReceiveWindow() {
494 flow_controller_.set_auto_tune_receive_window(true);
495 // Inform all existing streams about the new window.
496 GetCryptoStream()->flow_controller()->set_auto_tune_receive_window(true);
497 headers_stream_->flow_controller()->set_auto_tune_receive_window(true);
498 for (auto const& kv : stream_map_) {
499 kv.second->flow_controller()->set_auto_tune_receive_window(true);
503 void QuicSession::OnNewStreamFlowControlWindow(QuicStreamOffset new_window) {
504 if (new_window < kMinimumFlowControlSendWindow) {
505 LOG(ERROR) << "Peer sent us an invalid stream flow control send window: "
506 << new_window
507 << ", below default: " << kMinimumFlowControlSendWindow;
508 if (connection_->connected()) {
509 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
511 return;
514 // Inform all existing streams about the new window.
515 GetCryptoStream()->UpdateSendWindowOffset(new_window);
516 headers_stream_->UpdateSendWindowOffset(new_window);
517 for (auto const& kv : stream_map_) {
518 kv.second->UpdateSendWindowOffset(new_window);
522 void QuicSession::OnNewSessionFlowControlWindow(QuicStreamOffset new_window) {
523 if (new_window < kMinimumFlowControlSendWindow) {
524 LOG(ERROR) << "Peer sent us an invalid session flow control send window: "
525 << new_window
526 << ", below default: " << kMinimumFlowControlSendWindow;
527 if (connection_->connected()) {
528 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
530 return;
533 flow_controller_.UpdateSendWindowOffset(new_window);
536 void QuicSession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event) {
537 switch (event) {
538 // TODO(satyamshekhar): Move the logic of setting the encrypter/decrypter
539 // to QuicSession since it is the glue.
540 case ENCRYPTION_FIRST_ESTABLISHED:
541 break;
543 case ENCRYPTION_REESTABLISHED:
544 // Retransmit originally packets that were sent, since they can't be
545 // decrypted by the peer.
546 connection_->RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION);
547 break;
549 case HANDSHAKE_CONFIRMED:
550 LOG_IF(DFATAL, !config_.negotiated()) << ENDPOINT
551 << "Handshake confirmed without parameter negotiation.";
552 // Discard originally encrypted packets, since they can't be decrypted by
553 // the peer.
554 connection_->NeuterUnencryptedPackets();
555 break;
557 default:
558 LOG(ERROR) << ENDPOINT << "Got unknown handshake event: " << event;
562 void QuicSession::OnCryptoHandshakeMessageSent(
563 const CryptoHandshakeMessage& message) {
566 void QuicSession::OnCryptoHandshakeMessageReceived(
567 const CryptoHandshakeMessage& message) {
570 QuicConfig* QuicSession::config() {
571 return &config_;
574 void QuicSession::ActivateStream(QuicDataStream* stream) {
575 DVLOG(1) << ENDPOINT << "num_streams: " << stream_map_.size()
576 << ". activating " << stream->id();
577 DCHECK_EQ(stream_map_.count(stream->id()), 0u);
578 stream_map_[stream->id()] = stream;
579 // Increase the number of streams being emulated when a new one is opened.
580 connection_->SetNumOpenStreams(stream_map_.size());
583 QuicStreamId QuicSession::GetNextStreamId() {
584 QuicStreamId id = next_stream_id_;
585 next_stream_id_ += 2;
586 return id;
589 ReliableQuicStream* QuicSession::GetStream(const QuicStreamId stream_id) {
590 if (stream_id == kCryptoStreamId) {
591 return GetCryptoStream();
593 if (stream_id == kHeadersStreamId) {
594 return headers_stream_.get();
596 return GetDataStream(stream_id);
599 QuicDataStream* QuicSession::GetDataStream(const QuicStreamId stream_id) {
600 if (stream_id == kCryptoStreamId) {
601 DLOG(FATAL) << "Attempt to call GetDataStream with the crypto stream id";
602 return nullptr;
604 if (stream_id == kHeadersStreamId) {
605 DLOG(FATAL) << "Attempt to call GetDataStream with the headers stream id";
606 return nullptr;
609 DataStreamMap::iterator it = stream_map_.find(stream_id);
610 if (it != stream_map_.end()) {
611 return it->second;
614 if (IsClosedStream(stream_id)) {
615 return nullptr;
618 if (stream_id % 2 == next_stream_id_ % 2) {
619 // We've received a frame for a locally-created stream that is not
620 // currently active. This is an error.
621 if (connection()->connected()) {
622 connection()->SendConnectionClose(QUIC_PACKET_FOR_NONEXISTENT_STREAM);
624 return nullptr;
627 return GetIncomingDataStream(stream_id);
630 QuicDataStream* QuicSession::GetIncomingDataStream(QuicStreamId stream_id) {
631 if (IsClosedStream(stream_id)) {
632 return nullptr;
635 implicitly_created_streams_.erase(stream_id);
636 if (stream_id > largest_peer_created_stream_id_) {
637 if (stream_id - largest_peer_created_stream_id_ > kMaxStreamIdDelta) {
638 // We may already have sent a connection close due to multiple reset
639 // streams in the same packet.
640 if (connection()->connected()) {
641 LOG(ERROR) << "Trying to get stream: " << stream_id
642 << ", largest peer created stream: "
643 << largest_peer_created_stream_id_
644 << ", max delta: " << kMaxStreamIdDelta;
645 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
647 return nullptr;
649 if (largest_peer_created_stream_id_ == 0 &&
650 perspective() == Perspective::IS_SERVER) {
651 largest_peer_created_stream_id_ = 3;
653 for (QuicStreamId id = largest_peer_created_stream_id_ + 2;
654 id < stream_id;
655 id += 2) {
656 implicitly_created_streams_.insert(id);
658 largest_peer_created_stream_id_ = stream_id;
660 QuicDataStream* stream = CreateIncomingDataStream(stream_id);
661 if (stream == nullptr) {
662 return nullptr;
664 ActivateStream(stream);
665 return stream;
668 void QuicSession::set_max_open_streams(size_t max_open_streams) {
669 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams;
670 max_open_streams_ = max_open_streams;
673 bool QuicSession::IsClosedStream(QuicStreamId id) {
674 DCHECK_NE(0u, id);
675 if (id == kCryptoStreamId) {
676 return false;
678 if (id == kHeadersStreamId) {
679 return false;
681 if (ContainsKey(stream_map_, id)) {
682 // Stream is active
683 return false;
685 if (id % 2 == next_stream_id_ % 2) {
686 // Locally created streams are strictly in-order. If the id is in the
687 // range of created streams and it's not active, it must have been closed.
688 return id < next_stream_id_;
690 // For peer created streams, we also need to consider implicitly created
691 // streams.
692 return id <= largest_peer_created_stream_id_ &&
693 !ContainsKey(implicitly_created_streams_, id);
696 size_t QuicSession::GetNumOpenStreams() const {
697 return stream_map_.size() + implicitly_created_streams_.size();
700 void QuicSession::MarkWriteBlocked(QuicStreamId id, QuicPriority priority) {
701 #ifndef NDEBUG
702 ReliableQuicStream* stream = GetStream(id);
703 if (stream != nullptr) {
704 LOG_IF(DFATAL, priority != stream->EffectivePriority())
705 << ENDPOINT << "Stream " << id
706 << "Priorities do not match. Got: " << priority
707 << " Expected: " << stream->EffectivePriority();
708 } else {
709 LOG(DFATAL) << "Marking unknown stream " << id << " blocked.";
711 #endif
713 if (id == kCryptoStreamId) {
714 DCHECK(!has_pending_handshake_);
715 has_pending_handshake_ = true;
716 // TODO(jar): Be sure to use the highest priority for the crypto stream,
717 // perhaps by adding a "special" priority for it that is higher than
718 // kHighestPriority.
719 priority = kHighestPriority;
721 write_blocked_streams_.PushBack(id, priority);
724 bool QuicSession::HasDataToWrite() const {
725 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
726 write_blocked_streams_.HasWriteBlockedDataStreams() ||
727 connection_->HasQueuedData();
730 void QuicSession::PostProcessAfterData() {
731 STLDeleteElements(&closed_streams_);
733 if (connection()->connected() &&
734 locally_closed_streams_highest_offset_.size() > max_open_streams_) {
735 // A buggy client may fail to send FIN/RSTs. Don't tolerate this.
736 connection_->SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS);
740 bool QuicSession::IsConnectionFlowControlBlocked() const {
741 return flow_controller_.IsBlocked();
744 bool QuicSession::IsStreamFlowControlBlocked() {
745 if (headers_stream_->flow_controller()->IsBlocked() ||
746 GetCryptoStream()->flow_controller()->IsBlocked()) {
747 return true;
749 for (auto const& kv : stream_map_) {
750 if (kv.second->flow_controller()->IsBlocked()) {
751 return true;
754 return false;
757 } // namespace net