[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/quic/reliable_quic_stream.h" |
| 6 | |
[email protected] | 497bfb2 | 2014-01-08 01:28:03 | [diff] [blame^] | 7 | #include "base/logging.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 8 | #include "net/quic/quic_session.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 9 | #include "net/quic/quic_spdy_decompressor.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 10 | #include "net/spdy/write_blocked_list.h" |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 11 | |
| 12 | using base::StringPiece; |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 13 | using std::min; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 17 | #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 18 | |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 21 | struct 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] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 29 | ReliableQuicStream::ReliableQuicStream(QuicStreamId id, |
| 30 | QuicSession* session) |
| 31 | : sequencer_(this), |
| 32 | id_(id), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 33 | session_(session), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 34 | stream_bytes_read_(0), |
| 35 | stream_bytes_written_(0), |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 36 | stream_error_(QUIC_STREAM_NO_ERROR), |
| 37 | connection_error_(QUIC_NO_ERROR), |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 38 | read_side_closed_(false), |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 39 | write_side_closed_(false), |
| 40 | fin_buffered_(false), |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 41 | fin_sent_(false), |
| 42 | is_server_(session_->is_server()) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ReliableQuicStream::~ReliableQuicStream() { |
| 46 | } |
| 47 | |
| 48 | bool ReliableQuicStream::WillAcceptStreamFrame( |
| 49 | const QuicStreamFrame& frame) const { |
| 50 | if (read_side_closed_) { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 51 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 52 | } |
| 53 | if (frame.stream_id != id_) { |
| 54 | LOG(ERROR) << "Error!"; |
| 55 | return false; |
| 56 | } |
| 57 | return sequencer_.WillAcceptStreamFrame(frame); |
| 58 | } |
| 59 | |
| 60 | bool ReliableQuicStream::OnStreamFrame(const QuicStreamFrame& frame) { |
| 61 | DCHECK_EQ(frame.stream_id, id_); |
| 62 | if (read_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 63 | DVLOG(1) << ENDPOINT << "Ignoring frame " << frame.stream_id; |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 64 | // We don't want to be reading: blackhole the data. |
| 65 | return true; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 66 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 67 | // Note: This count include duplicate data received. |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 68 | stream_bytes_read_ += frame.data.TotalBufferSize(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 69 | |
| 70 | bool accepted = sequencer_.OnStreamFrame(frame); |
| 71 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 72 | return accepted; |
| 73 | } |
| 74 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 75 | void ReliableQuicStream::OnStreamReset(QuicRstStreamErrorCode error) { |
| 76 | stream_error_ = error; |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 77 | CloseWriteSide(); |
| 78 | CloseReadSide(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 81 | void ReliableQuicStream::OnConnectionClosed(QuicErrorCode error, |
| 82 | bool from_peer) { |
[email protected] | 4e49b6a | 2013-06-18 16:39:28 | [diff] [blame] | 83 | if (read_side_closed_ && write_side_closed_) { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 84 | return; |
| 85 | } |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 86 | if (error != QUIC_NO_ERROR) { |
| 87 | stream_error_ = QUIC_STREAM_CONNECTION_ERROR; |
| 88 | connection_error_ = error; |
| 89 | } |
| 90 | |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 91 | CloseWriteSide(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 92 | CloseReadSide(); |
| 93 | } |
| 94 | |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 95 | void ReliableQuicStream::OnFinRead() { |
| 96 | DCHECK(sequencer_.IsClosed()); |
| 97 | CloseReadSide(); |
| 98 | } |
| 99 | |
| 100 | void ReliableQuicStream::Reset(QuicRstStreamErrorCode error) { |
| 101 | DCHECK_NE(QUIC_STREAM_NO_ERROR, error); |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 102 | stream_error_ = error; |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 103 | // Sending a RstStream results in calling CloseStream. |
| 104 | session()->SendRstStream(id(), error); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 2cfc6bb8 | 2013-10-27 03:40:44 | [diff] [blame] | 107 | void ReliableQuicStream::CloseConnection(QuicErrorCode error) { |
| 108 | session()->connection()->SendConnectionClose(error); |
| 109 | } |
| 110 | |
| 111 | void ReliableQuicStream::CloseConnectionWithDetails(QuicErrorCode error, |
| 112 | const string& details) { |
| 113 | session()->connection()->SendConnectionCloseWithDetails(error, details); |
| 114 | } |
| 115 | |
[email protected] | c05a6d22 | 2013-12-16 19:42:03 | [diff] [blame] | 116 | QuicVersion ReliableQuicStream::version() { |
| 117 | return session()->connection()->version(); |
| 118 | } |
| 119 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 120 | void ReliableQuicStream::WriteOrBufferData(StringPiece data, bool fin) { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 121 | DCHECK(data.size() > 0 || fin); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 122 | DCHECK(!fin_buffered_); |
| 123 | |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 124 | QuicConsumedData consumed_data(0, false); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 125 | fin_buffered_ = fin; |
| 126 | |
| 127 | if (queued_data_.empty()) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 128 | struct iovec iov(MakeIovec(data)); |
| 129 | consumed_data = WritevData(&iov, 1, fin, NULL); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 130 | DCHECK_LE(consumed_data.bytes_consumed, data.length()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 131 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 132 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 133 | // If there's unconsumed data or an unconsumed fin, queue it. |
| 134 | if (consumed_data.bytes_consumed < data.length() || |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 135 | (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] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 139 | } |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void ReliableQuicStream::OnCanWrite() { |
| 143 | bool fin = false; |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 144 | while (!queued_data_.empty()) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 145 | const string& data = queued_data_.front(); |
| 146 | if (queued_data_.size() == 1 && fin_buffered_) { |
| 147 | fin = true; |
| 148 | } |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 149 | struct iovec iov(MakeIovec(data)); |
| 150 | QuicConsumedData consumed_data = WritevData(&iov, 1, fin, NULL); |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 151 | if (consumed_data.bytes_consumed == data.size() && |
| 152 | fin == consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 153 | queued_data_.pop_front(); |
| 154 | } else { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 155 | queued_data_.front().erase(0, consumed_data.bytes_consumed); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 156 | break; |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 161 | QuicConsumedData ReliableQuicStream::WritevData( |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 162 | const struct iovec* iov, |
| 163 | int iov_count, |
| 164 | bool fin, |
| 165 | QuicAckNotifier::DelegateInterface* ack_notifier_delegate) { |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 166 | if (write_side_closed_) { |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 167 | DLOG(ERROR) << ENDPOINT << "Attempt to write when the write side is closed"; |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 168 | return QuicConsumedData(0, false); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 171 | size_t write_length = 0u; |
[email protected] | 4d1789c3 | 2013-09-18 23:54:36 | [diff] [blame] | 172 | for (int i = 0; i < iov_count; ++i) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 173 | write_length += iov[i].iov_len; |
| 174 | } |
[email protected] | 0ac0c5b | 2013-11-20 05:55:58 | [diff] [blame] | 175 | QuicConsumedData consumed_data = session()->WritevData( |
| 176 | id(), iov, iov_count, stream_bytes_written_, fin, ack_notifier_delegate); |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 177 | stream_bytes_written_ += consumed_data.bytes_consumed; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 178 | if (consumed_data.bytes_consumed == write_length) { |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 179 | if (fin && consumed_data.fin_consumed) { |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 180 | fin_sent_ = true; |
| 181 | CloseWriteSide(); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 182 | } else if (fin && !consumed_data.fin_consumed) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 183 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | a5d4eee2 | 2012-12-13 09:09:01 | [diff] [blame] | 184 | } |
| 185 | } else { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 186 | session_->MarkWriteBlocked(id(), EffectivePriority()); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 187 | } |
[email protected] | cff7b7b5 | 2013-01-11 08:49:07 | [diff] [blame] | 188 | return consumed_data; |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void ReliableQuicStream::CloseReadSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 192 | if (read_side_closed_) { |
| 193 | return; |
| 194 | } |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 195 | DVLOG(1) << ENDPOINT << "Done reading from stream " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 196 | |
| 197 | read_side_closed_ = true; |
| 198 | if (write_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 199 | DVLOG(1) << ENDPOINT << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 200 | session_->CloseStream(id()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void ReliableQuicStream::CloseWriteSide() { |
[email protected] | 044ac2b | 2012-11-13 21:41:06 | [diff] [blame] | 205 | if (write_side_closed_) { |
| 206 | return; |
| 207 | } |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 208 | DVLOG(1) << ENDPOINT << "Done writing to stream " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 209 | |
| 210 | write_side_closed_ = true; |
| 211 | if (read_side_closed_) { |
[email protected] | 3e5fed1 | 2013-11-22 22:21:41 | [diff] [blame] | 212 | DVLOG(1) << ENDPOINT << "Closing stream: " << id(); |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 213 | session_->CloseStream(id()); |
| 214 | } |
| 215 | } |
| 216 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 217 | bool ReliableQuicStream::HasBufferedData() { |
| 218 | return !queued_data_.empty(); |
| 219 | } |
| 220 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 221 | void ReliableQuicStream::OnClose() { |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 222 | CloseReadSide(); |
| 223 | CloseWriteSide(); |
[email protected] | 8edeb8d | 2013-08-28 06:11:43 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 9c0b135 | 2012-11-04 00:03:27 | [diff] [blame] | 226 | } // namespace net |