1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/buffered_socket_writer.h"
#include "base/message_loop.h"
#include "base/stl_util.h"
#include "net/base/net_errors.h"
namespace remoting {
namespace protocol {
class BufferedSocketWriterBase::PendingPacket {
public:
PendingPacket(scoped_refptr<net::IOBufferWithSize> data, Task* done_task)
: data_(data),
done_task_(done_task) {
}
~PendingPacket() {
if (done_task_.get())
done_task_->Run();
}
net::IOBufferWithSize* data() {
return data_;
}
private:
scoped_refptr<net::IOBufferWithSize> data_;
scoped_ptr<Task> done_task_;
DISALLOW_COPY_AND_ASSIGN(PendingPacket);
};
BufferedSocketWriterBase::BufferedSocketWriterBase()
: buffer_size_(0),
socket_(NULL),
message_loop_(NULL),
write_pending_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(
written_callback_(this, &BufferedSocketWriterBase::OnWritten)),
closed_(false) {
}
BufferedSocketWriterBase::~BufferedSocketWriterBase() { }
void BufferedSocketWriterBase::Init(net::Socket* socket,
WriteFailedCallback* callback) {
// TODO(garykac) Save copy of WriteFailedCallback.
base::AutoLock auto_lock(lock_);
message_loop_ = MessageLoop::current();
socket_ = socket;
DCHECK(socket_);
}
bool BufferedSocketWriterBase::Write(
scoped_refptr<net::IOBufferWithSize> data, Task* done_task) {
{
base::AutoLock auto_lock(lock_);
queue_.push_back(new PendingPacket(data, done_task));
buffer_size_ += data->size();
}
message_loop_->PostTask(
FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite));
return true;
}
void BufferedSocketWriterBase::DoWrite() {
DCHECK_EQ(message_loop_, MessageLoop::current());
DCHECK(socket_);
// Don't try to write if there is another write pending.
if (write_pending_)
return;
// Don't write after Close().
if (closed_)
return;
while (true) {
net::IOBuffer* current_packet;
int current_packet_size;
{
base::AutoLock auto_lock(lock_);
GetNextPacket_Locked(¤t_packet, ¤t_packet_size);
}
// Return if the queue is empty.
if (!current_packet)
return;
int result = socket_->Write(current_packet, current_packet_size,
&written_callback_);
if (result >= 0) {
base::AutoLock auto_lock(lock_);
AdvanceBufferPosition_Locked(result);
} else {
if (result == net::ERR_IO_PENDING) {
write_pending_ = true;
} else {
HandleError(result);
if (write_failed_callback_.get())
write_failed_callback_->Run(result);
}
return;
}
}
}
void BufferedSocketWriterBase::OnWritten(int result) {
DCHECK_EQ(message_loop_, MessageLoop::current());
write_pending_ = false;
if (result < 0) {
HandleError(result);
if (write_failed_callback_.get())
write_failed_callback_->Run(result);
return;
}
{
base::AutoLock auto_lock(lock_);
AdvanceBufferPosition_Locked(result);
}
// Schedule next write.
message_loop_->PostTask(
FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite));
}
void BufferedSocketWriterBase::HandleError(int result) {
DCHECK_EQ(message_loop_, MessageLoop::current());
closed_ = true;
base::AutoLock auto_lock(lock_);
STLDeleteElements(&queue_);
// Notify subclass that an error is received.
OnError_Locked(result);
}
int BufferedSocketWriterBase::GetBufferSize() {
base::AutoLock auto_lock(lock_);
return buffer_size_;
}
int BufferedSocketWriterBase::GetBufferChunks() {
base::AutoLock auto_lock(lock_);
return queue_.size();
}
void BufferedSocketWriterBase::Close() {
DCHECK_EQ(message_loop_, MessageLoop::current());
closed_ = true;
}
void BufferedSocketWriterBase::PopQueue() {
// This also calls |done_task|.
delete queue_.front();
queue_.pop_front();
}
BufferedSocketWriter::BufferedSocketWriter() { }
BufferedSocketWriter::~BufferedSocketWriter() {
STLDeleteElements(&queue_);
}
void BufferedSocketWriter::GetNextPacket_Locked(
net::IOBuffer** buffer, int* size) {
if (!current_buf_) {
if (queue_.empty()) {
*buffer = NULL;
return; // Nothing to write.
}
current_buf_ = new net::DrainableIOBuffer(
queue_.front()->data(), queue_.front()->data()->size());
}
*buffer = current_buf_;
*size = current_buf_->BytesRemaining();
}
void BufferedSocketWriter::AdvanceBufferPosition_Locked(int written) {
buffer_size_ -= written;
current_buf_->DidConsume(written);
if (current_buf_->BytesRemaining() == 0) {
PopQueue();
current_buf_ = NULL;
}
}
void BufferedSocketWriter::OnError_Locked(int result) {
current_buf_ = NULL;
}
BufferedDatagramWriter::BufferedDatagramWriter() { }
BufferedDatagramWriter::~BufferedDatagramWriter() { }
void BufferedDatagramWriter::GetNextPacket_Locked(
net::IOBuffer** buffer, int* size) {
if (queue_.empty()) {
*buffer = NULL;
return; // Nothing to write.
}
*buffer = queue_.front()->data();
*size = queue_.front()->data()->size();
}
void BufferedDatagramWriter::AdvanceBufferPosition_Locked(int written) {
DCHECK_EQ(written, queue_.front()->data()->size());
buffer_size_ -= queue_.front()->data()->size();
PopQueue();
}
void BufferedDatagramWriter::OnError_Locked(int result) {
// Nothing to do here.
}
} // namespace protocol
} // namespace remoting
|