| 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/core/quic_stream.h" | 5 #include "net/quic/core/quic_stream.h" |
| 6 | 6 |
| 7 #include "net/quic/core/quic_flow_controller.h" | 7 #include "net/quic/core/quic_flow_controller.h" |
| 8 #include "net/quic/core/quic_session.h" | 8 #include "net/quic/core/quic_session.h" |
| 9 #include "net/quic/platform/api/quic_bug_tracker.h" | 9 #include "net/quic/platform/api/quic_bug_tracker.h" |
| 10 #include "net/quic/platform/api/quic_logging.h" | 10 #include "net/quic/platform/api/quic_logging.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 stream_error_(QUIC_STREAM_NO_ERROR), | 58 stream_error_(QUIC_STREAM_NO_ERROR), |
| 59 connection_error_(QUIC_NO_ERROR), | 59 connection_error_(QUIC_NO_ERROR), |
| 60 read_side_closed_(false), | 60 read_side_closed_(false), |
| 61 write_side_closed_(false), | 61 write_side_closed_(false), |
| 62 fin_buffered_(false), | 62 fin_buffered_(false), |
| 63 fin_sent_(false), | 63 fin_sent_(false), |
| 64 fin_acked_(false), | 64 fin_acked_(false), |
| 65 fin_received_(false), | 65 fin_received_(false), |
| 66 rst_sent_(false), | 66 rst_sent_(false), |
| 67 rst_received_(false), | 67 rst_received_(false), |
| 68 is_deletable_(true), | |
| 69 perspective_(session_->perspective()), | 68 perspective_(session_->perspective()), |
| 70 flow_controller_(session_->connection(), | 69 flow_controller_(session_->connection(), |
| 71 id_, | 70 id_, |
| 72 perspective_, | 71 perspective_, |
| 73 GetReceivedFlowControlWindow(session), | 72 GetReceivedFlowControlWindow(session), |
| 74 GetInitialStreamFlowControlWindowToSend(session), | 73 GetInitialStreamFlowControlWindowToSend(session), |
| 75 session_->flow_controller()->auto_tune_receive_window(), | 74 session_->flow_controller()->auto_tune_receive_window(), |
| 76 session_->flow_controller()), | 75 session_->flow_controller()), |
| 77 connection_flow_controller_(session_->flow_controller()), | 76 connection_flow_controller_(session_->flow_controller()), |
| 78 stream_contributes_to_connection_flow_control_(true), | 77 stream_contributes_to_connection_flow_control_(true), |
| 79 busy_counter_(0), | 78 busy_counter_(0), |
| 80 add_random_padding_after_fin_(false), | 79 add_random_padding_after_fin_(false), |
| 81 ack_listener_(nullptr) { | 80 ack_listener_(nullptr) { |
| 82 SetFromConfig(); | 81 SetFromConfig(); |
| 83 } | 82 } |
| 84 | 83 |
| 85 QuicStream::~QuicStream() { | 84 QuicStream::~QuicStream() { |
| 86 QUIC_LOG_IF(WARNING, !is_deletable_) | 85 QUIC_LOG_IF(WARNING, !IsWaitingForAcks()) |
| 87 << "Stream destroyed while not deletable."; | 86 << "Stream destroyed while waiting for acks."; |
| 88 } | 87 } |
| 89 | 88 |
| 90 void QuicStream::SetFromConfig() {} | 89 void QuicStream::SetFromConfig() {} |
| 91 | 90 |
| 92 void QuicStream::OnStreamFrame(const QuicStreamFrame& frame) { | 91 void QuicStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 93 DCHECK_EQ(frame.stream_id, id_); | 92 DCHECK_EQ(frame.stream_id, id_); |
| 94 | 93 |
| 95 DCHECK(!(read_side_closed_ && write_side_closed_)); | 94 DCHECK(!(read_side_closed_ && write_side_closed_)); |
| 96 | 95 |
| 97 if (frame.fin) { | 96 if (frame.fin) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 rst_received_ = true; | 142 rst_received_ = true; |
| 144 MaybeIncreaseHighestReceivedOffset(frame.byte_offset); | 143 MaybeIncreaseHighestReceivedOffset(frame.byte_offset); |
| 145 | 144 |
| 146 stream_error_ = frame.error_code; | 145 stream_error_ = frame.error_code; |
| 147 CloseWriteSide(); | 146 CloseWriteSide(); |
| 148 CloseReadSide(); | 147 CloseReadSide(); |
| 149 } | 148 } |
| 150 | 149 |
| 151 void QuicStream::OnConnectionClosed(QuicErrorCode error, | 150 void QuicStream::OnConnectionClosed(QuicErrorCode error, |
| 152 ConnectionCloseSource /*source*/) { | 151 ConnectionCloseSource /*source*/) { |
| 153 if (session()->use_stream_notifier()) { | |
| 154 // Connection is closed, consider this stream as deletable. | |
| 155 SetIsDeletable(true); | |
| 156 } | |
| 157 if (read_side_closed_ && write_side_closed_) { | 152 if (read_side_closed_ && write_side_closed_) { |
| 158 return; | 153 return; |
| 159 } | 154 } |
| 160 if (error != QUIC_NO_ERROR) { | 155 if (error != QUIC_NO_ERROR) { |
| 161 stream_error_ = QUIC_STREAM_CONNECTION_ERROR; | 156 stream_error_ = QUIC_STREAM_CONNECTION_ERROR; |
| 162 connection_error_ = error; | 157 connection_error_ = error; |
| 163 } | 158 } |
| 164 | 159 |
| 165 CloseWriteSide(); | 160 CloseWriteSide(); |
| 166 CloseReadSide(); | 161 CloseReadSide(); |
| 167 } | 162 } |
| 168 | 163 |
| 169 void QuicStream::OnFinRead() { | 164 void QuicStream::OnFinRead() { |
| 170 DCHECK(sequencer_.IsClosed()); | 165 DCHECK(sequencer_.IsClosed()); |
| 171 // OnFinRead can be called due to a FIN flag in a headers block, so there may | 166 // OnFinRead can be called due to a FIN flag in a headers block, so there may |
| 172 // have been no OnStreamFrame call with a FIN in the frame. | 167 // have been no OnStreamFrame call with a FIN in the frame. |
| 173 fin_received_ = true; | 168 fin_received_ = true; |
| 174 // If fin_sent_ is true, then CloseWriteSide has already been called, and the | 169 // If fin_sent_ is true, then CloseWriteSide has already been called, and the |
| 175 // stream will be destroyed by CloseReadSide, so don't need to call | 170 // stream will be destroyed by CloseReadSide, so don't need to call |
| 176 // StreamDraining. | 171 // StreamDraining. |
| 177 CloseReadSide(); | 172 CloseReadSide(); |
| 178 } | 173 } |
| 179 | 174 |
| 180 void QuicStream::Reset(QuicRstStreamErrorCode error) { | 175 void QuicStream::Reset(QuicRstStreamErrorCode error) { |
| 181 stream_error_ = error; | 176 stream_error_ = error; |
| 182 // Sending a RstStream results in calling CloseStream. | 177 // Sending a RstStream results in calling CloseStream. |
| 183 session()->SendRstStream(id(), error, stream_bytes_written_); | 178 session()->SendRstStream(id(), error, stream_bytes_written_); |
| 184 rst_sent_ = true; | 179 rst_sent_ = true; |
| 185 if (session()->use_stream_notifier() && error != QUIC_STREAM_NO_ERROR) { | 180 if (session()->use_stream_notifier() && !IsWaitingForAcks()) { |
| 186 // This stream is deletable as data is not going to be retransmitted. | 181 session_->OnStreamDoneWaitingForAcks(id_); |
| 187 SetIsDeletable(true); | |
| 188 } | 182 } |
| 189 } | 183 } |
| 190 | 184 |
| 191 void QuicStream::CloseConnectionWithDetails(QuicErrorCode error, | 185 void QuicStream::CloseConnectionWithDetails(QuicErrorCode error, |
| 192 const string& details) { | 186 const string& details) { |
| 193 session()->connection()->CloseConnection( | 187 session()->connection()->CloseConnection( |
| 194 error, details, ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 188 error, details, ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 195 } | 189 } |
| 196 | 190 |
| 197 void QuicStream::WriteOrBufferData( | 191 void QuicStream::WriteOrBufferData( |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 CloseWriteSide(); | 351 CloseWriteSide(); |
| 358 } else if (fin && !consumed_data.fin_consumed) { | 352 } else if (fin && !consumed_data.fin_consumed) { |
| 359 session_->MarkConnectionLevelWriteBlocked(id()); | 353 session_->MarkConnectionLevelWriteBlocked(id()); |
| 360 } | 354 } |
| 361 } else { | 355 } else { |
| 362 session_->MarkConnectionLevelWriteBlocked(id()); | 356 session_->MarkConnectionLevelWriteBlocked(id()); |
| 363 } | 357 } |
| 364 if (consumed_data.bytes_consumed > 0 || consumed_data.fin_consumed) { | 358 if (consumed_data.bytes_consumed > 0 || consumed_data.fin_consumed) { |
| 365 busy_counter_ = 0; | 359 busy_counter_ = 0; |
| 366 } | 360 } |
| 367 if (session()->use_stream_notifier() && | |
| 368 (stream_bytes_written_ > stream_bytes_acked_ || | |
| 369 (fin && consumed_data.fin_consumed))) { | |
| 370 // This stream is considered not deletable when it has unacked data | |
| 371 // (including FIN). | |
| 372 SetIsDeletable(false); | |
| 373 } | |
| 374 return consumed_data; | 361 return consumed_data; |
| 375 } | 362 } |
| 376 | 363 |
| 377 QuicConsumedData QuicStream::WritevDataInner( | 364 QuicConsumedData QuicStream::WritevDataInner( |
| 378 QuicIOVector iov, | 365 QuicIOVector iov, |
| 379 QuicStreamOffset offset, | 366 QuicStreamOffset offset, |
| 380 bool fin, | 367 bool fin, |
| 381 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { | 368 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { |
| 382 StreamSendingState state = fin ? FIN : NO_FIN; | 369 StreamSendingState state = fin ? FIN : NO_FIN; |
| 383 if (fin && add_random_padding_after_fin_) { | 370 if (fin && add_random_padding_after_fin_) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 void QuicStream::UpdateSendWindowOffset(QuicStreamOffset new_window) { | 494 void QuicStream::UpdateSendWindowOffset(QuicStreamOffset new_window) { |
| 508 if (flow_controller_.UpdateSendWindowOffset(new_window)) { | 495 if (flow_controller_.UpdateSendWindowOffset(new_window)) { |
| 509 OnCanWrite(); | 496 OnCanWrite(); |
| 510 } | 497 } |
| 511 } | 498 } |
| 512 | 499 |
| 513 void QuicStream::AddRandomPaddingAfterFin() { | 500 void QuicStream::AddRandomPaddingAfterFin() { |
| 514 add_random_padding_after_fin_ = true; | 501 add_random_padding_after_fin_ = true; |
| 515 } | 502 } |
| 516 | 503 |
| 517 void QuicStream::SetIsDeletable(bool is_deletable) { | |
| 518 is_deletable_ = is_deletable; | |
| 519 if (is_deletable) { | |
| 520 session_->MarkStreamDeletable(id_); | |
| 521 } | |
| 522 } | |
| 523 | |
| 524 void QuicStream::OnStreamFrameAcked(const QuicStreamFrame& frame, | 504 void QuicStream::OnStreamFrameAcked(const QuicStreamFrame& frame, |
| 525 QuicTime::Delta ack_delay_time) { | 505 QuicTime::Delta ack_delay_time) { |
| 526 DCHECK_EQ(frame.stream_id, id()); | 506 DCHECK_EQ(frame.stream_id, id()); |
| 527 stream_bytes_acked_ += frame.data_length; | 507 stream_bytes_acked_ += frame.data_length; |
| 528 if (stream_bytes_acked_ > stream_bytes_written_) { | 508 if (stream_bytes_acked_ > stream_bytes_written_) { |
| 529 CloseConnectionWithDetails(QUIC_INTERNAL_ERROR, | 509 CloseConnectionWithDetails(QUIC_INTERNAL_ERROR, |
| 530 "Unsent stream data is acked"); | 510 "Unsent stream data is acked"); |
| 531 return; | 511 return; |
| 532 } | 512 } |
| 533 if (frame.fin) { | 513 if (frame.fin) { |
| 534 fin_acked_ = true; | 514 fin_acked_ = true; |
| 535 } | 515 } |
| 536 if (ack_listener_ != nullptr) { | 516 if (ack_listener_ != nullptr) { |
| 537 ack_listener_->OnPacketAcked(frame.data_length, ack_delay_time); | 517 ack_listener_->OnPacketAcked(frame.data_length, ack_delay_time); |
| 538 } | 518 } |
| 539 if (stream_bytes_acked_ == stream_bytes_written_ && | 519 if (!IsWaitingForAcks()) { |
| 540 ((fin_sent_ && fin_acked_) || !fin_sent_)) { | 520 session_->OnStreamDoneWaitingForAcks(id_); |
| 541 // This stream is considered deletable when all sent bytes are acked | |
| 542 // (including FIN). | |
| 543 SetIsDeletable(true); | |
| 544 } | 521 } |
| 545 } | 522 } |
| 546 | 523 |
| 547 void QuicStream::OnStreamFrameRetransmitted(const QuicStreamFrame& frame) { | 524 void QuicStream::OnStreamFrameRetransmitted(const QuicStreamFrame& frame) { |
| 548 if (ack_listener_ != nullptr) { | 525 if (ack_listener_ != nullptr) { |
| 549 ack_listener_->OnPacketRetransmitted(frame.data_length); | 526 ack_listener_->OnPacketRetransmitted(frame.data_length); |
| 550 } | 527 } |
| 551 } | 528 } |
| 552 | 529 |
| 530 bool QuicStream::IsWaitingForAcks() const { |
| 531 if (rst_sent_ && stream_error_ != QUIC_STREAM_NO_ERROR) { |
| 532 // RST_STREAM sent because of error. |
| 533 return false; |
| 534 } |
| 535 if (connection_error_ != QUIC_NO_ERROR) { |
| 536 // Connection encounters error and is going to close. |
| 537 return false; |
| 538 } |
| 539 if (stream_bytes_acked_ == stream_bytes_written_ && |
| 540 ((fin_sent_ && fin_acked_) || !fin_sent_)) { |
| 541 // All sent data has been acked. |
| 542 return false; |
| 543 } |
| 544 |
| 545 return true; |
| 546 } |
| 547 |
| 553 } // namespace net | 548 } // namespace net |
| OLD | NEW |