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/quic_stream_sequencer.h" | 5 #include "net/quic/quic_stream_sequencer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "net/quic/reliable_quic_stream.h" | 11 #include "net/quic/reliable_quic_stream.h" |
12 | 12 |
13 using std::min; | 13 using std::min; |
14 using std::numeric_limits; | 14 using std::numeric_limits; |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 QuicStreamSequencer::QuicStreamSequencer(ReliableQuicStream* quic_stream) | 18 QuicStreamSequencer::QuicStreamSequencer(ReliableQuicStream* quic_stream) |
19 : stream_(quic_stream), | 19 : stream_(quic_stream), |
20 num_bytes_consumed_(0), | 20 num_bytes_consumed_(0), |
21 max_frame_memory_(numeric_limits<size_t>::max()), | 21 max_frame_memory_(numeric_limits<size_t>::max()), |
22 close_offset_(numeric_limits<QuicStreamOffset>::max()), | 22 close_offset_(numeric_limits<QuicStreamOffset>::max()) { |
23 half_close_(true) { | |
24 } | 23 } |
25 | 24 |
26 QuicStreamSequencer::QuicStreamSequencer(size_t max_frame_memory, | 25 QuicStreamSequencer::QuicStreamSequencer(size_t max_frame_memory, |
27 ReliableQuicStream* quic_stream) | 26 ReliableQuicStream* quic_stream) |
28 : stream_(quic_stream), | 27 : stream_(quic_stream), |
29 num_bytes_consumed_(0), | 28 num_bytes_consumed_(0), |
30 max_frame_memory_(max_frame_memory), | 29 max_frame_memory_(max_frame_memory), |
31 close_offset_(numeric_limits<QuicStreamOffset>::max()), | 30 close_offset_(numeric_limits<QuicStreamOffset>::max()) { |
32 half_close_(true) { | |
33 if (max_frame_memory < kMaxPacketSize) { | 31 if (max_frame_memory < kMaxPacketSize) { |
34 LOG(DFATAL) << "Setting max frame memory to " << max_frame_memory | 32 LOG(DFATAL) << "Setting max frame memory to " << max_frame_memory |
35 << ". Some frames will be impossible to handle."; | 33 << ". Some frames will be impossible to handle."; |
36 } | 34 } |
37 } | 35 } |
38 | 36 |
39 QuicStreamSequencer::~QuicStreamSequencer() { | 37 QuicStreamSequencer::~QuicStreamSequencer() { |
40 } | 38 } |
41 | 39 |
42 bool QuicStreamSequencer::WillAcceptStreamFrame( | 40 bool QuicStreamSequencer::WillAcceptStreamFrame( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 data_len -= bytes_consumed; | 97 data_len -= bytes_consumed; |
100 data += bytes_consumed; | 98 data += bytes_consumed; |
101 byte_offset += bytes_consumed; | 99 byte_offset += bytes_consumed; |
102 } | 100 } |
103 } | 101 } |
104 DVLOG(1) << "Buffering packet at offset " << byte_offset; | 102 DVLOG(1) << "Buffering packet at offset " << byte_offset; |
105 frames_.insert(make_pair(byte_offset, string(data, data_len))); | 103 frames_.insert(make_pair(byte_offset, string(data, data_len))); |
106 return true; | 104 return true; |
107 } | 105 } |
108 | 106 |
109 void QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset, | 107 void QuicStreamSequencer::CloseStreamAtOffset(QuicStreamOffset offset) { |
110 bool half_close) { | |
111 const QuicStreamOffset kMaxOffset = numeric_limits<QuicStreamOffset>::max(); | 108 const QuicStreamOffset kMaxOffset = numeric_limits<QuicStreamOffset>::max(); |
112 | 109 |
113 // If we have a scheduled termination or close, any new offset should match | 110 // If we have a scheduled termination or close, any new offset should match |
114 // it. | 111 // it. |
115 if (close_offset_ != kMaxOffset && offset != close_offset_) { | 112 if (close_offset_ != kMaxOffset && offset != close_offset_) { |
116 stream_->Close(QUIC_MULTIPLE_TERMINATION_OFFSETS); | 113 stream_->Close(QUIC_MULTIPLE_TERMINATION_OFFSETS); |
117 return; | 114 return; |
118 } | 115 } |
119 | 116 |
120 close_offset_ = offset; | 117 close_offset_ = offset; |
121 // Full close overrides half close. | |
122 if (half_close == false) { | |
123 half_close_ = false; | |
124 } | |
125 | 118 |
126 MaybeCloseStream(); | 119 MaybeCloseStream(); |
127 } | 120 } |
128 | 121 |
129 bool QuicStreamSequencer::MaybeCloseStream() { | 122 bool QuicStreamSequencer::MaybeCloseStream() { |
130 if (IsHalfClosed()) { | 123 if (IsHalfClosed()) { |
131 DVLOG(1) << "Passing up termination, as we've processed " | 124 DVLOG(1) << "Passing up termination, as we've processed " |
132 << num_bytes_consumed_ << " of " << close_offset_ | 125 << num_bytes_consumed_ << " of " << close_offset_ |
133 << " bytes."; | 126 << " bytes."; |
134 // Technically it's an error if num_bytes_consumed isn't exactly | 127 // Technically it's an error if num_bytes_consumed isn't exactly |
135 // equal, but error handling seems silly at this point. | 128 // equal, but error handling seems silly at this point. |
136 stream_->TerminateFromPeer(half_close_); | 129 stream_->TerminateFromPeer(true); |
137 return true; | 130 return true; |
138 } | 131 } |
139 return false; | 132 return false; |
140 } | 133 } |
141 | 134 |
142 int QuicStreamSequencer::GetReadableRegions(iovec* iov, size_t iov_len) { | 135 int QuicStreamSequencer::GetReadableRegions(iovec* iov, size_t iov_len) { |
143 FrameMap::iterator it = frames_.begin(); | 136 FrameMap::iterator it = frames_.begin(); |
144 size_t index = 0; | 137 size_t index = 0; |
145 QuicStreamOffset offset = num_bytes_consumed_; | 138 QuicStreamOffset offset = num_bytes_consumed_; |
146 while (it != frames_.end() && index < iov_len) { | 139 while (it != frames_.end() && index < iov_len) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 bool QuicStreamSequencer::HasBytesToRead() const { | 216 bool QuicStreamSequencer::HasBytesToRead() const { |
224 FrameMap::const_iterator it = frames_.begin(); | 217 FrameMap::const_iterator it = frames_.begin(); |
225 | 218 |
226 return it != frames_.end() && it->first == num_bytes_consumed_; | 219 return it != frames_.end() && it->first == num_bytes_consumed_; |
227 } | 220 } |
228 | 221 |
229 bool QuicStreamSequencer::IsHalfClosed() const { | 222 bool QuicStreamSequencer::IsHalfClosed() const { |
230 return num_bytes_consumed_ >= close_offset_; | 223 return num_bytes_consumed_ >= close_offset_; |
231 } | 224 } |
232 | 225 |
233 bool QuicStreamSequencer::IsClosed() const { | |
234 return num_bytes_consumed_ >= close_offset_ && half_close_ == false; | |
235 } | |
236 | |
237 bool QuicStreamSequencer::IsDuplicate(const QuicStreamFrame& frame) const { | 226 bool QuicStreamSequencer::IsDuplicate(const QuicStreamFrame& frame) const { |
238 // A frame is duplicate if the frame offset is smaller than our bytes consumed | 227 // A frame is duplicate if the frame offset is smaller than our bytes consumed |
239 // or we have stored the frame in our map. | 228 // or we have stored the frame in our map. |
240 // TODO(pwestin): Is it possible that a new frame contain more data even if | 229 // TODO(pwestin): Is it possible that a new frame contain more data even if |
241 // the offset is the same? | 230 // the offset is the same? |
242 return frame.offset < num_bytes_consumed_ || | 231 return frame.offset < num_bytes_consumed_ || |
243 frames_.find(frame.offset) != frames_.end(); | 232 frames_.find(frame.offset) != frames_.end(); |
244 } | 233 } |
245 | 234 |
246 void QuicStreamSequencer::FlushBufferedFrames() { | 235 void QuicStreamSequencer::FlushBufferedFrames() { |
(...skipping 16 matching lines...) Expand all Loading... |
263 } else { | 252 } else { |
264 string new_data = it->second.substr(bytes_consumed); | 253 string new_data = it->second.substr(bytes_consumed); |
265 frames_.erase(it); | 254 frames_.erase(it); |
266 frames_.insert(make_pair(num_bytes_consumed_, new_data)); | 255 frames_.insert(make_pair(num_bytes_consumed_, new_data)); |
267 return; | 256 return; |
268 } | 257 } |
269 } | 258 } |
270 } | 259 } |
271 | 260 |
272 } // namespace net | 261 } // namespace net |
OLD | NEW |