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"
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
;
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
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
{
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 bool WillingAndAbleToWrite() const override
{
87 return session_
->WillingAndAbleToWrite();
90 bool HasPendingHandshake() const override
{
91 return session_
->HasPendingHandshake();
94 bool HasOpenDynamicStreams() const override
{
95 return session_
->HasOpenDynamicStreams();
99 QuicSession
* session_
;
102 QuicSession::QuicSession(QuicConnection
* connection
, const QuicConfig
& config
)
103 : connection_(connection
),
104 visitor_shim_(new VisitorShim(this)),
106 max_open_streams_(config_
.MaxStreamsPerConnection()),
107 next_stream_id_(perspective() == Perspective::IS_SERVER
? 2 : 3),
108 largest_peer_created_stream_id_(
109 perspective() == Perspective::IS_SERVER
? 1 : 0),
110 error_(QUIC_NO_ERROR
),
111 flow_controller_(connection_
.get(),
114 kMinimumFlowControlSendWindow
,
115 config_
.GetInitialSessionFlowControlWindowToSend(),
117 has_pending_handshake_(false) {
120 void QuicSession::Initialize() {
121 connection_
->set_visitor(visitor_shim_
.get());
122 connection_
->SetFromConfig(config_
);
124 DCHECK_EQ(kCryptoStreamId
, GetCryptoStream()->id());
125 static_stream_map_
[kCryptoStreamId
] = GetCryptoStream();
128 QuicSession::~QuicSession() {
129 #ifdef TEMP_INSTRUMENTATION_473893
131 stack_trace_
= base::debug::StackTrace();
133 // Probably not necessary, but just in case compiler tries to optimize out the
134 // writes to liveness_ and stack_trace_.
135 base::debug::Alias(&liveness_
);
136 base::debug::Alias(&stack_trace_
);
139 STLDeleteElements(&closed_streams_
);
140 STLDeleteValues(&dynamic_stream_map_
);
143 locally_closed_streams_highest_offset_
.size() > max_open_streams_
)
144 << "Surprisingly high number of locally closed streams still waiting for "
145 "final byte offset: " << locally_closed_streams_highest_offset_
.size();
148 void QuicSession::OnStreamFrame(const QuicStreamFrame
& frame
) {
149 // TODO(rch) deal with the error case of stream id 0.
150 QuicStreamId stream_id
= frame
.stream_id
;
151 ReliableQuicStream
* stream
= GetStream(stream_id
);
153 // The stream no longer exists, but we may still be interested in the
154 // final stream byte offset sent by the peer. A frame with a FIN can give
157 QuicStreamOffset final_byte_offset
= frame
.offset
+ frame
.data
.size();
158 UpdateFlowControlOnFinalReceivedByteOffset(stream_id
, final_byte_offset
);
162 stream
->OnStreamFrame(frame
);
165 void QuicSession::OnRstStream(const QuicRstStreamFrame
& frame
) {
166 if (ContainsKey(static_stream_map_
, frame
.stream_id
)) {
167 connection()->SendConnectionCloseWithDetails(
168 QUIC_INVALID_STREAM_ID
, "Attempt to reset a static stream");
172 ReliableQuicStream
* stream
= GetDynamicStream(frame
.stream_id
);
174 // The RST frame contains the final byte offset for the stream: we can now
175 // update the connection level flow controller if needed.
176 UpdateFlowControlOnFinalReceivedByteOffset(frame
.stream_id
,
178 return; // Errors are handled by GetStream.
181 stream
->OnStreamReset(frame
);
184 void QuicSession::OnGoAway(const QuicGoAwayFrame
& frame
) {
185 DCHECK(frame
.last_good_stream_id
< next_stream_id_
);
188 void QuicSession::OnConnectionClosed(QuicErrorCode error
, bool from_peer
) {
189 DCHECK(!connection_
->connected());
190 if (error_
== QUIC_NO_ERROR
) {
194 while (!dynamic_stream_map_
.empty()) {
195 StreamMap::iterator it
= dynamic_stream_map_
.begin();
196 QuicStreamId id
= it
->first
;
197 it
->second
->OnConnectionClosed(error
, from_peer
);
198 // The stream should call CloseStream as part of OnConnectionClosed.
199 if (dynamic_stream_map_
.find(id
) != dynamic_stream_map_
.end()) {
200 LOG(DFATAL
) << ENDPOINT
201 << "Stream failed to close under OnConnectionClosed";
207 void QuicSession::OnSuccessfulVersionNegotiation(const QuicVersion
& version
) {
210 void QuicSession::OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) {
211 // Stream may be closed by the time we receive a WINDOW_UPDATE, so we can't
212 // assume that it still exists.
213 QuicStreamId stream_id
= frame
.stream_id
;
214 if (stream_id
== kConnectionLevelId
) {
215 // This is a window update that applies to the connection, rather than an
216 // individual stream.
217 DVLOG(1) << ENDPOINT
<< "Received connection level flow control window "
218 "update with byte offset: "
219 << frame
.byte_offset
;
220 if (flow_controller_
.UpdateSendWindowOffset(frame
.byte_offset
)) {
221 // Connection level flow control window has increased, so blocked streams
223 // TODO(ianswett): I suspect this can be delayed until the packet
224 // processing is complete.
225 if (!FLAGS_quic_dont_write_when_flow_unblocked
) {
231 ReliableQuicStream
* stream
= GetStream(stream_id
);
233 stream
->OnWindowUpdateFrame(frame
);
237 void QuicSession::OnBlockedFrame(const QuicBlockedFrame
& frame
) {
238 // TODO(rjshade): Compare our flow control receive windows for specified
239 // streams: if we have a large window then maybe something
240 // had gone wrong with the flow control accounting.
242 << "Received BLOCKED frame with stream id: " << frame
.stream_id
;
245 void QuicSession::OnCanWrite() {
246 // We limit the number of writes to the number of pending streams. If more
247 // streams become pending, WillingAndAbleToWrite will be true, which will
248 // cause the connection to request resumption before yielding to other
250 size_t num_writes
= write_blocked_streams_
.NumBlockedStreams();
251 if (flow_controller_
.IsBlocked()) {
252 // If we are connection level flow control blocked, then only allow the
253 // crypto and headers streams to try writing as all other streams will be
256 if (write_blocked_streams_
.crypto_stream_blocked()) {
259 if (write_blocked_streams_
.headers_stream_blocked()) {
263 if (num_writes
== 0) {
267 QuicConnection::ScopedPacketBundler
ack_bundler(
268 connection_
.get(), QuicConnection::NO_ACK
);
269 for (size_t i
= 0; i
< num_writes
; ++i
) {
270 if (!(write_blocked_streams_
.HasWriteBlockedCryptoOrHeadersStream() ||
271 write_blocked_streams_
.HasWriteBlockedDataStreams())) {
272 // Writing one stream removed another!? Something's broken.
273 LOG(DFATAL
) << "WriteBlockedStream is missing";
274 connection_
->CloseConnection(QUIC_INTERNAL_ERROR
, false);
277 if (!connection_
->CanWriteStreamData()) {
280 QuicStreamId stream_id
= write_blocked_streams_
.PopFront();
281 if (stream_id
== kCryptoStreamId
) {
282 has_pending_handshake_
= false; // We just popped it.
284 ReliableQuicStream
* stream
= GetStream(stream_id
);
285 if (stream
!= nullptr && !stream
->flow_controller()->IsBlocked()) {
286 // If the stream can't write all bytes, it'll re-add itself to the blocked
288 stream
->OnCanWrite();
293 bool QuicSession::WillingAndAbleToWrite() const {
294 // If the crypto or headers streams are blocked, we want to schedule a write -
295 // they don't get blocked by connection level flow control. Otherwise only
296 // schedule a write if we are not flow control blocked at the connection
298 return write_blocked_streams_
.HasWriteBlockedCryptoOrHeadersStream() ||
299 (!flow_controller_
.IsBlocked() &&
300 write_blocked_streams_
.HasWriteBlockedDataStreams());
303 bool QuicSession::HasPendingHandshake() const {
304 return has_pending_handshake_
;
307 bool QuicSession::HasOpenDynamicStreams() const {
308 return GetNumOpenStreams() > 0;
311 QuicConsumedData
QuicSession::WritevData(
313 const QuicIOVector
& iov
,
314 QuicStreamOffset offset
,
316 FecProtection fec_protection
,
317 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
) {
318 return connection_
->SendStreamData(id
, iov
, offset
, fin
, fec_protection
,
319 ack_notifier_delegate
);
322 void QuicSession::SendRstStream(QuicStreamId id
,
323 QuicRstStreamErrorCode error
,
324 QuicStreamOffset bytes_written
) {
325 if (ContainsKey(static_stream_map_
, id
)) {
326 LOG(DFATAL
) << "Cannot send RST for a static stream with ID " << id
;
330 if (connection()->connected()) {
331 // Only send a RST_STREAM frame if still connected.
332 connection_
->SendRstStream(id
, error
, bytes_written
);
334 CloseStreamInner(id
, true);
337 void QuicSession::SendGoAway(QuicErrorCode error_code
, const string
& reason
) {
342 connection_
->SendGoAway(error_code
, largest_peer_created_stream_id_
, reason
);
345 void QuicSession::CloseStream(QuicStreamId stream_id
) {
346 CloseStreamInner(stream_id
, false);
349 void QuicSession::CloseStreamInner(QuicStreamId stream_id
,
350 bool locally_reset
) {
351 DVLOG(1) << ENDPOINT
<< "Closing stream " << stream_id
;
353 StreamMap::iterator it
= dynamic_stream_map_
.find(stream_id
);
354 if (it
== dynamic_stream_map_
.end()) {
355 // When CloseStreamInner has been called recursively (via
356 // ReliableQuicStream::OnClose), the stream will already have been deleted
357 // from stream_map_, so return immediately.
358 DVLOG(1) << ENDPOINT
<< "Stream is already closed: " << stream_id
;
361 ReliableQuicStream
* stream
= it
->second
;
363 // Tell the stream that a RST has been sent.
365 stream
->set_rst_sent(true);
368 closed_streams_
.push_back(it
->second
);
370 // If we haven't received a FIN or RST for this stream, we need to keep track
371 // of the how many bytes the stream's flow controller believes it has
372 // received, for accurate connection level flow control accounting.
373 if (!stream
->HasFinalReceivedByteOffset()) {
374 locally_closed_streams_highest_offset_
[stream_id
] =
375 stream
->flow_controller()->highest_received_byte_offset();
378 dynamic_stream_map_
.erase(it
);
379 draining_streams_
.erase(stream_id
);
381 // Decrease the number of streams being emulated when a new one is opened.
382 connection_
->SetNumOpenStreams(dynamic_stream_map_
.size());
385 void QuicSession::UpdateFlowControlOnFinalReceivedByteOffset(
386 QuicStreamId stream_id
, QuicStreamOffset final_byte_offset
) {
387 map
<QuicStreamId
, QuicStreamOffset
>::iterator it
=
388 locally_closed_streams_highest_offset_
.find(stream_id
);
389 if (it
== locally_closed_streams_highest_offset_
.end()) {
393 DVLOG(1) << ENDPOINT
<< "Received final byte offset " << final_byte_offset
394 << " for stream " << stream_id
;
395 QuicByteCount offset_diff
= final_byte_offset
- it
->second
;
396 if (flow_controller_
.UpdateHighestReceivedOffset(
397 flow_controller_
.highest_received_byte_offset() + offset_diff
)) {
398 // If the final offset violates flow control, close the connection now.
399 if (flow_controller_
.FlowControlViolation()) {
400 connection_
->SendConnectionClose(
401 QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
);
406 flow_controller_
.AddBytesConsumed(offset_diff
);
407 locally_closed_streams_highest_offset_
.erase(it
);
410 bool QuicSession::IsEncryptionEstablished() {
411 return GetCryptoStream()->encryption_established();
414 bool QuicSession::IsCryptoHandshakeConfirmed() {
415 return GetCryptoStream()->handshake_confirmed();
418 void QuicSession::OnConfigNegotiated() {
419 connection_
->SetFromConfig(config_
);
421 uint32 max_streams
= config_
.MaxStreamsPerConnection();
422 if (perspective() == Perspective::IS_SERVER
) {
423 // A server should accept a small number of additional streams beyond the
424 // limit sent to the client. This helps avoid early connection termination
425 // when FIN/RSTs for old streams are lost or arrive out of order.
426 // Use a minimum number of additional streams, or a percentage increase,
427 // whichever is larger.
429 max(max_streams
+ kMaxStreamsMinimumIncrement
,
430 static_cast<uint32
>(max_streams
* kMaxStreamsMultiplier
));
432 if (config_
.HasReceivedConnectionOptions()) {
433 if (ContainsQuicTag(config_
.ReceivedConnectionOptions(), kAFCW
)) {
434 EnableAutoTuneReceiveWindow();
436 // The following variations change the initial receive flow control window
437 // size for streams. For simplicity reasons, do not try to effect
438 // existing streams but only future ones.
439 if (ContainsQuicTag(config_
.ReceivedConnectionOptions(), kIFW5
)) {
440 config_
.SetInitialStreamFlowControlWindowToSend(32 * 1024);
442 if (ContainsQuicTag(config_
.ReceivedConnectionOptions(), kIFW6
)) {
443 config_
.SetInitialStreamFlowControlWindowToSend(64 * 1024);
445 if (ContainsQuicTag(config_
.ReceivedConnectionOptions(), kIFW7
)) {
446 config_
.SetInitialStreamFlowControlWindowToSend(128 * 1024);
450 set_max_open_streams(max_streams
);
452 if (config_
.HasReceivedInitialStreamFlowControlWindowBytes()) {
453 // Streams which were created before the SHLO was received (0-RTT
454 // requests) are now informed of the peer's initial flow control window.
455 OnNewStreamFlowControlWindow(
456 config_
.ReceivedInitialStreamFlowControlWindowBytes());
458 if (config_
.HasReceivedInitialSessionFlowControlWindowBytes()) {
459 OnNewSessionFlowControlWindow(
460 config_
.ReceivedInitialSessionFlowControlWindowBytes());
464 void QuicSession::EnableAutoTuneReceiveWindow() {
465 flow_controller_
.set_auto_tune_receive_window(true);
466 // Inform all existing streams about the new window.
467 for (auto const& kv
: static_stream_map_
) {
468 kv
.second
->flow_controller()->set_auto_tune_receive_window(true);
470 for (auto const& kv
: dynamic_stream_map_
) {
471 kv
.second
->flow_controller()->set_auto_tune_receive_window(true);
475 void QuicSession::OnNewStreamFlowControlWindow(QuicStreamOffset new_window
) {
476 if (new_window
< kMinimumFlowControlSendWindow
) {
477 LOG(ERROR
) << "Peer sent us an invalid stream flow control send window: "
479 << ", below default: " << kMinimumFlowControlSendWindow
;
480 if (connection_
->connected()) {
481 connection_
->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
);
486 // Inform all existing streams about the new window.
487 for (auto const& kv
: static_stream_map_
) {
488 kv
.second
->UpdateSendWindowOffset(new_window
);
490 for (auto const& kv
: dynamic_stream_map_
) {
491 kv
.second
->UpdateSendWindowOffset(new_window
);
495 void QuicSession::OnNewSessionFlowControlWindow(QuicStreamOffset new_window
) {
496 if (new_window
< kMinimumFlowControlSendWindow
) {
497 LOG(ERROR
) << "Peer sent us an invalid session flow control send window: "
499 << ", below default: " << kMinimumFlowControlSendWindow
;
500 if (connection_
->connected()) {
501 connection_
->SendConnectionClose(QUIC_FLOW_CONTROL_INVALID_WINDOW
);
506 flow_controller_
.UpdateSendWindowOffset(new_window
);
509 void QuicSession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event
) {
511 // TODO(satyamshekhar): Move the logic of setting the encrypter/decrypter
512 // to QuicSession since it is the glue.
513 case ENCRYPTION_FIRST_ESTABLISHED
:
516 case ENCRYPTION_REESTABLISHED
:
517 // Retransmit originally packets that were sent, since they can't be
518 // decrypted by the peer.
519 connection_
->RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION
);
522 case HANDSHAKE_CONFIRMED
:
523 LOG_IF(DFATAL
, !config_
.negotiated()) << ENDPOINT
524 << "Handshake confirmed without parameter negotiation.";
525 // Discard originally encrypted packets, since they can't be decrypted by
527 connection_
->NeuterUnencryptedPackets();
531 LOG(ERROR
) << ENDPOINT
<< "Got unknown handshake event: " << event
;
535 void QuicSession::OnCryptoHandshakeMessageSent(
536 const CryptoHandshakeMessage
& message
) {
539 void QuicSession::OnCryptoHandshakeMessageReceived(
540 const CryptoHandshakeMessage
& message
) {
543 QuicConfig
* QuicSession::config() {
547 void QuicSession::ActivateStream(ReliableQuicStream
* stream
) {
548 DVLOG(1) << ENDPOINT
<< "num_streams: " << dynamic_stream_map_
.size()
549 << ". activating " << stream
->id();
550 DCHECK(!ContainsKey(dynamic_stream_map_
, stream
->id()));
551 DCHECK(!ContainsKey(static_stream_map_
, stream
->id()));
552 dynamic_stream_map_
[stream
->id()] = stream
;
553 // Increase the number of streams being emulated when a new one is opened.
554 connection_
->SetNumOpenStreams(dynamic_stream_map_
.size());
557 QuicStreamId
QuicSession::GetNextStreamId() {
558 QuicStreamId id
= next_stream_id_
;
559 next_stream_id_
+= 2;
563 ReliableQuicStream
* QuicSession::GetStream(const QuicStreamId stream_id
) {
564 StreamMap::iterator it
= static_stream_map_
.find(stream_id
);
565 if (it
!= static_stream_map_
.end()) {
568 return GetDynamicStream(stream_id
);
571 void QuicSession::StreamDraining(QuicStreamId stream_id
) {
572 DCHECK(ContainsKey(dynamic_stream_map_
, stream_id
));
573 if (!ContainsKey(draining_streams_
, stream_id
)) {
574 draining_streams_
.insert(stream_id
);
578 ReliableQuicStream
* QuicSession::GetDynamicStream(
579 const QuicStreamId stream_id
) {
580 if (static_stream_map_
.find(stream_id
) != static_stream_map_
.end()) {
581 DLOG(FATAL
) << "Attempt to call GetDynamicStream for a static stream";
585 StreamMap::iterator it
= dynamic_stream_map_
.find(stream_id
);
586 if (it
!= dynamic_stream_map_
.end()) {
590 if (IsClosedStream(stream_id
)) {
594 if (stream_id
% 2 == next_stream_id_
% 2) {
595 // We've received a frame for a locally-created stream that is not
596 // currently active. This is an error.
597 if (connection()->connected()) {
598 connection()->SendConnectionClose(QUIC_PACKET_FOR_NONEXISTENT_STREAM
);
603 return GetIncomingDynamicStream(stream_id
);
606 ReliableQuicStream
* QuicSession::GetIncomingDynamicStream(
607 QuicStreamId stream_id
) {
608 if (IsClosedStream(stream_id
)) {
611 implicitly_created_streams_
.erase(stream_id
);
612 if (stream_id
> largest_peer_created_stream_id_
) {
613 if (FLAGS_exact_stream_id_delta
) {
614 // Check if the number of streams that will be created (including
615 // implicitly open streams) would cause the number of open streams to
616 // exceed the limit. Note that the peer can create only
617 // alternately-numbered streams.
618 if ((stream_id
- largest_peer_created_stream_id_
) / 2 +
619 GetNumOpenStreams() >
620 get_max_open_streams()) {
621 DVLOG(1) << "Failed to create a new incoming stream with id:"
622 << stream_id
<< ". Already " << GetNumOpenStreams()
623 << " streams open, would exceed max " << get_max_open_streams()
625 // We may already have sent a connection close due to multiple reset
626 // streams in the same packet.
627 if (connection()->connected()) {
628 connection()->SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS
);
633 // Limit on the delta between stream IDs.
634 const QuicStreamId kMaxStreamIdDelta
= 200;
635 if (stream_id
- largest_peer_created_stream_id_
> kMaxStreamIdDelta
) {
636 // We may already have sent a connection close due to multiple reset
637 // streams in the same packet.
638 if (connection()->connected()) {
639 LOG(ERROR
) << "Trying to get stream: " << stream_id
640 << ", largest peer created stream: "
641 << largest_peer_created_stream_id_
642 << ", max delta: " << kMaxStreamIdDelta
;
643 connection()->SendConnectionClose(QUIC_INVALID_STREAM_ID
);
648 for (QuicStreamId id
= largest_peer_created_stream_id_
+ 2;
651 implicitly_created_streams_
.insert(id
);
653 largest_peer_created_stream_id_
= stream_id
;
655 ReliableQuicStream
* stream
= CreateIncomingDynamicStream(stream_id
);
656 if (stream
== nullptr) {
659 ActivateStream(stream
);
663 void QuicSession::set_max_open_streams(size_t max_open_streams
) {
664 DVLOG(1) << "Setting max_open_streams_ to " << max_open_streams
;
665 max_open_streams_
= max_open_streams
;
668 bool QuicSession::goaway_sent() const {
669 return connection_
->goaway_sent();
672 bool QuicSession::goaway_received() const {
673 return connection_
->goaway_received();
676 bool QuicSession::IsClosedStream(QuicStreamId id
) {
678 if (ContainsKey(static_stream_map_
, id
) ||
679 ContainsKey(dynamic_stream_map_
, id
)) {
683 if (id
% 2 == next_stream_id_
% 2) {
684 // Locally created streams are strictly in-order. If the id is in the
685 // range of created streams and it's not active, it must have been closed.
686 return id
< next_stream_id_
;
688 // For peer created streams, we also need to consider implicitly created
690 return id
<= largest_peer_created_stream_id_
&&
691 !ContainsKey(implicitly_created_streams_
, id
);
694 size_t QuicSession::GetNumOpenStreams() const {
695 return dynamic_stream_map_
.size() + implicitly_created_streams_
.size() -
696 draining_streams_
.size();
699 void QuicSession::MarkConnectionLevelWriteBlocked(QuicStreamId id
,
700 QuicPriority priority
) {
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();
709 LOG(DFATAL
) << "Marking unknown stream " << id
<< " blocked.";
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
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 for (auto const& kv
: static_stream_map_
) {
746 if (kv
.second
->flow_controller()->IsBlocked()) {
750 for (auto const& kv
: dynamic_stream_map_
) {
751 if (kv
.second
->flow_controller()->IsBlocked()) {
758 void QuicSession::CrashIfInvalid() const {
759 #ifdef TEMP_INSTRUMENTATION_473893
760 Liveness liveness
= liveness_
;
762 if (liveness
== ALIVE
)
765 // Copy relevant variables onto the stack to guarantee they will be available
766 // in minidumps, and then crash.
767 base::debug::StackTrace stack_trace
= stack_trace_
;
769 base::debug::Alias(&liveness
);
770 base::debug::Alias(&stack_trace
);
772 CHECK_EQ(ALIVE
, liveness
);