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_write_queue.h" |
| 6 |
| 7 #include <cstddef> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "net/spdy/spdy_frame_producer.h" |
| 11 #include "net/spdy/spdy_stream.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 SpdyWriteQueue::PendingWrite::PendingWrite() : frame_producer(NULL) {} |
| 16 |
| 17 SpdyWriteQueue::PendingWrite::PendingWrite( |
| 18 SpdyFrameProducer* frame_producer, |
| 19 const scoped_refptr<SpdyStream>& stream) |
| 20 : frame_producer(frame_producer), |
| 21 stream(stream) {} |
| 22 |
| 23 SpdyWriteQueue::PendingWrite::~PendingWrite() {} |
| 24 |
| 25 SpdyWriteQueue::SpdyWriteQueue() {} |
| 26 |
| 27 SpdyWriteQueue::~SpdyWriteQueue() { |
| 28 Clear(); |
| 29 } |
| 30 |
| 31 void SpdyWriteQueue::Enqueue(RequestPriority priority, |
| 32 scoped_ptr<SpdyFrameProducer> frame_producer, |
| 33 const scoped_refptr<SpdyStream>& stream) { |
| 34 if (stream.get()) { |
| 35 DCHECK_EQ(stream->priority(), priority); |
| 36 } |
| 37 queue_[priority].push_back(PendingWrite(frame_producer.release(), stream)); |
| 38 } |
| 39 |
| 40 bool SpdyWriteQueue::Dequeue(scoped_ptr<SpdyFrameProducer>* frame_producer, |
| 41 scoped_refptr<SpdyStream>* stream) { |
| 42 for (int i = NUM_PRIORITIES - 1; i >= 0; --i) { |
| 43 if (!queue_[i].empty()) { |
| 44 PendingWrite pending_write = queue_[i].front(); |
| 45 queue_[i].pop_front(); |
| 46 frame_producer->reset(pending_write.frame_producer); |
| 47 *stream = pending_write.stream; |
| 48 return true; |
| 49 } |
| 50 } |
| 51 return false; |
| 52 } |
| 53 |
| 54 void SpdyWriteQueue::RemovePendingWritesForStream( |
| 55 const scoped_refptr<SpdyStream>& stream) { |
| 56 DCHECK(stream.get()); |
| 57 if (DCHECK_IS_ON()) { |
| 58 // |stream| should not have pending writes in a queue not matching |
| 59 // its priority. |
| 60 for (int i = 0; i < NUM_PRIORITIES; ++i) { |
| 61 if (stream->priority() == i) |
| 62 continue; |
| 63 for (std::deque<PendingWrite>::const_iterator it = queue_[i].begin(); |
| 64 it != queue_[i].end(); ++it) { |
| 65 DCHECK_NE(it->stream, stream); |
| 66 } |
| 67 } |
| 68 } |
| 69 |
| 70 // Do the actual deletion and removal, preserving FIFO-ness. |
| 71 std::deque<PendingWrite>* queue = &queue_[stream->priority()]; |
| 72 std::deque<PendingWrite>::iterator out_it = queue->begin(); |
| 73 for (std::deque<PendingWrite>::const_iterator it = queue->begin(); |
| 74 it != queue->end(); ++it) { |
| 75 if (it->stream == stream) { |
| 76 delete it->frame_producer; |
| 77 } else { |
| 78 *out_it = *it; |
| 79 ++out_it; |
| 80 } |
| 81 } |
| 82 queue->erase(out_it, queue->end()); |
| 83 } |
| 84 |
| 85 void SpdyWriteQueue::Clear() { |
| 86 for (int i = 0; i < NUM_PRIORITIES; ++i) { |
| 87 for (std::deque<PendingWrite>::iterator it = queue_[i].begin(); |
| 88 it != queue_[i].end(); ++it) { |
| 89 delete it->frame_producer; |
| 90 } |
| 91 queue_[i].clear(); |
| 92 } |
| 93 } |
| 94 |
| 95 } // namespace net |
OLD | NEW |