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