| OLD | NEW |
| (Empty) |
| 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/quic_stream_sequencer.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "net/quic/reliable_quic_stream.h" | |
| 12 | |
| 13 using std::min; | |
| 14 using std::numeric_limits; | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 QuicStreamSequencer::QuicStreamSequencer(ReliableQuicStream* quic_stream) | |
| 19 : stream_(quic_stream), | |
| 20 num_bytes_consumed_(0), | |
| 21 max_frame_memory_(numeric_limits<size_t>::max()), | |
| 22 close_offset_(numeric_limits<QuicStreamOffset>::max()), | |
| 23 half_close_(true) { | |
| 24 } | |
| 25 | |
| 26 QuicStreamSequencer::QuicStreamSequencer(size_t max_frame_memory, | |
| 27 ReliableQuicStream* quic_stream) | |
| 28 : stream_(quic_stream), | |
| 29 num_bytes_consumed_(0), | |
| 30 max_frame_memory_(max_frame_memory), | |
| 31 close_offset_(numeric_limits<QuicStreamOffset>::max()), | |
| 32 half_close_(true) { | |
| 33 if (max_frame_memory < kMaxPacketSize) { | |
| 34 LOG(DFATAL) << "Setting max frame memory to " << max_frame_memory | |
| 35 << ". Some frames will be impossible to handle."; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 QuicStreamSequencer::~QuicStreamSequencer() { | |
| 40 } | |
| 41 | |
| 42 bool QuicStreamSequencer::WillAcceptStreamFrame( | |
| 43 const QuicStreamFrame& frame) const { | |
| 44 QuicStreamOffset byte_offset = frame.offset; | |
| 45 size_t data_len = frame.data.size(); | |
| 46 DCHECK_LE(data_len, max_frame_memory_); | |
| 47 | |
| 48 if (byte_offset < num_bytes_consumed_ || | |
| 49 frames_.find(byte_offset) != frames_.end()) { | |
| 50 return false; | |
| 51 } | |
| 52 if (data_len > max_frame_memory_) { | |
| 53 // We're never going to buffer this frame and we can't pass it up the | |
| 54 // stream might only consume part of it and we'd need a partial ack. | |
| 55 // | |
| 56 // Ideally this should never happen, as we check that | |
| 57 // max_frame_memory_ > kMaxPacketSize and lower levels should reject | |
| 58 // frames larger than that. | |
| 59 return false; | |
| 60 } | |
| 61 if (byte_offset + data_len - num_bytes_consumed_ > max_frame_memory_) { | |
| 62 // We can buffer this but not right now. Toss it. | |
| 63 // It might be worth trying an experiment where we try best-effort buffering | |
| 64 return false; | |
| 65 } | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 bool QuicStreamSequencer::OnStreamFrame(const QuicStreamFrame& frame) { | |
| 70 QuicStreamOffset byte_offset = frame.offset; | |
| 71 const char* data = frame.data.data(); | |
| 72 size_t data_len = frame.data.size(); | |
| 73 | |
| 74 if (!WillAcceptStreamFrame(frame)) { | |
| 75 // This should not happen, as WillAcceptFrame should be called before | |
| 76 // OnStreamFrame. Error handling should be done by the caller. | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 if (byte_offset == num_bytes_consumed_) { | |
| 81 DVLOG(1) << "Processing byte offset " << byte_offset; | |
| 82 size_t bytes_consumed = stream_->ProcessData(data, data_len); | |
| 83 num_bytes_consumed_ += bytes_consumed; | |
| 84 | |
| 85 if (MaybeCloseStream()) { | |
| 86 return true; | |
| 87 } | |
| 88 if (bytes_consumed > data_len) { | |
| 89 stream_->Close(QUIC_SERVER_ERROR_PROCESSING_STREAM); | |
| 90 return false; | |
| 91 } else if (bytes_consumed == data_len) { | |
| 92 FlushBufferedFrames(); | |
| 93 return true; // it's safe to ack this frame. | |
| 94 } else { | |
| 95 // Set ourselves up to buffer what's left | |
| 96 data_len -= bytes_consumed; | |
| 97 data += bytes_consumed; | |
| 98 byte_offset += bytes_consumed; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 DVLOG(1) << "Buffering packet at offset " << byte_offset; | |
| 103 frames_.insert(make_pair(byte_offset, string(data, data_len))); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset, | |
| 108 bool half_close) { | |
| 109 const QuicStreamOffset kMaxOffset = numeric_limits<QuicStreamOffset>::max(); | |
| 110 | |
| 111 // If we have a scheduled termination or close, any new offset should match | |
| 112 // it. | |
| 113 if (close_offset_ != kMaxOffset && offset != close_offset_) { | |
| 114 stream_->Close(QUIC_MULTIPLE_TERMINATION_OFFSETS); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 close_offset_ = offset; | |
| 119 // Full close overrides half close. | |
| 120 if (half_close == false) { | |
| 121 half_close_ = false; | |
| 122 } | |
| 123 | |
| 124 MaybeCloseStream(); | |
| 125 } | |
| 126 | |
| 127 bool QuicStreamSequencer::MaybeCloseStream() { | |
| 128 if (IsHalfClosed()) { | |
| 129 DVLOG(1) << "Passing up termination, as we've processed " | |
| 130 << num_bytes_consumed_ << " of " << close_offset_ | |
| 131 << " bytes."; | |
| 132 // Technically it's an error if num_bytes_consumed isn't exactly | |
| 133 // equal, but error handling seems silly at this point. | |
| 134 stream_->TerminateFromPeer(half_close_); | |
| 135 return true; | |
| 136 } | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 void QuicStreamSequencer::AdvanceReadablePtr(size_t data_read) { | |
| 141 FrameMap::iterator it = frames_.begin(); | |
| 142 | |
| 143 while (data_read) { | |
| 144 if (it->first != num_bytes_consumed_ || it == frames_.end()) { | |
| 145 stream_->Close(QUIC_SERVER_ERROR_PROCESSING_STREAM); // Programming error | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 if (data_read >= it->second.size()) { | |
| 150 data_read -= it->second.size(); | |
| 151 num_bytes_consumed_ += it->second.size(); | |
| 152 frames_.erase(it); | |
| 153 it = frames_.begin(); | |
| 154 } else { | |
| 155 frames_.insert(make_pair(it->first + data_read, | |
| 156 it->second.substr(data_read))); | |
| 157 frames_.erase(frames_.begin()); | |
| 158 num_bytes_consumed_ += data_read; | |
| 159 data_read = 0; | |
| 160 } | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 bool QuicStreamSequencer::HasBytesToRead() { | |
| 165 FrameMap::iterator it = frames_.begin(); | |
| 166 | |
| 167 return it != frames_.end() && it->first == num_bytes_consumed_; | |
| 168 } | |
| 169 | |
| 170 bool QuicStreamSequencer::IsHalfClosed() { | |
| 171 return num_bytes_consumed_ >= close_offset_; | |
| 172 } | |
| 173 | |
| 174 bool QuicStreamSequencer::IsClosed() { | |
| 175 return num_bytes_consumed_ >= close_offset_ && half_close_ == false; | |
| 176 } | |
| 177 | |
| 178 void QuicStreamSequencer::FlushBufferedFrames() { | |
| 179 FrameMap::iterator it = frames_.find(num_bytes_consumed_); | |
| 180 while (it != frames_.end()) { | |
| 181 DVLOG(1) << "Flushing buffered packet at offset " << it->first; | |
| 182 string* data = &it->second; | |
| 183 size_t bytes_consumed = stream_->ProcessData(data->c_str(), data->size()); | |
| 184 num_bytes_consumed_ += bytes_consumed; | |
| 185 if (MaybeCloseStream()) { | |
| 186 return; | |
| 187 } | |
| 188 if (bytes_consumed > data->size()) { | |
| 189 stream_->Close(QUIC_SERVER_ERROR_PROCESSING_STREAM); // Programming error | |
| 190 return; | |
| 191 } else if (bytes_consumed == data->size()) { | |
| 192 frames_.erase(it); | |
| 193 it = frames_.find(num_bytes_consumed_); | |
| 194 } else { | |
| 195 string new_data = it->second.substr(bytes_consumed); | |
| 196 frames_.erase(it); | |
| 197 frames_.insert(make_pair(num_bytes_consumed_, new_data)); | |
| 198 return; | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 } // namespace net | |
| OLD | NEW |