blob: a04dc7574fd59e3130b2c33f82a54feb9e57fdd5 [file] [log] [blame]
[email protected]9c0b1352012-11-04 00:03:271// 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.
4
5#include "net/quic/reliable_quic_stream.h"
6
[email protected]497bfb22014-01-08 01:28:037#include "base/logging.h"
[email protected]9c0b1352012-11-04 00:03:278#include "net/quic/quic_session.h"
[email protected]c244c5a12013-05-07 20:55:049#include "net/quic/quic_spdy_decompressor.h"
[email protected]24e5bc52013-09-18 15:36:5810#include "net/spdy/write_blocked_list.h"
[email protected]9c0b1352012-11-04 00:03:2711
12using base::StringPiece;
[email protected]c244c5a12013-05-07 20:55:0413using std::min;
[email protected]9c0b1352012-11-04 00:03:2714
15namespace net {
16
[email protected]457d6952013-12-13 09:24:5817#define ENDPOINT (is_server_ ? "Server: " : " Client: ")
18
[email protected]8edeb8d2013-08-28 06:11:4319namespace {
20
[email protected]457d6952013-12-13 09:24:5821struct iovec MakeIovec(StringPiece data) {
22 struct iovec iov = {const_cast<char*>(data.data()),
23 static_cast<size_t>(data.size())};
24 return iov;
[email protected]8edeb8d2013-08-28 06:11:4325}
26
27} // namespace
28
[email protected]9c0b1352012-11-04 00:03:2729ReliableQuicStream::ReliableQuicStream(QuicStreamId id,
30 QuicSession* session)
31 : sequencer_(this),
32 id_(id),
[email protected]9c0b1352012-11-04 00:03:2733 session_(session),
[email protected]63534512012-12-23 18:49:0034 stream_bytes_read_(0),
35 stream_bytes_written_(0),
[email protected]74bda142013-03-31 02:49:1136 stream_error_(QUIC_STREAM_NO_ERROR),
37 connection_error_(QUIC_NO_ERROR),
[email protected]9c0b1352012-11-04 00:03:2738 read_side_closed_(false),
[email protected]a5d4eee22012-12-13 09:09:0139 write_side_closed_(false),
40 fin_buffered_(false),
[email protected]b007e632013-10-28 08:39:2541 fin_sent_(false),
42 is_server_(session_->is_server()) {
[email protected]9c0b1352012-11-04 00:03:2743}
44
45ReliableQuicStream::~ReliableQuicStream() {
46}
47
48bool ReliableQuicStream::WillAcceptStreamFrame(
49 const QuicStreamFrame& frame) const {
50 if (read_side_closed_) {
[email protected]044ac2b2012-11-13 21:41:0651 return true;
[email protected]9c0b1352012-11-04 00:03:2752 }
53 if (frame.stream_id != id_) {
54 LOG(ERROR) << "Error!";
55 return false;
56 }
57 return sequencer_.WillAcceptStreamFrame(frame);
58}
59
60bool ReliableQuicStream::OnStreamFrame(const QuicStreamFrame& frame) {
61 DCHECK_EQ(frame.stream_id, id_);
62 if (read_side_closed_) {
[email protected]3e5fed12013-11-22 22:21:4163 DVLOG(1) << ENDPOINT << "Ignoring frame " << frame.stream_id;
[email protected]044ac2b2012-11-13 21:41:0664 // We don't want to be reading: blackhole the data.
65 return true;
[email protected]9c0b1352012-11-04 00:03:2766 }
[email protected]9db443912013-02-25 05:27:0367 // Note: This count include duplicate data received.
[email protected]5dafdb62013-11-14 01:24:2668 stream_bytes_read_ += frame.data.TotalBufferSize();
[email protected]9c0b1352012-11-04 00:03:2769
70 bool accepted = sequencer_.OnStreamFrame(frame);
71
[email protected]9c0b1352012-11-04 00:03:2772 return accepted;
73}
74
[email protected]74bda142013-03-31 02:49:1175void ReliableQuicStream::OnStreamReset(QuicRstStreamErrorCode error) {
76 stream_error_ = error;
[email protected]2adef90e2013-12-02 20:26:5777 CloseWriteSide();
78 CloseReadSide();
[email protected]9c0b1352012-11-04 00:03:2779}
80
[email protected]2cfc6bb82013-10-27 03:40:4481void ReliableQuicStream::OnConnectionClosed(QuicErrorCode error,
82 bool from_peer) {
[email protected]4e49b6a2013-06-18 16:39:2883 if (read_side_closed_ && write_side_closed_) {
[email protected]a57e0272013-04-26 07:31:4784 return;
85 }
[email protected]74bda142013-03-31 02:49:1186 if (error != QUIC_NO_ERROR) {
87 stream_error_ = QUIC_STREAM_CONNECTION_ERROR;
88 connection_error_ = error;
89 }
90
[email protected]2adef90e2013-12-02 20:26:5791 CloseWriteSide();
[email protected]9c0b1352012-11-04 00:03:2792 CloseReadSide();
93}
94
[email protected]2adef90e2013-12-02 20:26:5795void ReliableQuicStream::OnFinRead() {
96 DCHECK(sequencer_.IsClosed());
97 CloseReadSide();
98}
99
100void ReliableQuicStream::Reset(QuicRstStreamErrorCode error) {
101 DCHECK_NE(QUIC_STREAM_NO_ERROR, error);
[email protected]74bda142013-03-31 02:49:11102 stream_error_ = error;
[email protected]2adef90e2013-12-02 20:26:57103 // Sending a RstStream results in calling CloseStream.
104 session()->SendRstStream(id(), error);
[email protected]9c0b1352012-11-04 00:03:27105}
106
[email protected]2cfc6bb82013-10-27 03:40:44107void ReliableQuicStream::CloseConnection(QuicErrorCode error) {
108 session()->connection()->SendConnectionClose(error);
109}
110
111void ReliableQuicStream::CloseConnectionWithDetails(QuicErrorCode error,
112 const string& details) {
113 session()->connection()->SendConnectionCloseWithDetails(error, details);
114}
115
[email protected]c05a6d222013-12-16 19:42:03116QuicVersion ReliableQuicStream::version() {
117 return session()->connection()->version();
118}
119
[email protected]457d6952013-12-13 09:24:58120void ReliableQuicStream::WriteOrBufferData(StringPiece data, bool fin) {
[email protected]25c31dc2013-06-05 17:56:04121 DCHECK(data.size() > 0 || fin);
[email protected]a5d4eee22012-12-13 09:09:01122 DCHECK(!fin_buffered_);
123
[email protected]cff7b7b52013-01-11 08:49:07124 QuicConsumedData consumed_data(0, false);
[email protected]a5d4eee22012-12-13 09:09:01125 fin_buffered_ = fin;
126
127 if (queued_data_.empty()) {
[email protected]457d6952013-12-13 09:24:58128 struct iovec iov(MakeIovec(data));
129 consumed_data = WritevData(&iov, 1, fin, NULL);
[email protected]c995c572013-01-18 05:43:20130 DCHECK_LE(consumed_data.bytes_consumed, data.length());
[email protected]a5d4eee22012-12-13 09:09:01131 }
[email protected]cff7b7b52013-01-11 08:49:07132
[email protected]c995c572013-01-18 05:43:20133 // If there's unconsumed data or an unconsumed fin, queue it.
134 if (consumed_data.bytes_consumed < data.length() ||
[email protected]cff7b7b52013-01-11 08:49:07135 (fin && !consumed_data.fin_consumed)) {
136 queued_data_.push_back(
137 string(data.data() + consumed_data.bytes_consumed,
138 data.length() - consumed_data.bytes_consumed));
[email protected]a5d4eee22012-12-13 09:09:01139 }
[email protected]a5d4eee22012-12-13 09:09:01140}
141
142void ReliableQuicStream::OnCanWrite() {
143 bool fin = false;
[email protected]610a7e942012-12-18 00:21:39144 while (!queued_data_.empty()) {
[email protected]a5d4eee22012-12-13 09:09:01145 const string& data = queued_data_.front();
146 if (queued_data_.size() == 1 && fin_buffered_) {
147 fin = true;
148 }
[email protected]457d6952013-12-13 09:24:58149 struct iovec iov(MakeIovec(data));
150 QuicConsumedData consumed_data = WritevData(&iov, 1, fin, NULL);
[email protected]cff7b7b52013-01-11 08:49:07151 if (consumed_data.bytes_consumed == data.size() &&
152 fin == consumed_data.fin_consumed) {
[email protected]a5d4eee22012-12-13 09:09:01153 queued_data_.pop_front();
154 } else {
[email protected]cff7b7b52013-01-11 08:49:07155 queued_data_.front().erase(0, consumed_data.bytes_consumed);
[email protected]610a7e942012-12-18 00:21:39156 break;
[email protected]a5d4eee22012-12-13 09:09:01157 }
158 }
159}
160
[email protected]457d6952013-12-13 09:24:58161QuicConsumedData ReliableQuicStream::WritevData(
[email protected]0ac0c5b2013-11-20 05:55:58162 const struct iovec* iov,
163 int iov_count,
164 bool fin,
165 QuicAckNotifier::DelegateInterface* ack_notifier_delegate) {
[email protected]9c0b1352012-11-04 00:03:27166 if (write_side_closed_) {
[email protected]b007e632013-10-28 08:39:25167 DLOG(ERROR) << ENDPOINT << "Attempt to write when the write side is closed";
[email protected]cff7b7b52013-01-11 08:49:07168 return QuicConsumedData(0, false);
[email protected]9c0b1352012-11-04 00:03:27169 }
170
[email protected]24e5bc52013-09-18 15:36:58171 size_t write_length = 0u;
[email protected]4d1789c32013-09-18 23:54:36172 for (int i = 0; i < iov_count; ++i) {
[email protected]24e5bc52013-09-18 15:36:58173 write_length += iov[i].iov_len;
174 }
[email protected]0ac0c5b2013-11-20 05:55:58175 QuicConsumedData consumed_data = session()->WritevData(
176 id(), iov, iov_count, stream_bytes_written_, fin, ack_notifier_delegate);
[email protected]cff7b7b52013-01-11 08:49:07177 stream_bytes_written_ += consumed_data.bytes_consumed;
[email protected]24e5bc52013-09-18 15:36:58178 if (consumed_data.bytes_consumed == write_length) {
[email protected]cff7b7b52013-01-11 08:49:07179 if (fin && consumed_data.fin_consumed) {
[email protected]a5d4eee22012-12-13 09:09:01180 fin_sent_ = true;
181 CloseWriteSide();
[email protected]25c31dc2013-06-05 17:56:04182 } else if (fin && !consumed_data.fin_consumed) {
[email protected]24e5bc52013-09-18 15:36:58183 session_->MarkWriteBlocked(id(), EffectivePriority());
[email protected]a5d4eee22012-12-13 09:09:01184 }
185 } else {
[email protected]24e5bc52013-09-18 15:36:58186 session_->MarkWriteBlocked(id(), EffectivePriority());
[email protected]9c0b1352012-11-04 00:03:27187 }
[email protected]cff7b7b52013-01-11 08:49:07188 return consumed_data;
[email protected]9c0b1352012-11-04 00:03:27189}
190
191void ReliableQuicStream::CloseReadSide() {
[email protected]044ac2b2012-11-13 21:41:06192 if (read_side_closed_) {
193 return;
194 }
[email protected]3e5fed12013-11-22 22:21:41195 DVLOG(1) << ENDPOINT << "Done reading from stream " << id();
[email protected]9c0b1352012-11-04 00:03:27196
197 read_side_closed_ = true;
198 if (write_side_closed_) {
[email protected]3e5fed12013-11-22 22:21:41199 DVLOG(1) << ENDPOINT << "Closing stream: " << id();
[email protected]9c0b1352012-11-04 00:03:27200 session_->CloseStream(id());
201 }
202}
203
204void ReliableQuicStream::CloseWriteSide() {
[email protected]044ac2b2012-11-13 21:41:06205 if (write_side_closed_) {
206 return;
207 }
[email protected]3e5fed12013-11-22 22:21:41208 DVLOG(1) << ENDPOINT << "Done writing to stream " << id();
[email protected]9c0b1352012-11-04 00:03:27209
210 write_side_closed_ = true;
211 if (read_side_closed_) {
[email protected]3e5fed12013-11-22 22:21:41212 DVLOG(1) << ENDPOINT << "Closing stream: " << id();
[email protected]9c0b1352012-11-04 00:03:27213 session_->CloseStream(id());
214 }
215}
216
[email protected]6adeb922013-09-01 22:43:25217bool ReliableQuicStream::HasBufferedData() {
218 return !queued_data_.empty();
219}
220
[email protected]63534512012-12-23 18:49:00221void ReliableQuicStream::OnClose() {
[email protected]a57e0272013-04-26 07:31:47222 CloseReadSide();
223 CloseWriteSide();
[email protected]8edeb8d2013-08-28 06:11:43224}
225
[email protected]9c0b1352012-11-04 00:03:27226} // namespace net