Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: blimp/net/blimp_message_output_buffer.cc

Issue 2632803002: Remove all blimp network code. (Closed)
Patch Set: merge from origin/master for good measure Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "blimp/net/blimp_message_output_buffer.h"
6
7 #include <algorithm>
8
9 #include "base/location.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "blimp/common/logging.h"
15 #include "blimp/common/proto/blimp_message.pb.h"
16 #include "net/base/net_errors.h"
17
18 namespace blimp {
19
20 BlimpMessageOutputBuffer::BlimpMessageOutputBuffer(int max_buffer_size_bytes)
21 : max_buffer_size_bytes_(max_buffer_size_bytes) {}
22
23 BlimpMessageOutputBuffer::~BlimpMessageOutputBuffer() {}
24
25 void BlimpMessageOutputBuffer::SetOutputProcessor(
26 BlimpMessageProcessor* processor) {
27 DVLOG(1) << "SetOutputProcessor " << processor;
28 // Check that we are setting or removing the processor, not replacing it.
29 if (processor) {
30 DCHECK(!output_processor_);
31 output_processor_ = processor;
32 write_complete_cb_.Reset(base::Bind(
33 &BlimpMessageOutputBuffer::OnWriteComplete, base::Unretained(this)));
34 WriteNextMessageIfReady();
35 } else {
36 DCHECK(output_processor_);
37 output_processor_ = nullptr;
38 write_complete_cb_.Cancel();
39 }
40 }
41
42 void BlimpMessageOutputBuffer::RetransmitBufferedMessages() {
43 DCHECK(output_processor_);
44 DVLOG(1) << "RetransmitBufferedMessages()";
45
46 // Prepend the entirety of |ack_buffer_| to |write_buffer_|.
47 write_buffer_.insert(write_buffer_.begin(),
48 std::make_move_iterator(ack_buffer_.begin()),
49 std::make_move_iterator(ack_buffer_.end()));
50 ack_buffer_.clear();
51
52 WriteNextMessageIfReady();
53 }
54
55 int BlimpMessageOutputBuffer::GetBufferByteSizeForTest() const {
56 return write_buffer_.size() + ack_buffer_.size();
57 }
58
59 int BlimpMessageOutputBuffer::GetUnacknowledgedMessageCountForTest() const {
60 return ack_buffer_.size();
61 }
62
63 void BlimpMessageOutputBuffer::ProcessMessage(
64 std::unique_ptr<BlimpMessage> message,
65 const net::CompletionCallback& callback) {
66 DVLOG(2) << "OutputBuffer::ProcessMessage " << *message;
67
68 message->set_message_id(++prev_message_id_);
69
70 current_buffer_size_bytes_ += message->ByteSize();
71 if (max_buffer_size_bytes_ < current_buffer_size_bytes_)
72 DLOG(WARNING) << "Output Buffer Size exceeds " << max_buffer_size_bytes_
73 << "bytes. Current size: " << current_buffer_size_bytes_
74 << " bytes.";
75
76 write_buffer_.push_back(
77 base::MakeUnique<BufferEntry>(std::move(message), callback));
78
79 // Write the message
80 if (write_buffer_.size() == 1 && output_processor_) {
81 WriteNextMessageIfReady();
82 }
83 }
84
85 // Flushes acknowledged messages from the buffer and invokes their
86 // |callbacks|, if any.
87 void BlimpMessageOutputBuffer::OnMessageCheckpoint(int64_t message_id) {
88 VLOG(2) << "OnMessageCheckpoint (message_id=" << message_id << ")";
89 if (ack_buffer_.empty()) {
90 LOG(WARNING) << "Checkpoint called while buffer is empty.";
91 return;
92 }
93 if (message_id > prev_message_id_) {
94 LOG(WARNING) << "Illegal checkpoint response: " << message_id;
95 return;
96 }
97
98 // Remove all acknowledged messages through |message_id| and invoke their
99 // write callbacks, if set.
100 while (!ack_buffer_.empty() &&
101 ack_buffer_.front()->message->message_id() <= message_id) {
102 const BufferEntry& ack_entry = *ack_buffer_.front();
103 current_buffer_size_bytes_ -= ack_entry.message->GetCachedSize();
104 DCHECK_GE(current_buffer_size_bytes_, 0);
105 VLOG(3) << "Buffer size: " << current_buffer_size_bytes_
106 << " (max=" << current_buffer_size_bytes_ << ")";
107
108 if (!ack_entry.callback.is_null()) {
109 base::ThreadTaskRunnerHandle::Get()->PostTask(
110 FROM_HERE, base::Bind(ack_entry.callback, net::OK));
111 }
112
113 ack_buffer_.pop_front();
114 }
115
116 // An empty buffer should have a zero-byte footprint.
117 DCHECK(current_buffer_size_bytes_ > 0 ||
118 (ack_buffer_.empty() && write_buffer_.empty()))
119 << "Expected zero-length buffer size, was " << current_buffer_size_bytes_
120 << " bytes instead.";
121 }
122
123 BlimpMessageOutputBuffer::BufferEntry::BufferEntry(
124 std::unique_ptr<BlimpMessage> message,
125 net::CompletionCallback callback)
126 : message(std::move(message)), callback(callback) {}
127
128 BlimpMessageOutputBuffer::BufferEntry::~BufferEntry() {}
129
130 void BlimpMessageOutputBuffer::WriteNextMessageIfReady() {
131 DVLOG(3) << "WriteNextMessageIfReady";
132 if (write_buffer_.empty()) {
133 DVLOG(3) << "Nothing to write.";
134 return;
135 }
136
137 std::unique_ptr<BlimpMessage> message_to_write(
138 new BlimpMessage(*write_buffer_.front()->message));
139 DVLOG(3) << "Writing message (id="
140 << write_buffer_.front()->message->message_id() << ", "
141 << *message_to_write << ")";
142
143 output_processor_->ProcessMessage(std::move(message_to_write),
144 write_complete_cb_.callback());
145 DVLOG(3) << "Queue size: " << write_buffer_.size();
146 }
147
148 void BlimpMessageOutputBuffer::OnWriteComplete(int result) {
149 DCHECK_LE(result, 0);
150
151 VLOG(2) << "Write result=" << net::ErrorToString(result);
152 if (result == net::OK) {
153 ack_buffer_.push_back(std::move(write_buffer_.front()));
154 write_buffer_.pop_front();
155 WriteNextMessageIfReady();
156 }
157 }
158
159 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/blimp_message_output_buffer.h ('k') | blimp/net/blimp_message_output_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698