| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/core/quic_stream_sequencer_buffer.h" | 5 #include "net/quic/core/quic_stream_sequencer_buffer.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, | 44 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, |
| 45 QuicTime timestamp) | 45 QuicTime timestamp) |
| 46 : length(length), timestamp(timestamp) {} | 46 : length(length), timestamp(timestamp) {} |
| 47 | 47 |
| 48 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) | 48 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) |
| 49 : max_buffer_capacity_bytes_(max_capacity_bytes), | 49 : max_buffer_capacity_bytes_(max_capacity_bytes), |
| 50 blocks_count_( | 50 blocks_count_( |
| 51 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), | 51 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), |
| 52 total_bytes_read_(0), | 52 total_bytes_read_(0), |
| 53 blocks_(nullptr) { | 53 blocks_(nullptr), |
| 54 destruction_indicator_(123456) { |
| 55 CHECK_GT(blocks_count_, 1u) |
| 56 << "blocks_count_ = " << blocks_count_ |
| 57 << ", max_buffer_capacity_bytes_ = " << max_buffer_capacity_bytes_; |
| 54 Clear(); | 58 Clear(); |
| 55 } | 59 } |
| 56 | 60 |
| 57 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { | 61 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { |
| 58 Clear(); | 62 Clear(); |
| 63 destruction_indicator_ = 654321; |
| 59 } | 64 } |
| 60 | 65 |
| 61 void QuicStreamSequencerBuffer::Clear() { | 66 void QuicStreamSequencerBuffer::Clear() { |
| 62 if (blocks_ != nullptr) { | 67 if (blocks_ != nullptr) { |
| 63 for (size_t i = 0; i < blocks_count_; ++i) { | 68 for (size_t i = 0; i < blocks_count_; ++i) { |
| 64 if (blocks_[i] != nullptr) { | 69 if (blocks_[i] != nullptr) { |
| 65 RetireBlock(i); | 70 RetireBlock(i); |
| 66 } | 71 } |
| 67 } | 72 } |
| 68 } | 73 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 85 DVLOG(1) << "Retired block with index: " << idx; | 90 DVLOG(1) << "Retired block with index: " << idx; |
| 86 return true; | 91 return true; |
| 87 } | 92 } |
| 88 | 93 |
| 89 QuicErrorCode QuicStreamSequencerBuffer::OnStreamData( | 94 QuicErrorCode QuicStreamSequencerBuffer::OnStreamData( |
| 90 QuicStreamOffset starting_offset, | 95 QuicStreamOffset starting_offset, |
| 91 base::StringPiece data, | 96 base::StringPiece data, |
| 92 QuicTime timestamp, | 97 QuicTime timestamp, |
| 93 size_t* const bytes_buffered, | 98 size_t* const bytes_buffered, |
| 94 std::string* error_details) { | 99 std::string* error_details) { |
| 100 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; |
| 95 *bytes_buffered = 0; | 101 *bytes_buffered = 0; |
| 96 QuicStreamOffset offset = starting_offset; | 102 QuicStreamOffset offset = starting_offset; |
| 97 size_t size = data.size(); | 103 size_t size = data.size(); |
| 98 if (size == 0) { | 104 if (size == 0) { |
| 99 *error_details = "Received empty stream frame without FIN."; | 105 *error_details = "Received empty stream frame without FIN."; |
| 100 return QUIC_EMPTY_STREAM_FRAME_NO_FIN; | 106 return QUIC_EMPTY_STREAM_FRAME_NO_FIN; |
| 101 } | 107 } |
| 102 | 108 |
| 103 // Find the first gap not ending before |offset|. This gap maybe the gap to | 109 // Find the first gap not ending before |offset|. This gap maybe the gap to |
| 104 // fill if the arriving frame doesn't overlaps with previous ones. | 110 // fill if the arriving frame doesn't overlaps with previous ones. |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 start_offset + bytes_written) { | 272 start_offset + bytes_written) { |
| 267 // This gap has been filled with new data. So it's no longer a gap. | 273 // This gap has been filled with new data. So it's no longer a gap. |
| 268 gaps_.erase(gap_with_new_data_written); | 274 gaps_.erase(gap_with_new_data_written); |
| 269 } | 275 } |
| 270 } | 276 } |
| 271 | 277 |
| 272 QuicErrorCode QuicStreamSequencerBuffer::Readv(const iovec* dest_iov, | 278 QuicErrorCode QuicStreamSequencerBuffer::Readv(const iovec* dest_iov, |
| 273 size_t dest_count, | 279 size_t dest_count, |
| 274 size_t* bytes_read, | 280 size_t* bytes_read, |
| 275 string* error_details) { | 281 string* error_details) { |
| 282 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; |
| 283 |
| 276 *bytes_read = 0; | 284 *bytes_read = 0; |
| 277 for (size_t i = 0; i < dest_count && ReadableBytes() > 0; ++i) { | 285 for (size_t i = 0; i < dest_count && ReadableBytes() > 0; ++i) { |
| 278 char* dest = reinterpret_cast<char*>(dest_iov[i].iov_base); | 286 char* dest = reinterpret_cast<char*>(dest_iov[i].iov_base); |
| 287 CHECK_NE(dest, nullptr); |
| 279 size_t dest_remaining = dest_iov[i].iov_len; | 288 size_t dest_remaining = dest_iov[i].iov_len; |
| 280 while (dest_remaining > 0 && ReadableBytes() > 0) { | 289 while (dest_remaining > 0 && ReadableBytes() > 0) { |
| 281 size_t block_idx = NextBlockToRead(); | 290 size_t block_idx = NextBlockToRead(); |
| 282 size_t start_offset_in_block = ReadOffset(); | 291 size_t start_offset_in_block = ReadOffset(); |
| 283 size_t block_capacity = GetBlockCapacity(block_idx); | 292 size_t block_capacity = GetBlockCapacity(block_idx); |
| 284 size_t bytes_available_in_block = | 293 size_t bytes_available_in_block = |
| 285 min<size_t>(ReadableBytes(), block_capacity - start_offset_in_block); | 294 min<size_t>(ReadableBytes(), block_capacity - start_offset_in_block); |
| 286 size_t bytes_to_copy = | 295 size_t bytes_to_copy = |
| 287 min<size_t>(bytes_available_in_block, dest_remaining); | 296 min<size_t>(bytes_available_in_block, dest_remaining); |
| 288 DCHECK_GT(bytes_to_copy, 0UL); | 297 DCHECK_GT(bytes_to_copy, 0UL); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 331 } |
| 323 | 332 |
| 324 if (*bytes_read > 0) { | 333 if (*bytes_read > 0) { |
| 325 UpdateFrameArrivalMap(total_bytes_read_); | 334 UpdateFrameArrivalMap(total_bytes_read_); |
| 326 } | 335 } |
| 327 return QUIC_NO_ERROR; | 336 return QUIC_NO_ERROR; |
| 328 } | 337 } |
| 329 | 338 |
| 330 int QuicStreamSequencerBuffer::GetReadableRegions(struct iovec* iov, | 339 int QuicStreamSequencerBuffer::GetReadableRegions(struct iovec* iov, |
| 331 int iov_count) const { | 340 int iov_count) const { |
| 341 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; |
| 342 |
| 332 DCHECK(iov != nullptr); | 343 DCHECK(iov != nullptr); |
| 333 DCHECK_GT(iov_count, 0); | 344 DCHECK_GT(iov_count, 0); |
| 334 | 345 |
| 335 if (ReadableBytes() == 0) { | 346 if (ReadableBytes() == 0) { |
| 336 iov[0].iov_base = nullptr; | 347 iov[0].iov_base = nullptr; |
| 337 iov[0].iov_len = 0; | 348 iov[0].iov_len = 0; |
| 338 return 0; | 349 return 0; |
| 339 } | 350 } |
| 340 | 351 |
| 341 size_t start_block_idx = NextBlockToRead(); | 352 size_t start_block_idx = NextBlockToRead(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 iov[iov_used].iov_base = blocks_[end_block_idx]->buffer; | 391 iov[iov_used].iov_base = blocks_[end_block_idx]->buffer; |
| 381 iov[iov_used].iov_len = end_block_offset + 1; | 392 iov[iov_used].iov_len = end_block_offset + 1; |
| 382 DVLOG(1) << "Got last block with index: " << end_block_idx; | 393 DVLOG(1) << "Got last block with index: " << end_block_idx; |
| 383 ++iov_used; | 394 ++iov_used; |
| 384 } | 395 } |
| 385 return iov_used; | 396 return iov_used; |
| 386 } | 397 } |
| 387 | 398 |
| 388 bool QuicStreamSequencerBuffer::GetReadableRegion(iovec* iov, | 399 bool QuicStreamSequencerBuffer::GetReadableRegion(iovec* iov, |
| 389 QuicTime* timestamp) const { | 400 QuicTime* timestamp) const { |
| 401 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; |
| 402 |
| 390 if (ReadableBytes() == 0) { | 403 if (ReadableBytes() == 0) { |
| 391 iov[0].iov_base = nullptr; | 404 iov[0].iov_base = nullptr; |
| 392 iov[0].iov_len = 0; | 405 iov[0].iov_len = 0; |
| 393 return false; | 406 return false; |
| 394 } | 407 } |
| 395 | 408 |
| 396 size_t start_block_idx = NextBlockToRead(); | 409 size_t start_block_idx = NextBlockToRead(); |
| 397 iov->iov_base = blocks_[start_block_idx]->buffer + ReadOffset(); | 410 iov->iov_base = blocks_[start_block_idx]->buffer + ReadOffset(); |
| 398 size_t readable_bytes_in_block = min<size_t>( | 411 size_t readable_bytes_in_block = min<size_t>( |
| 399 GetBlockCapacity(start_block_idx) - ReadOffset(), ReadableBytes()); | 412 GetBlockCapacity(start_block_idx) - ReadOffset(), ReadableBytes()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 418 // If encountered the end of readable bytes before reaching a different | 431 // If encountered the end of readable bytes before reaching a different |
| 419 // timestamp. | 432 // timestamp. |
| 420 DVLOG(1) << "Got all readable bytes in first block."; | 433 DVLOG(1) << "Got all readable bytes in first block."; |
| 421 region_len = readable_bytes_in_block; | 434 region_len = readable_bytes_in_block; |
| 422 } | 435 } |
| 423 iov->iov_len = region_len; | 436 iov->iov_len = region_len; |
| 424 return true; | 437 return true; |
| 425 } | 438 } |
| 426 | 439 |
| 427 bool QuicStreamSequencerBuffer::MarkConsumed(size_t bytes_used) { | 440 bool QuicStreamSequencerBuffer::MarkConsumed(size_t bytes_used) { |
| 441 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; |
| 442 |
| 428 if (bytes_used > ReadableBytes()) { | 443 if (bytes_used > ReadableBytes()) { |
| 429 return false; | 444 return false; |
| 430 } | 445 } |
| 431 size_t bytes_to_consume = bytes_used; | 446 size_t bytes_to_consume = bytes_used; |
| 432 while (bytes_to_consume > 0) { | 447 while (bytes_to_consume > 0) { |
| 433 size_t block_idx = NextBlockToRead(); | 448 size_t block_idx = NextBlockToRead(); |
| 434 size_t offset_in_block = ReadOffset(); | 449 size_t offset_in_block = ReadOffset(); |
| 435 size_t bytes_available = min<size_t>( | 450 size_t bytes_available = min<size_t>( |
| 436 ReadableBytes(), GetBlockCapacity(block_idx) - offset_in_block); | 451 ReadableBytes(), GetBlockCapacity(block_idx) - offset_in_block); |
| 437 size_t bytes_read = min<size_t>(bytes_to_consume, bytes_available); | 452 size_t bytes_read = min<size_t>(bytes_to_consume, bytes_available); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 QuicStreamOffset current_frame_end_offset = | 597 QuicStreamOffset current_frame_end_offset = |
| 583 it.second.length + current_frame_begin_offset; | 598 it.second.length + current_frame_begin_offset; |
| 584 current_frames_string = string(StringPrintf( | 599 current_frames_string = string(StringPrintf( |
| 585 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), | 600 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), |
| 586 current_frame_begin_offset, current_frame_end_offset)); | 601 current_frame_begin_offset, current_frame_end_offset)); |
| 587 } | 602 } |
| 588 return current_frames_string; | 603 return current_frames_string; |
| 589 } | 604 } |
| 590 | 605 |
| 591 } // namespace net | 606 } // namespace net |
| OLD | NEW |