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.
5 #include "remoting/base/buffered_socket_writer.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/stl_util.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "net/base/net_errors.h"
16 struct BufferedSocketWriterBase::PendingPacket
{
17 PendingPacket(scoped_refptr
<net::IOBufferWithSize
> data
,
18 const base::Closure
& done_task
)
20 done_task(done_task
) {
23 scoped_refptr
<net::IOBufferWithSize
> data
;
24 base::Closure done_task
;
27 BufferedSocketWriterBase::BufferedSocketWriterBase()
30 write_pending_(false),
32 destroyed_flag_(nullptr) {
35 void BufferedSocketWriterBase::Init(net::Socket
* socket
,
36 const WriteFailedCallback
& callback
) {
37 DCHECK(CalledOnValidThread());
40 write_failed_callback_
= callback
;
43 bool BufferedSocketWriterBase::Write(
44 scoped_refptr
<net::IOBufferWithSize
> data
, const base::Closure
& done_task
) {
45 DCHECK(CalledOnValidThread());
49 // Don't write after Close().
53 queue_
.push_back(new PendingPacket(data
, done_task
));
54 buffer_size_
+= data
->size();
58 // DoWrite() may trigger OnWriteError() to be called.
62 void BufferedSocketWriterBase::DoWrite() {
63 DCHECK(CalledOnValidThread());
66 // Don't try to write if there is another write pending.
70 // Don't write after Close().
75 net::IOBuffer
* current_packet
;
76 int current_packet_size
;
77 GetNextPacket(¤t_packet
, ¤t_packet_size
);
79 // Return if the queue is empty.
83 int result
= socket_
->Write(
84 current_packet
, current_packet_size
,
85 base::Bind(&BufferedSocketWriterBase::OnWritten
,
86 base::Unretained(this)));
87 bool write_again
= false;
88 HandleWriteResult(result
, &write_again
);
94 void BufferedSocketWriterBase::HandleWriteResult(int result
,
98 if (result
== net::ERR_IO_PENDING
) {
99 write_pending_
= true;
102 if (!write_failed_callback_
.is_null())
103 write_failed_callback_
.Run(result
);
108 base::Closure done_task
= AdvanceBufferPosition(result
);
109 if (!done_task
.is_null()) {
110 bool destroyed
= false;
111 destroyed_flag_
= &destroyed
;
114 // Stop doing anything if we've been destroyed by the callback.
117 destroyed_flag_
= nullptr;
123 void BufferedSocketWriterBase::OnWritten(int result
) {
124 DCHECK(CalledOnValidThread());
125 DCHECK(write_pending_
);
126 write_pending_
= false;
129 HandleWriteResult(result
, &write_again
);
134 void BufferedSocketWriterBase::HandleError(int result
) {
135 DCHECK(CalledOnValidThread());
139 STLDeleteElements(&queue_
);
141 // Notify subclass that an error is received.
145 int BufferedSocketWriterBase::GetBufferSize() {
149 int BufferedSocketWriterBase::GetBufferChunks() {
150 return queue_
.size();
153 void BufferedSocketWriterBase::Close() {
154 DCHECK(CalledOnValidThread());
158 BufferedSocketWriterBase::~BufferedSocketWriterBase() {
160 *destroyed_flag_
= true;
162 STLDeleteElements(&queue_
);
165 base::Closure
BufferedSocketWriterBase::PopQueue() {
166 base::Closure result
= queue_
.front()->done_task
;
167 delete queue_
.front();
172 BufferedSocketWriter::BufferedSocketWriter() {
175 void BufferedSocketWriter::GetNextPacket(
176 net::IOBuffer
** buffer
, int* size
) {
177 if (!current_buf_
.get()) {
178 if (queue_
.empty()) {
180 return; // Nothing to write.
182 current_buf_
= new net::DrainableIOBuffer(queue_
.front()->data
.get(),
183 queue_
.front()->data
->size());
186 *buffer
= current_buf_
.get();
187 *size
= current_buf_
->BytesRemaining();
190 base::Closure
BufferedSocketWriter::AdvanceBufferPosition(int written
) {
191 buffer_size_
-= written
;
192 current_buf_
->DidConsume(written
);
194 if (current_buf_
->BytesRemaining() == 0) {
195 current_buf_
= nullptr;
198 return base::Closure();
201 void BufferedSocketWriter::OnError(int result
) {
202 current_buf_
= nullptr;
205 BufferedSocketWriter::~BufferedSocketWriter() {
208 BufferedDatagramWriter::BufferedDatagramWriter() {
211 void BufferedDatagramWriter::GetNextPacket(
212 net::IOBuffer
** buffer
, int* size
) {
213 if (queue_
.empty()) {
215 return; // Nothing to write.
217 *buffer
= queue_
.front()->data
.get();
218 *size
= queue_
.front()->data
->size();
221 base::Closure
BufferedDatagramWriter::AdvanceBufferPosition(int written
) {
222 DCHECK_EQ(written
, queue_
.front()->data
->size());
223 buffer_size_
-= queue_
.front()->data
->size();
227 void BufferedDatagramWriter::OnError(int result
) {
228 // Nothing to do here.
231 BufferedDatagramWriter::~BufferedDatagramWriter() {
234 } // namespace remoting