Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / quic / quic_session.cc
blob8e3696ba07d6648f3fbbc94d8e5ccb2892576c8d
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 #ifdef TEMP_INSTRUMENTATION_473893
9 #include "base/debug/alias.h"
10 #endif
11 #include "net/quic/crypto/proof_verifier.h"
12 #include "net/quic/quic_connection.h"
13 #include "net/quic/quic_flags.h"
14 #include "net/quic/quic_flow_controller.h"
15 #include "net/ssl/ssl_info.h"
17 using base::StringPiece;
18 using base::hash_map;
19 using base::hash_set;
20 using std::make_pair;
21 using std::map;
22 using std::max;
23 using std::string;
24 using std::vector;
26 namespace net {
28 #define ENDPOINT \
29 (perspective() == Perspective::IS_SERVER ? "Server: " : " Client: ")
31 // We want to make sure we delete any closed streams in a safe manner.
32 // To avoid deleting a stream in mid-operation, we have a simple shim between
33 // us and the stream, so we can delete any streams when we return from
34 // processing.
36 // We could just override the base methods, but this makes it easier to make
37 // sure we don't miss any.
38 class VisitorShim : public QuicConnectionVisitorInterface {
39 public:
40 explicit VisitorShim(QuicSession* session) : session_(session) {}
42 void OnStreamFrame(const QuicStreamFrame& frame) override {
43 session_->OnStreamFrame(frame);
44 session_->PostProcessAfterData();
46 void OnRstStream(const QuicRstStreamFrame& frame) override {
47 session_->OnRstStream(frame);
48 session_->PostProcessAfterData();
51 void OnGoAway(const QuicGoAwayFrame& frame) override {
52 session_->OnGoAway(frame);
53 session_->PostProcessAfterData();
56 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
57 session_->OnWindowUpdateFrame(frame);
58 session_->PostProcessAfterData();
61 void OnBlockedFrame(const QuicBlockedFrame& frame) override {
62 session_->OnBlockedFrame(frame);
63 session_->PostProcessAfterData();
66 void OnCanWrite() override {
67 session_->OnCanWrite();
68 session_->PostProcessAfterData();
71 void OnCongestionWindowChange(QuicTime now) override {
72 session_->OnCongestionWindowChange(now);
75 void OnSuccessfulVersionNegotiation(const QuicVersion& version) override {
76 session_->OnSuccessfulVersionNegotiation(version);
79 void OnConnectionClosed(QuicErrorCode error, bool from_peer) override {
80 session_->OnConnectionClosed(error, from_peer);
81 // The session will go away, so don't bother with cleanup.
84 void OnWriteBlocked() override { session_->OnWriteBlocked(); }
86 void OnConnectionMigration() override { session_->OnConnectionMigration(); }
88 bool WillingAndAbleToWrite() const override {
89 return session_->WillingAndAbleToWrite();
92 bool HasPendingHandshake() const override {
93 return session_->HasPendingHandshake();
96 bool HasOpenDynamicStreams() const override {
97 return session_->HasOpenDynamicStreams();
100 private:
101 QuicSession* session_;
104 QuicSession::QuicSession(QuicConnection* connection, const QuicConfig& config)
105 : connection_(connection),
106 visitor_shim_(new VisitorShim(this)),
107 config_(config),
108 max_open_streams_(config_.MaxStreamsPerConnection()),
109 next_stream_id_(perspective() == Perspective::IS_SERVER ? 2 : 3),
110 largest_peer_created_stream_id_(
111 perspective() == Perspective::IS_SERVER ? 1 : 0),
112 error_(QUIC_NO_ERROR),
113 flow_controller_(connection_.get(),
115 perspective(),
116 kMinimumFlowControlSendWindow,
117 config_.GetInitialSessionFlowControlWindowToSend(),
118 false),
119 has_pending_handshake_(false) {
122 void QuicSession::Initialize() {
123 connection_->set_visitor(visitor_shim_.get());
124 connection_->SetFromConfig(config_);
126 DCHECK_EQ(kCryptoStreamId, GetCryptoStream()->id());
127 static_stream_map_[kCryptoStreamId] = GetCryptoStream();
130 QuicSession::~QuicSession() {
131 #ifdef TEMP_INSTRUMENTATION_473893
132 liveness_ = DEAD;
133 stack_trace_ = base::debug::StackTrace();
135 // Probably not necessary, but just in case compiler tries to optimize out the
136 // writes to liveness_ and stack_trace_.
137 base::debug::Alias(&liveness_);
138 base::debug::Alias(&stack_trace_);
139 #endif
141 STLDeleteElements(&closed_streams_);
142 STLDeleteValues(&dynamic_stream_map_);
144 DLOG_IF(WARNING,
145 locally_closed_streams_highest_offset_.size() > max_open_streams_)
146 << "Surprisingly high number of locally closed streams still waiting for "
147 "final byte offset: " << locally_closed_streams_highest_offset_.size();
150 void QuicSession::OnStreamFrame(const QuicStreamFrame& frame) {
151 // TODO(rch) deal with the error case of stream id 0.
152 QuicStreamId stream_id = frame.stream_id;
153 ReliableQuicStream* stream = GetStream(stream_id);
154 if (!stream) {
155 // The stream no longer exists, but we may still be interested in the
156 // final stream byte offset sent by the peer. A frame with a FIN can give
157 // us this offset.
158 if (frame.fin) {
159 QuicStreamOffset final_byte_offset = frame.offset + frame.data.size();
160 UpdateFlowControlOnFinalReceivedByteOffset(stream_id, final_byte_offset);
162 return;
164 stream->OnStreamFrame(frame);
167 void QuicSession::OnRstStream(const QuicRstStreamFrame& frame) {
168 if (ContainsKey(static_stream_map_, frame.stream_id)) {
169 connection()->SendConnectionCloseWithDetails(
170 QUIC_INVALID_STREAM_ID, "Attempt to reset a static stream");
171 return;
174 ReliableQuicStream* stream = GetDynamicStream(frame.stream_id);
175 if (!stream) {
176 // The RST frame contains the final byte offset for the stream: we can now
177 // update the connection level flow controller if needed.
178 UpdateFlowControlOnFinalReceivedByteOffset(frame.stream_id,
179 frame.byte_offset);
180 return; // Errors are handled by GetStream.
183 stream->OnStreamReset(frame);
186 void QuicSession::OnGoAway(const QuicGoAwayFrame& frame) {
187 DCHECK(frame.last_good_stream_id < next_stream_id_);
190 void QuicSession::OnConnectionClosed(QuicErrorCode error, bool from_peer) {
191 DCHECK(!connection_->connected());
192 if (error_ == QUIC_NO_ERROR) {
193 error_ = error;
196 while (!dynamic_stream_map_.empty()) {
197 StreamMap::iterator it = dynamic_stream_map_.begin();
198 QuicStreamId id = it->first;
199 it->second->OnConnectionClosed(error, from_peer);
200 // The stream should call CloseStream as part of OnConnectionClosed.
201 if (dynamic_stream_map_.find(id) != dynamic_stream_map_.end()) {
202 LOG(DFATAL) << ENDPOINT
203 << "Stream failed to close under OnConnectionClosed";
204 CloseStream(id);
209 void QuicSession::OnSuccessfulVersionNegotiation(const QuicVersion& version) {
212 void QuicSession::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) {
213 // Stream may be closed by the time we receive a WINDOW_UPDATE, so we can't
214 // assume that it still exists.
215 QuicStreamId stream_id = frame.stream_id;
216 if (stream_id == kConnectionLevelId) {
217 // This is a window update that applies to the connection, rather than an
218 // individual stream.
219 DVLOG(1) << ENDPOINT << "Received connection level flow control window "
220 "update with byte offset: "
221 << frame.byte_offset;
222 flow_controller_.UpdateSendWindowOffset(frame.byte_offset);
223 return;
225 ReliableQuicStream* stream = GetStream(stream_id);
226 if (stream) {
227 stream->OnWindowUpdateFrame(frame);
231 void QuicSession::OnBlockedFrame(const QuicBlockedFrame& frame) {
232 // TODO(rjshade): Compare our flow control receive windows for specified
233 // streams: if we have a large window then maybe something
234 // had gone wrong with the flow control accounting.
235 DVLOG(1) << ENDPOINT
236 << "Received BLOCKED frame with stream id: " << frame.stream_id;
239 void QuicSession::OnCanWrite() {
240 // We limit the number of writes to the number of pending streams. If more
241 // streams become pending, WillingAndAbleToWrite will be true, which will
242 // cause the connection to request resumption before yielding to other
243 // connections.
244 size_t num_writes = write_blocked_streams_.NumBlockedStreams();
245 if (flow_controller_.IsBlocked()) {
246 // If we are connection level flow control blocked, then only allow the
247 // crypto and headers streams to try writing as all other streams will be
248 // blocked.
249 num_writes = 0;
250 if (write_blocked_streams_.crypto_stream_blocked()) {
251 num_writes += 1;
253 if (write_blocked_streams_.headers_stream_blocked()) {
254 num_writes += 1;
257 if (num_writes == 0) {
258 return;
261 QuicConnection::ScopedPacketBundler ack_bundler(
262 connection_.get(), QuicConnection::NO_ACK);
263 for (size_t i = 0; i < num_writes; ++i) {
264 if (!(write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
265 write_blocked_streams_.HasWriteBlockedDataStreams())) {
266 // Writing one stream removed another!? Something's broken.
267 LOG(DFATAL) << "WriteBlockedStream is missing";
268 connection_->CloseConnection(QUIC_INTERNAL_ERROR, false);
269 return;
271 if (!connection_->CanWriteStreamData()) {
272 return;
274 QuicStreamId stream_id = write_blocked_streams_.PopFront();
275 if (stream_id == kCryptoStreamId) {
276 has_pending_handshake_ = false; // We just popped it.
278 ReliableQuicStream* stream = GetStream(stream_id);
279 if (stream != nullptr && !stream->flow_controller()->IsBlocked()) {
280 // If the stream can't write all bytes, it'll re-add itself to the blocked
281 // list.
282 stream->OnCanWrite();
287 bool QuicSession::WillingAndAbleToWrite() const {
288 // If the crypto or headers streams are blocked, we want to schedule a write -
289 // they don't get blocked by connection level flow control. Otherwise only
290 // schedule a write if we are not flow control blocked at the connection
291 // level.
292 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
293 (!flow_controller_.IsBlocked() &&
294 write_blocked_streams_.HasWriteBlockedDataStreams());
297 bool QuicSession::HasPendingHandshake() const {
298 return has_pending_handshake_;
301 bool QuicSession::HasOpenDynamicStreams() const {
302 return GetNumOpenStreams() > 0;
305 QuicConsumedData QuicSession::WritevData(
306 QuicStreamId id,
307 const QuicIOVector& iov,
308 QuicStreamOffset offset,
309 bool fin,
310 FecProtection fec_protection,
311 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
312 return connection_->SendStreamData(id, iov, offset, fin, fec_protection,
313 ack_notifier_delegate);
316 void QuicSession::SendRstStream(QuicStreamId id,
317 QuicRstStreamErrorCode error,
318 QuicStreamOffset bytes_written) {
319 if (ContainsKey(static_stream_map_, id)) {
320 LOG(DFATAL) << "Cannot send RST for a static stream with ID " << id;
321 return;
324 if (connection()->connected()) {
325 // Only send a RST_STREAM frame if still connected.
326 connection_->SendRstStream(id, error, bytes_written);
328 CloseStreamInner(id, true);
331 void QuicSession::SendGoAway(QuicErrorCode error_code, const string& reason) {
332 if (goaway_sent()) {
333 return;
336 connection_->SendGoAway(error_code, largest_peer_created_stream_id_, reason);
339 void QuicSession::CloseStream(QuicStreamId stream_id) {
340 CloseStreamInner(stream_id, false);
343 void QuicSession::CloseStreamInner(QuicStreamId stream_id,
344 bool locally_reset) {
345 DVLOG(1) << ENDPOINT << "Closing stream " << stream_id;
347 StreamMap::iterator it = dynamic_stream_map_.find(stream_id);
348 if (it == dynamic_stream_map_.end()) {
349 // When CloseStreamInner has been called recursively (via
350 // ReliableQuicStream::OnClose), the stream will already have been deleted
351 // from stream_map_, so return immediately.
352 DVLOG(1) << ENDPOINT << "Stream is already closed: " << stream_id;
353 return;
355 ReliableQuicStream* stream = it->second;
357 // Tell the stream that a RST has been sent.
358 if (locally_reset) {
359 stream->set_rst_sent(true);
362 closed_streams_.push_back(it->second);
364 // If we haven't received a FIN or RST for this stream, we need to keep track
365 // of the how many bytes the stream's flow controller believes it has
366 // received, for accurate connection level flow control accounting.
367 if (!stream->HasFinalReceivedByteOffset()) {
368 locally_closed_streams_highest_offset_[stream_id] =
369 stream->flow_controller()->highest_received_byte_offset();
372 dynamic_stream_map_.erase(it);
373 draining_streams_.erase(stream_id);
374 stream->OnClose();
375 // Decrease the number of streams being emulated when a new one is opened.
376 connection_->SetNumOpenStreams(dynamic_stream_map_.size());
379 void QuicSession::UpdateFlowControlOnFinalReceivedByteOffset(
380 QuicStreamId stream_id, QuicStreamOffset final_byte_offset) {
381 map<QuicStreamId, QuicStreamOffset>::iterator it =
382 locally_closed_streams_highest_offset_.find(stream_id);
383 if (it == locally_closed_streams_highest_offset_.end()) {
384 return;
387 DVLOG(1) << ENDPOINT << "Received final byte offset " << final_byte_offset
388 << " for stream " << stream_id;
389 QuicByteCount offset_diff = final_byte_offset - it->second;
390 if (flow_controller_.UpdateHighestReceivedOffset(
391 flow_controller_.highest_received_byte_offset() + offset_diff)) {
392 // If the final offset violates flow control, close the connection now.
393 if (flow_controller_.FlowControlViolation()) {
394 connection_->SendConnectionClose(
395 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA);
396 return;
400 flow_controller_.AddBytesConsumed(offset_diff);
401 locally_closed_streams_highest_offset_.erase(it);
404 bool QuicSession::IsEncryptionEstablished() {
405 return GetCryptoStream()->encryption_established();
408 bool QuicSession::IsCryptoHandshakeConfirmed() {
409 return GetCryptoStream()->handshake_confirmed();
412 void QuicSession::OnConfigNegotiated() {
413 connection_->SetFromConfig(config_);
415 uint32 max_streams = config_.MaxStreamsPerConnection();
416 if (perspective() == Perspective::IS_SERVER) {
417 // A server should accept a small number of additional streams beyond the
418 // limit sent to the client. This helps avoid early connection termination
419 // when FIN/RSTs for old streams are lost or arrive out of order.
420 // Use a minimum number of additional streams, or a percentage increase,
421 // whichever is larger.
422 max_streams =
423 max(max_streams + kMaxStreamsMinimumIncrement,
424 static_cast<uint32>(max_streams * kMaxStreamsMultiplier));
426 if (config_.HasReceivedConnectionOptions()) {
427 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kAFCW)) {
428 EnableAutoTuneReceiveWindow();
430 // The following variations change the initial receive flow control window
431 // size for streams. For simplicity reasons, do not try to effect
432 // existing streams but only future ones.
433 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW5)) {
434 config_.SetInitialStreamFlowControlWindowToSend(32 * 1024);
436 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW6)) {
437 config_.SetInitialStreamFlowControlWindowToSend(64 * 1024);
439 if (ContainsQuicTag(config_.ReceivedConnectionOptions(), kIFW7)) {
440 config_.SetInitialStreamFlowControlWindowToSend(128 * 1024);
444 set_max_open_streams(max_streams);
446 if (config_.HasReceivedInitialStreamFlowControlWindowBytes()) {
447 // Streams which were created before the SHLO was received (0-RTT
448 // requests) are now informed of the peer's initial flow control window.
449 OnNewStreamFlowControlWindow(
450 config_.ReceivedInitialStreamFlowControlWindowBytes());
452 if (config_.HasReceivedInitialSessionFlowControlWindowBytes()) {
453 OnNewSessionFlowControlWindow(
454 config_.ReceivedInitialSessionFlowControlWindowBytes());
458 void QuicSession::EnableAutoTuneReceiveWindow() {
459 flow_controller_.set_auto_tune_receive_window(true);
460 // Inform all existing streams about the new window.
461 for (auto const& kv : static_stream_map_) {
462 kv.second->flow_controller()->set_auto_tune_receive_window(true);
464 for (auto const& kv : dynamic_stream_map_) {
465 kv.second->flow_controller()->set_auto_tune_receive_window(true);
469 void QuicSession::OnNewStreamFlowControlWindow(QuicStreamOffset new_window) {
470 if (new_window < kMinimumFlowControlSendWindow) {
471 LOG(ERROR) << "Peer sent us an invalid stream flow control send window: "
472 << new_window
473 << ", below default: " << kMinimumFlowControlSendWindow;
474 if (connection_->connected()) {
475 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
477 return;
480 // Inform all existing streams about the new window.
481 for (auto const& kv : static_stream_map_) {
482 kv.second->UpdateSendWindowOffset(new_window);
484 for (auto const& kv : dynamic_stream_map_) {
485 kv.second->UpdateSendWindowOffset(new_window);
489 void QuicSession::OnNewSessionFlowControlWindow(QuicStreamOffset new_window) {
490 if (new_window < kMinimumFlowControlSendWindow) {
491 LOG(ERROR) << "Peer sent us an invalid session flow control send window: "
492 << new_window
493 << ", below default: " << kMinimumFlowControlSendWindow;
494 if (connection_->connected()) {
495 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW);
497 return;
500 flow_controller_.UpdateSendWindowOffset(new_window);
503 void QuicSession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event) {
504 switch (event) {
505 // TODO(satyamshekhar): Move the logic of setting the encrypter/decrypter
506 // to QuicSession since it is the glue.
507 case ENCRYPTION_FIRST_ESTABLISHED:
508 break;
510 case ENCRYPTION_REESTABLISHED:
511 // Retransmit originally packets that were sent, since they can't be
512 // decrypted by the peer.
513 connection_->RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION);
514 break;
516 case HANDSHAKE_CONFIRMED:
517 LOG_IF(DFATAL, !config_.negotiated()) << ENDPOINT
518 << "Handshake confirmed without parameter negotiation.";
519 // Discard originally encrypted packets, since they can't be decrypted by
520 // the peer.
521 connection_->NeuterUnencryptedPackets();
522 break;
524 default:
525 LOG(ERROR) << ENDPOINT << "Got unknown handshake event: " << event;
529 void QuicSession::OnCryptoHandshakeMessageSent(
530 const CryptoHandshakeMessage& message) {
533 void QuicSession::OnCryptoHandshakeMessageReceived(
534 const CryptoHandshakeMessage& message) {
537 QuicConfig* QuicSession::config() {
538 return &config_;
541 void QuicSession::ActivateStream(ReliableQuicStream* stream) {
542 DVLOG(1) << ENDPOINT << "num_streams: " << dynamic_stream_map_.size()
543 << ". activating " << stream->id();
544 DCHECK(!ContainsKey(dynamic_stream_map_, stream->id()));
545 DCHECK(!ContainsKey(static_stream_map_, stream->id()));
546 dynamic_stream_map_[stream->id()] = stream;
547 // Increase the number of streams being emulated when a new one is opened.
548 connection_->SetNumOpenStreams(dynamic_stream_map_.size());
551 QuicStreamId QuicSession::GetNextStreamId() {
552 QuicStreamId id = next_stream_id_;
553 next_stream_id_ += 2;
554 return id;
557 ReliableQuicStream* QuicSession::GetStream(const QuicStreamId stream_id) {
558 StreamMap::iterator it = static_stream_map_.find(stream_id);
559 if (it != static_stream_map_.end()) {
560 return it->second;
562 return GetDynamicStream(stream_id);
565 void QuicSession::StreamDraining(QuicStreamId stream_id) {
566 DCHECK(ContainsKey(dynamic_stream_map_, stream_id));
567 if (!ContainsKey(draining_streams_, stream_id)) {
568 draining_streams_.insert(stream_id);
572 ReliableQuicStream* QuicSession::GetDynamicStream(
573 const QuicStreamId stream_id) {
574 if (static_stream_map_.find(stream_id) != static_stream_map_.end()) {
575 DLOG(FATAL) << "Attempt to call GetDynamicStream for a static stream";
576 return nullptr;
579 StreamMap::iterator it = dynamic_stream_map_.find(stream_id);
580 if (it != dynamic_stream_map_.end()) {
581 return it->second;
584 if (IsClosedStream(stream_id)) {
585 return nullptr;
588 if (stream_id % 2 == next_stream_id_ % 2) {
589 // We've received a frame for a locally-created stream that is not
590 // currently active. This is an error.
591 if (connection()->connected()) {
592 connection()->SendConnectionClose(QUIC_PACKET_FOR_NONEXISTENT_STREAM);
594 return nullptr;
597 return GetIncomingDynamicStream(stream_id);
600 ReliableQuicStream* QuicSession::GetIncomingDynamicStream(
601 QuicStreamId stream_id) {
602 if (IsClosedStream(stream_id)) {
603 return nullptr;
605 implicitly_created_streams_.erase(stream_id);
606 if (stream_id > largest_peer_created_stream_id_) {
607 if (FLAGS_exact_stream_id_delta) {
608 // Check if the number of streams that will be created (including
609 // implicitly open streams) would cause the number of open streams to
610 // exceed the limit. Note that the peer can create only
611 // alternately-numbered streams.
612 if ((stream_id - largest_peer_created_stream_id_) / 2 +
613 GetNumOpenStreams() >
614 get_max_open_streams()) {
615 DVLOG(1) << "Failed to create a new incoming stream with id:"
616 << stream_id << ". Already " << GetNumOpenStreams()
617 << " streams open, would exceed max " << get_max_open_streams()
618 << ".";
619 // We may already have sent a connection close due to multiple reset
620 // streams in the same packet.
621 if (connection()->connected()) {
622 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS);
624 return nullptr;
626 } else {
627 // Limit on the delta between stream IDs.
628 const QuicStreamId kMaxStreamIdDelta = 200;
629 if (stream_id - largest_peer_created_stream_id_ > kMaxStreamIdDelta) {
630 // We may already have sent a connection close due to multiple reset
631 // streams in the same packet.
632 if (connection()->connected()) {
633 LOG(ERROR) << "Trying to get stream: " << stream_id
634 << ", largest peer created stream: "
635 << largest_peer_created_stream_id_
636 << ", max delta: " << kMaxStreamIdDelta;
637 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID);
639 return nullptr;
642 for (QuicStreamId id = largest_peer_created_stream_id_ + 2;
643 id < stream_id;
644 id += 2) {
645 implicitly_created_streams_.insert(id);
647 largest_peer_created_stream_id_ = stream_id;
649 ReliableQuicStream* stream = CreateIncomingDynamicStream(stream_id);
650 if (stream == nullptr) {
651 return nullptr;
653 ActivateStream(stream);
654 return stream;
657 void QuicSession::set_max_open_streams(size_t max_open_streams) {
658 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams;
659 max_open_streams_ = max_open_streams;
662 bool QuicSession::goaway_sent() const {
663 return connection_->goaway_sent();
666 bool QuicSession::goaway_received() const {
667 return connection_->goaway_received();
670 bool QuicSession::IsClosedStream(QuicStreamId id) {
671 DCHECK_NE(0u, id);
672 if (ContainsKey(static_stream_map_, id) ||
673 ContainsKey(dynamic_stream_map_, id)) {
674 // Stream is active
675 return false;
677 if (id % 2 == next_stream_id_ % 2) {
678 // Locally created streams are strictly in-order. If the id is in the
679 // range of created streams and it's not active, it must have been closed.
680 return id < next_stream_id_;
682 // For peer created streams, we also need to consider implicitly created
683 // streams.
684 return id <= largest_peer_created_stream_id_ &&
685 !ContainsKey(implicitly_created_streams_, id);
688 size_t QuicSession::GetNumOpenStreams() const {
689 return dynamic_stream_map_.size() + implicitly_created_streams_.size() -
690 draining_streams_.size();
693 void QuicSession::MarkConnectionLevelWriteBlocked(QuicStreamId id,
694 QuicPriority priority) {
695 #ifndef NDEBUG
696 ReliableQuicStream* stream = GetStream(id);
697 if (stream != nullptr) {
698 LOG_IF(DFATAL, priority != stream->EffectivePriority())
699 << ENDPOINT << "Stream " << id
700 << "Priorities do not match. Got: " << priority
701 << " Expected: " << stream->EffectivePriority();
702 } else {
703 LOG(DFATAL) << "Marking unknown stream " << id << " blocked.";
705 #endif
707 if (id == kCryptoStreamId) {
708 DCHECK(!has_pending_handshake_);
709 has_pending_handshake_ = true;
710 // TODO(jar): Be sure to use the highest priority for the crypto stream,
711 // perhaps by adding a "special" priority for it that is higher than
712 // kHighestPriority.
713 priority = kHighestPriority;
715 write_blocked_streams_.PushBack(id, priority);
718 bool QuicSession::HasDataToWrite() const {
719 return write_blocked_streams_.HasWriteBlockedCryptoOrHeadersStream() ||
720 write_blocked_streams_.HasWriteBlockedDataStreams() ||
721 connection_->HasQueuedData();
724 void QuicSession::PostProcessAfterData() {
725 STLDeleteElements(&closed_streams_);
727 if (connection()->connected() &&
728 locally_closed_streams_highest_offset_.size() > max_open_streams_) {
729 // A buggy client may fail to send FIN/RSTs. Don't tolerate this.
730 connection_->SendConnectionClose(QUIC_TOO_MANY_UNFINISHED_STREAMS);
734 bool QuicSession::IsConnectionFlowControlBlocked() const {
735 return flow_controller_.IsBlocked();
738 bool QuicSession::IsStreamFlowControlBlocked() {
739 for (auto const& kv : static_stream_map_) {
740 if (kv.second->flow_controller()->IsBlocked()) {
741 return true;
744 for (auto const& kv : dynamic_stream_map_) {
745 if (kv.second->flow_controller()->IsBlocked()) {
746 return true;
749 return false;
752 void QuicSession::CrashIfInvalid() const {
753 #ifdef TEMP_INSTRUMENTATION_473893
754 Liveness liveness = liveness_;
756 if (liveness == ALIVE)
757 return;
759 // Copy relevant variables onto the stack to guarantee they will be available
760 // in minidumps, and then crash.
761 base::debug::StackTrace stack_trace = stack_trace_;
763 base::debug::Alias(&liveness);
764 base::debug::Alias(&stack_trace);
766 CHECK_EQ(ALIVE, liveness);
767 #endif
770 } // namespace net