1 // Copyright (c) 2013 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/spdy/spdy_read_queue.h" 6 7 #include "base/logging.h" 8 #include "base/stl_util.h" 9 #include "net/spdy/spdy_buffer.h" 10 11 namespace net { 12 13 SpdyReadQueue::SpdyReadQueue() : total_size_(0) {} 14 15 SpdyReadQueue::~SpdyReadQueue() { 16 Clear(); 17 } 18 19 bool SpdyReadQueue::IsEmpty() const { 20 DCHECK_EQ(queue_.empty(), total_size_ == 0); 21 return queue_.empty(); 22 } 23 24 size_t SpdyReadQueue::GetTotalSize() const { 25 return total_size_; 26 } 27 28 void SpdyReadQueue::Enqueue(scoped_ptr<SpdyBuffer> buffer) { 29 DCHECK_GT(buffer->GetRemainingSize(), 0u); 30 total_size_ += buffer->GetRemainingSize(); 31 queue_.push_back(buffer.release()); 32 } 33 34 size_t SpdyReadQueue::Dequeue(char* out, size_t len) { 35 DCHECK_GT(len, 0u); 36 size_t bytes_copied = 0; 37 while (!queue_.empty() && bytes_copied < len) { 38 SpdyBuffer* buffer = queue_.front(); 39 size_t bytes_to_copy = 40 std::min(len - bytes_copied, buffer->GetRemainingSize()); 41 memcpy(out + bytes_copied, buffer->GetRemainingData(), bytes_to_copy); 42 bytes_copied += bytes_to_copy; 43 if (bytes_to_copy == buffer->GetRemainingSize()) { 44 delete queue_.front(); 45 queue_.pop_front(); 46 } else { 47 buffer->Consume(bytes_to_copy); 48 } 49 } 50 total_size_ -= bytes_copied; 51 return bytes_copied; 52 } 53 54 void SpdyReadQueue::Clear() { 55 STLDeleteElements(&queue_); 56 queue_.clear(); 57 } 58 59 } // namespace 60