spdy_read_queue.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "net/spdy/spdy_read_queue.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/check_op.h"
  8. #include "net/spdy/spdy_buffer.h"
  9. namespace net {
  10. SpdyReadQueue::SpdyReadQueue() = default;
  11. SpdyReadQueue::~SpdyReadQueue() {
  12. Clear();
  13. }
  14. bool SpdyReadQueue::IsEmpty() const {
  15. DCHECK_EQ(queue_.empty(), total_size_ == 0);
  16. return queue_.empty();
  17. }
  18. size_t SpdyReadQueue::GetTotalSize() const {
  19. return total_size_;
  20. }
  21. void SpdyReadQueue::Enqueue(std::unique_ptr<SpdyBuffer> buffer) {
  22. DCHECK_GT(buffer->GetRemainingSize(), 0u);
  23. total_size_ += buffer->GetRemainingSize();
  24. queue_.push_back(std::move(buffer));
  25. }
  26. size_t SpdyReadQueue::Dequeue(char* out, size_t len) {
  27. DCHECK_GT(len, 0u);
  28. size_t bytes_copied = 0;
  29. while (!queue_.empty() && bytes_copied < len) {
  30. SpdyBuffer* buffer = queue_.front().get();
  31. size_t bytes_to_copy =
  32. std::min(len - bytes_copied, buffer->GetRemainingSize());
  33. memcpy(out + bytes_copied, buffer->GetRemainingData(), bytes_to_copy);
  34. bytes_copied += bytes_to_copy;
  35. if (bytes_to_copy == buffer->GetRemainingSize())
  36. queue_.pop_front();
  37. else
  38. buffer->Consume(bytes_to_copy);
  39. }
  40. total_size_ -= bytes_copied;
  41. return bytes_copied;
  42. }
  43. void SpdyReadQueue::Clear() {
  44. queue_.clear();
  45. total_size_ = 0;
  46. }
  47. } // namespace net