| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 QuicStream::PendingData::~PendingData() {} | 48 QuicStream::PendingData::~PendingData() {} |
| 49 | 49 |
| 50 QuicStream::QuicStream(QuicStreamId id, QuicSession* session) | 50 QuicStream::QuicStream(QuicStreamId id, QuicSession* session) |
| 51 : queued_data_bytes_(0), | 51 : queued_data_bytes_(0), |
| 52 sequencer_(this, session->connection()->clock()), | 52 sequencer_(this, session->connection()->clock()), |
| 53 id_(id), | 53 id_(id), |
| 54 session_(session), | 54 session_(session), |
| 55 stream_bytes_read_(0), | 55 stream_bytes_read_(0), |
| 56 stream_bytes_written_(0), | 56 stream_bytes_written_(0), |
| 57 stream_bytes_acked_(0), |
| 57 stream_error_(QUIC_STREAM_NO_ERROR), | 58 stream_error_(QUIC_STREAM_NO_ERROR), |
| 58 connection_error_(QUIC_NO_ERROR), | 59 connection_error_(QUIC_NO_ERROR), |
| 59 read_side_closed_(false), | 60 read_side_closed_(false), |
| 60 write_side_closed_(false), | 61 write_side_closed_(false), |
| 61 fin_buffered_(false), | 62 fin_buffered_(false), |
| 62 fin_sent_(false), | 63 fin_sent_(false), |
| 64 fin_acked_(false), |
| 63 fin_received_(false), | 65 fin_received_(false), |
| 64 rst_sent_(false), | 66 rst_sent_(false), |
| 65 rst_received_(false), | 67 rst_received_(false), |
| 68 is_deletable_(true), |
| 66 perspective_(session_->perspective()), | 69 perspective_(session_->perspective()), |
| 67 flow_controller_(session_->connection(), | 70 flow_controller_(session_->connection(), |
| 68 id_, | 71 id_, |
| 69 perspective_, | 72 perspective_, |
| 70 GetReceivedFlowControlWindow(session), | 73 GetReceivedFlowControlWindow(session), |
| 71 GetInitialStreamFlowControlWindowToSend(session), | 74 GetInitialStreamFlowControlWindowToSend(session), |
| 72 session_->flow_controller()->auto_tune_receive_window(), | 75 session_->flow_controller()->auto_tune_receive_window(), |
| 73 session_->flow_controller()), | 76 session_->flow_controller()), |
| 74 connection_flow_controller_(session_->flow_controller()), | 77 connection_flow_controller_(session_->flow_controller()), |
| 75 stream_contributes_to_connection_flow_control_(true), | 78 stream_contributes_to_connection_flow_control_(true), |
| 76 busy_counter_(0), | 79 busy_counter_(0), |
| 77 add_random_padding_after_fin_(false) { | 80 add_random_padding_after_fin_(false), |
| 81 ack_listener_(nullptr) { |
| 78 SetFromConfig(); | 82 SetFromConfig(); |
| 79 } | 83 } |
| 80 | 84 |
| 81 QuicStream::~QuicStream() {} | 85 QuicStream::~QuicStream() { |
| 86 QUIC_LOG_IF(WARNING, !is_deletable_) |
| 87 << "Stream destroyed while not deletable."; |
| 88 } |
| 82 | 89 |
| 83 void QuicStream::SetFromConfig() {} | 90 void QuicStream::SetFromConfig() {} |
| 84 | 91 |
| 85 void QuicStream::OnStreamFrame(const QuicStreamFrame& frame) { | 92 void QuicStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 86 DCHECK_EQ(frame.stream_id, id_); | 93 DCHECK_EQ(frame.stream_id, id_); |
| 87 | 94 |
| 88 DCHECK(!(read_side_closed_ && write_side_closed_)); | 95 DCHECK(!(read_side_closed_ && write_side_closed_)); |
| 89 | 96 |
| 90 if (frame.fin) { | 97 if (frame.fin) { |
| 91 fin_received_ = true; | 98 fin_received_ = true; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 rst_received_ = true; | 143 rst_received_ = true; |
| 137 MaybeIncreaseHighestReceivedOffset(frame.byte_offset); | 144 MaybeIncreaseHighestReceivedOffset(frame.byte_offset); |
| 138 | 145 |
| 139 stream_error_ = frame.error_code; | 146 stream_error_ = frame.error_code; |
| 140 CloseWriteSide(); | 147 CloseWriteSide(); |
| 141 CloseReadSide(); | 148 CloseReadSide(); |
| 142 } | 149 } |
| 143 | 150 |
| 144 void QuicStream::OnConnectionClosed(QuicErrorCode error, | 151 void QuicStream::OnConnectionClosed(QuicErrorCode error, |
| 145 ConnectionCloseSource /*source*/) { | 152 ConnectionCloseSource /*source*/) { |
| 153 if (session()->use_stream_notifier()) { |
| 154 // Connection is closed, consider this stream as deletable. |
| 155 SetIsDeletable(true); |
| 156 } |
| 146 if (read_side_closed_ && write_side_closed_) { | 157 if (read_side_closed_ && write_side_closed_) { |
| 147 return; | 158 return; |
| 148 } | 159 } |
| 149 if (error != QUIC_NO_ERROR) { | 160 if (error != QUIC_NO_ERROR) { |
| 150 stream_error_ = QUIC_STREAM_CONNECTION_ERROR; | 161 stream_error_ = QUIC_STREAM_CONNECTION_ERROR; |
| 151 connection_error_ = error; | 162 connection_error_ = error; |
| 152 } | 163 } |
| 153 | 164 |
| 154 CloseWriteSide(); | 165 CloseWriteSide(); |
| 155 CloseReadSide(); | 166 CloseReadSide(); |
| 156 } | 167 } |
| 157 | 168 |
| 158 void QuicStream::OnFinRead() { | 169 void QuicStream::OnFinRead() { |
| 159 DCHECK(sequencer_.IsClosed()); | 170 DCHECK(sequencer_.IsClosed()); |
| 160 // OnFinRead can be called due to a FIN flag in a headers block, so there may | 171 // OnFinRead can be called due to a FIN flag in a headers block, so there may |
| 161 // have been no OnStreamFrame call with a FIN in the frame. | 172 // have been no OnStreamFrame call with a FIN in the frame. |
| 162 fin_received_ = true; | 173 fin_received_ = true; |
| 163 // If fin_sent_ is true, then CloseWriteSide has already been called, and the | 174 // If fin_sent_ is true, then CloseWriteSide has already been called, and the |
| 164 // stream will be destroyed by CloseReadSide, so don't need to call | 175 // stream will be destroyed by CloseReadSide, so don't need to call |
| 165 // StreamDraining. | 176 // StreamDraining. |
| 166 CloseReadSide(); | 177 CloseReadSide(); |
| 167 } | 178 } |
| 168 | 179 |
| 169 void QuicStream::Reset(QuicRstStreamErrorCode error) { | 180 void QuicStream::Reset(QuicRstStreamErrorCode error) { |
| 170 stream_error_ = error; | 181 stream_error_ = error; |
| 171 // Sending a RstStream results in calling CloseStream. | 182 // Sending a RstStream results in calling CloseStream. |
| 172 session()->SendRstStream(id(), error, stream_bytes_written_); | 183 session()->SendRstStream(id(), error, stream_bytes_written_); |
| 173 rst_sent_ = true; | 184 rst_sent_ = true; |
| 185 if (session()->use_stream_notifier() && error != QUIC_STREAM_NO_ERROR) { |
| 186 // This stream is deletable as data is not going to be retransmitted. |
| 187 SetIsDeletable(true); |
| 188 } |
| 174 } | 189 } |
| 175 | 190 |
| 176 void QuicStream::CloseConnectionWithDetails(QuicErrorCode error, | 191 void QuicStream::CloseConnectionWithDetails(QuicErrorCode error, |
| 177 const string& details) { | 192 const string& details) { |
| 178 session()->connection()->CloseConnection( | 193 session()->connection()->CloseConnection( |
| 179 error, details, ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); | 194 error, details, ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 180 } | 195 } |
| 181 | 196 |
| 182 void QuicStream::WriteOrBufferData( | 197 void QuicStream::WriteOrBufferData( |
| 183 QuicStringPiece data, | 198 QuicStringPiece data, |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 CloseWriteSide(); | 357 CloseWriteSide(); |
| 343 } else if (fin && !consumed_data.fin_consumed) { | 358 } else if (fin && !consumed_data.fin_consumed) { |
| 344 session_->MarkConnectionLevelWriteBlocked(id()); | 359 session_->MarkConnectionLevelWriteBlocked(id()); |
| 345 } | 360 } |
| 346 } else { | 361 } else { |
| 347 session_->MarkConnectionLevelWriteBlocked(id()); | 362 session_->MarkConnectionLevelWriteBlocked(id()); |
| 348 } | 363 } |
| 349 if (consumed_data.bytes_consumed > 0 || consumed_data.fin_consumed) { | 364 if (consumed_data.bytes_consumed > 0 || consumed_data.fin_consumed) { |
| 350 busy_counter_ = 0; | 365 busy_counter_ = 0; |
| 351 } | 366 } |
| 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 } |
| 352 return consumed_data; | 374 return consumed_data; |
| 353 } | 375 } |
| 354 | 376 |
| 355 QuicConsumedData QuicStream::WritevDataInner( | 377 QuicConsumedData QuicStream::WritevDataInner( |
| 356 QuicIOVector iov, | 378 QuicIOVector iov, |
| 357 QuicStreamOffset offset, | 379 QuicStreamOffset offset, |
| 358 bool fin, | 380 bool fin, |
| 359 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { | 381 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { |
| 360 StreamSendingState state = fin ? FIN : NO_FIN; | 382 StreamSendingState state = fin ? FIN : NO_FIN; |
| 361 if (fin && add_random_padding_after_fin_) { | 383 if (fin && add_random_padding_after_fin_) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 void QuicStream::UpdateSendWindowOffset(QuicStreamOffset new_window) { | 507 void QuicStream::UpdateSendWindowOffset(QuicStreamOffset new_window) { |
| 486 if (flow_controller_.UpdateSendWindowOffset(new_window)) { | 508 if (flow_controller_.UpdateSendWindowOffset(new_window)) { |
| 487 OnCanWrite(); | 509 OnCanWrite(); |
| 488 } | 510 } |
| 489 } | 511 } |
| 490 | 512 |
| 491 void QuicStream::AddRandomPaddingAfterFin() { | 513 void QuicStream::AddRandomPaddingAfterFin() { |
| 492 add_random_padding_after_fin_ = true; | 514 add_random_padding_after_fin_ = true; |
| 493 } | 515 } |
| 494 | 516 |
| 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, |
| 525 QuicTime::Delta ack_delay_time) { |
| 526 DCHECK_EQ(frame.stream_id, id()); |
| 527 stream_bytes_acked_ += frame.data_length; |
| 528 if (stream_bytes_acked_ > stream_bytes_written_) { |
| 529 CloseConnectionWithDetails(QUIC_INTERNAL_ERROR, |
| 530 "Unsent stream data is acked"); |
| 531 return; |
| 532 } |
| 533 if (frame.fin) { |
| 534 fin_acked_ = true; |
| 535 } |
| 536 if (ack_listener_ != nullptr) { |
| 537 ack_listener_->OnPacketAcked(frame.data_length, ack_delay_time); |
| 538 } |
| 539 if (stream_bytes_acked_ == stream_bytes_written_ && |
| 540 ((fin_sent_ && fin_acked_) || !fin_sent_)) { |
| 541 // This stream is considered deletable when all sent bytes are acked |
| 542 // (including FIN). |
| 543 SetIsDeletable(true); |
| 544 } |
| 545 } |
| 546 |
| 547 void QuicStream::OnStreamFrameRetransmitted(const QuicStreamFrame& frame) { |
| 548 if (ack_listener_ != nullptr) { |
| 549 ack_listener_->OnPacketRetransmitted(frame.data_length); |
| 550 } |
| 551 } |
| 552 |
| 495 } // namespace net | 553 } // namespace net |
| OLD | NEW |