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