| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 QuicTime timestamp) | 44 QuicTime timestamp) |
| 45 : length(length), timestamp(timestamp) {} | 45 : length(length), timestamp(timestamp) {} |
| 46 | 46 |
| 47 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) | 47 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) |
| 48 : max_buffer_capacity_bytes_(max_capacity_bytes), | 48 : max_buffer_capacity_bytes_(max_capacity_bytes), |
| 49 blocks_count_( | 49 blocks_count_( |
| 50 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), | 50 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), |
| 51 total_bytes_read_(0), | 51 total_bytes_read_(0), |
| 52 blocks_(nullptr), | 52 blocks_(nullptr), |
| 53 destruction_indicator_(123456) { | 53 destruction_indicator_(123456) { |
| 54 CHECK_GT(blocks_count_, 1u) | 54 // blocks_count_ = |blocks_count_|, max_buffer_capacity_bytes_ = |
| 55 << "blocks_count_ = " << blocks_count_ | 55 // |max_buffer_capacity_bytes_| |
| 56 << ", max_buffer_capacity_bytes_ = " << max_buffer_capacity_bytes_; | 56 CHECK_GT(blocks_count_, 1u); |
| 57 Clear(); | 57 Clear(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { | 60 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { |
| 61 Clear(); | 61 Clear(); |
| 62 destruction_indicator_ = 654321; | 62 destruction_indicator_ = 654321; |
| 63 } | 63 } |
| 64 | 64 |
| 65 void QuicStreamSequencerBuffer::Clear() { | 65 void QuicStreamSequencerBuffer::Clear() { |
| 66 if (blocks_ != nullptr) { | 66 if (blocks_ != nullptr) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 89 DVLOG(1) << "Retired block with index: " << idx; | 89 DVLOG(1) << "Retired block with index: " << idx; |
| 90 return true; | 90 return true; |
| 91 } | 91 } |
| 92 | 92 |
| 93 QuicErrorCode QuicStreamSequencerBuffer::OnStreamData( | 93 QuicErrorCode QuicStreamSequencerBuffer::OnStreamData( |
| 94 QuicStreamOffset starting_offset, | 94 QuicStreamOffset starting_offset, |
| 95 base::StringPiece data, | 95 base::StringPiece data, |
| 96 QuicTime timestamp, | 96 QuicTime timestamp, |
| 97 size_t* const bytes_buffered, | 97 size_t* const bytes_buffered, |
| 98 std::string* error_details) { | 98 std::string* error_details) { |
| 99 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; | 99 // This object has been destructed |
| 100 CHECK_EQ(destruction_indicator_, 123456); |
| 100 *bytes_buffered = 0; | 101 *bytes_buffered = 0; |
| 101 QuicStreamOffset offset = starting_offset; | 102 QuicStreamOffset offset = starting_offset; |
| 102 size_t size = data.size(); | 103 size_t size = data.size(); |
| 103 if (size == 0) { | 104 if (size == 0) { |
| 104 *error_details = "Received empty stream frame without FIN."; | 105 *error_details = "Received empty stream frame without FIN."; |
| 105 return QUIC_EMPTY_STREAM_FRAME_NO_FIN; | 106 return QUIC_EMPTY_STREAM_FRAME_NO_FIN; |
| 106 } | 107 } |
| 107 | 108 |
| 108 // 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 |
| 109 // fill if the arriving frame doesn't overlaps with previous ones. | 110 // fill if the arriving frame doesn't overlaps with previous ones. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 start_offset + bytes_written) { | 273 start_offset + bytes_written) { |
| 273 // This gap has been filled with new data. So it's no longer a gap. | 274 // This gap has been filled with new data. So it's no longer a gap. |
| 274 gaps_.erase(gap_with_new_data_written); | 275 gaps_.erase(gap_with_new_data_written); |
| 275 } | 276 } |
| 276 } | 277 } |
| 277 | 278 |
| 278 QuicErrorCode QuicStreamSequencerBuffer::Readv(const iovec* dest_iov, | 279 QuicErrorCode QuicStreamSequencerBuffer::Readv(const iovec* dest_iov, |
| 279 size_t dest_count, | 280 size_t dest_count, |
| 280 size_t* bytes_read, | 281 size_t* bytes_read, |
| 281 string* error_details) { | 282 string* error_details) { |
| 282 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; | 283 // This object has been destructed |
| 284 CHECK_EQ(destruction_indicator_, 123456); |
| 283 | 285 |
| 284 *bytes_read = 0; | 286 *bytes_read = 0; |
| 285 for (size_t i = 0; i < dest_count && ReadableBytes() > 0; ++i) { | 287 for (size_t i = 0; i < dest_count && ReadableBytes() > 0; ++i) { |
| 286 char* dest = reinterpret_cast<char*>(dest_iov[i].iov_base); | 288 char* dest = reinterpret_cast<char*>(dest_iov[i].iov_base); |
| 287 CHECK_NE(dest, nullptr); | 289 CHECK_NE(dest, nullptr); |
| 288 size_t dest_remaining = dest_iov[i].iov_len; | 290 size_t dest_remaining = dest_iov[i].iov_len; |
| 289 while (dest_remaining > 0 && ReadableBytes() > 0) { | 291 while (dest_remaining > 0 && ReadableBytes() > 0) { |
| 290 size_t block_idx = NextBlockToRead(); | 292 size_t block_idx = NextBlockToRead(); |
| 291 size_t start_offset_in_block = ReadOffset(); | 293 size_t start_offset_in_block = ReadOffset(); |
| 292 size_t block_capacity = GetBlockCapacity(block_idx); | 294 size_t block_capacity = GetBlockCapacity(block_idx); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 } | 333 } |
| 332 | 334 |
| 333 if (*bytes_read > 0) { | 335 if (*bytes_read > 0) { |
| 334 UpdateFrameArrivalMap(total_bytes_read_); | 336 UpdateFrameArrivalMap(total_bytes_read_); |
| 335 } | 337 } |
| 336 return QUIC_NO_ERROR; | 338 return QUIC_NO_ERROR; |
| 337 } | 339 } |
| 338 | 340 |
| 339 int QuicStreamSequencerBuffer::GetReadableRegions(struct iovec* iov, | 341 int QuicStreamSequencerBuffer::GetReadableRegions(struct iovec* iov, |
| 340 int iov_count) const { | 342 int iov_count) const { |
| 341 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; | 343 // This object has been destructed |
| 344 CHECK_EQ(destruction_indicator_, 123456); |
| 342 | 345 |
| 343 DCHECK(iov != nullptr); | 346 DCHECK(iov != nullptr); |
| 344 DCHECK_GT(iov_count, 0); | 347 DCHECK_GT(iov_count, 0); |
| 345 | 348 |
| 346 if (ReadableBytes() == 0) { | 349 if (ReadableBytes() == 0) { |
| 347 iov[0].iov_base = nullptr; | 350 iov[0].iov_base = nullptr; |
| 348 iov[0].iov_len = 0; | 351 iov[0].iov_len = 0; |
| 349 return 0; | 352 return 0; |
| 350 } | 353 } |
| 351 | 354 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 iov[iov_used].iov_base = blocks_[end_block_idx]->buffer; | 394 iov[iov_used].iov_base = blocks_[end_block_idx]->buffer; |
| 392 iov[iov_used].iov_len = end_block_offset + 1; | 395 iov[iov_used].iov_len = end_block_offset + 1; |
| 393 DVLOG(1) << "Got last block with index: " << end_block_idx; | 396 DVLOG(1) << "Got last block with index: " << end_block_idx; |
| 394 ++iov_used; | 397 ++iov_used; |
| 395 } | 398 } |
| 396 return iov_used; | 399 return iov_used; |
| 397 } | 400 } |
| 398 | 401 |
| 399 bool QuicStreamSequencerBuffer::GetReadableRegion(iovec* iov, | 402 bool QuicStreamSequencerBuffer::GetReadableRegion(iovec* iov, |
| 400 QuicTime* timestamp) const { | 403 QuicTime* timestamp) const { |
| 401 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; | 404 // This object has been destructed |
| 405 CHECK_EQ(destruction_indicator_, 123456); |
| 402 | 406 |
| 403 if (ReadableBytes() == 0) { | 407 if (ReadableBytes() == 0) { |
| 404 iov[0].iov_base = nullptr; | 408 iov[0].iov_base = nullptr; |
| 405 iov[0].iov_len = 0; | 409 iov[0].iov_len = 0; |
| 406 return false; | 410 return false; |
| 407 } | 411 } |
| 408 | 412 |
| 409 size_t start_block_idx = NextBlockToRead(); | 413 size_t start_block_idx = NextBlockToRead(); |
| 410 iov->iov_base = blocks_[start_block_idx]->buffer + ReadOffset(); | 414 iov->iov_base = blocks_[start_block_idx]->buffer + ReadOffset(); |
| 411 size_t readable_bytes_in_block = std::min<size_t>( | 415 size_t readable_bytes_in_block = std::min<size_t>( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 431 // If encountered the end of readable bytes before reaching a different | 435 // If encountered the end of readable bytes before reaching a different |
| 432 // timestamp. | 436 // timestamp. |
| 433 DVLOG(1) << "Got all readable bytes in first block."; | 437 DVLOG(1) << "Got all readable bytes in first block."; |
| 434 region_len = readable_bytes_in_block; | 438 region_len = readable_bytes_in_block; |
| 435 } | 439 } |
| 436 iov->iov_len = region_len; | 440 iov->iov_len = region_len; |
| 437 return true; | 441 return true; |
| 438 } | 442 } |
| 439 | 443 |
| 440 bool QuicStreamSequencerBuffer::MarkConsumed(size_t bytes_used) { | 444 bool QuicStreamSequencerBuffer::MarkConsumed(size_t bytes_used) { |
| 441 CHECK_EQ(destruction_indicator_, 123456) << "This object has been destructed"; | 445 // This object has been destructed |
| 446 CHECK_EQ(destruction_indicator_, 123456); |
| 442 | 447 |
| 443 if (bytes_used > ReadableBytes()) { | 448 if (bytes_used > ReadableBytes()) { |
| 444 return false; | 449 return false; |
| 445 } | 450 } |
| 446 size_t bytes_to_consume = bytes_used; | 451 size_t bytes_to_consume = bytes_used; |
| 447 while (bytes_to_consume > 0) { | 452 while (bytes_to_consume > 0) { |
| 448 size_t block_idx = NextBlockToRead(); | 453 size_t block_idx = NextBlockToRead(); |
| 449 size_t offset_in_block = ReadOffset(); | 454 size_t offset_in_block = ReadOffset(); |
| 450 size_t bytes_available = std::min<size_t>( | 455 size_t bytes_available = std::min<size_t>( |
| 451 ReadableBytes(), GetBlockCapacity(block_idx) - offset_in_block); | 456 ReadableBytes(), GetBlockCapacity(block_idx) - offset_in_block); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 QuicStreamOffset current_frame_end_offset = | 602 QuicStreamOffset current_frame_end_offset = |
| 598 it.second.length + current_frame_begin_offset; | 603 it.second.length + current_frame_begin_offset; |
| 599 current_frames_string = string(StringPrintf( | 604 current_frames_string = string(StringPrintf( |
| 600 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), | 605 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), |
| 601 current_frame_begin_offset, current_frame_end_offset)); | 606 current_frame_begin_offset, current_frame_end_offset)); |
| 602 } | 607 } |
| 603 return current_frames_string; | 608 return current_frames_string; |
| 604 } | 609 } |
| 605 | 610 |
| 606 } // namespace net | 611 } // namespace net |
| OLD | NEW |