| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/buffered_socket_writer.h" | 5 #include "remoting/protocol/buffered_socket_writer.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 WriteFailedCallback* callback) { | 49 WriteFailedCallback* callback) { |
| 50 // TODO(garykac) Save copy of WriteFailedCallback. | 50 // TODO(garykac) Save copy of WriteFailedCallback. |
| 51 base::AutoLock auto_lock(lock_); | 51 base::AutoLock auto_lock(lock_); |
| 52 message_loop_ = MessageLoop::current(); | 52 message_loop_ = MessageLoop::current(); |
| 53 socket_ = socket; | 53 socket_ = socket; |
| 54 DCHECK(socket_); | 54 DCHECK(socket_); |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool BufferedSocketWriterBase::Write( | 57 bool BufferedSocketWriterBase::Write( |
| 58 scoped_refptr<net::IOBufferWithSize> data, Task* done_task) { | 58 scoped_refptr<net::IOBufferWithSize> data, Task* done_task) { |
| 59 base::AutoLock auto_lock(lock_); | 59 { |
| 60 if (!socket_) | 60 base::AutoLock auto_lock(lock_); |
| 61 return false; | 61 queue_.push_back(new PendingPacket(data, done_task)); |
| 62 queue_.push_back(new PendingPacket(data, done_task)); | 62 buffer_size_ += data->size(); |
| 63 buffer_size_ += data->size(); | 63 } |
| 64 message_loop_->PostTask( | 64 message_loop_->PostTask( |
| 65 FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite)); | 65 FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite)); |
| 66 return true; | 66 return true; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BufferedSocketWriterBase::DoWrite() { | 69 void BufferedSocketWriterBase::DoWrite() { |
| 70 DCHECK_EQ(message_loop_, MessageLoop::current()); | 70 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 71 DCHECK(socket_); | 71 DCHECK(socket_); |
| 72 | 72 |
| 73 // Don't try to write if there is another write pending. | 73 // Don't try to write if there is another write pending. |
| 74 if (write_pending_) | 74 if (write_pending_) |
| 75 return; | 75 return; |
| 76 | 76 |
| 77 // Don't write after Close(). | 77 // Don't write after Close(). |
| 78 { | 78 if (closed_) |
| 79 base::AutoLock auto_lock(lock_); | 79 return; |
| 80 if (closed_) | |
| 81 return; | |
| 82 } | |
| 83 | 80 |
| 84 while (true) { | 81 while (true) { |
| 85 net::IOBuffer* current_packet; | 82 net::IOBuffer* current_packet; |
| 86 int current_packet_size; | 83 int current_packet_size; |
| 87 { | 84 { |
| 88 base::AutoLock auto_lock(lock_); | 85 base::AutoLock auto_lock(lock_); |
| 89 GetNextPacket_Locked(¤t_packet, ¤t_packet_size); | 86 GetNextPacket_Locked(¤t_packet, ¤t_packet_size); |
| 90 } | 87 } |
| 91 | 88 |
| 92 // Return if the queue is empty. | 89 // Return if the queue is empty. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 base::AutoLock auto_lock(lock_); | 123 base::AutoLock auto_lock(lock_); |
| 127 AdvanceBufferPosition_Locked(result); | 124 AdvanceBufferPosition_Locked(result); |
| 128 } | 125 } |
| 129 | 126 |
| 130 // Schedule next write. | 127 // Schedule next write. |
| 131 message_loop_->PostTask( | 128 message_loop_->PostTask( |
| 132 FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite)); | 129 FROM_HERE, NewRunnableMethod(this, &BufferedSocketWriterBase::DoWrite)); |
| 133 } | 130 } |
| 134 | 131 |
| 135 void BufferedSocketWriterBase::HandleError(int result) { | 132 void BufferedSocketWriterBase::HandleError(int result) { |
| 133 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 134 |
| 135 closed_ = true; |
| 136 |
| 136 base::AutoLock auto_lock(lock_); | 137 base::AutoLock auto_lock(lock_); |
| 137 closed_ = true; | |
| 138 STLDeleteElements(&queue_); | 138 STLDeleteElements(&queue_); |
| 139 | 139 |
| 140 // Notify subclass that an error is received. | 140 // Notify subclass that an error is received. |
| 141 OnError_Locked(result); | 141 OnError_Locked(result); |
| 142 } | 142 } |
| 143 | 143 |
| 144 int BufferedSocketWriterBase::GetBufferSize() { | 144 int BufferedSocketWriterBase::GetBufferSize() { |
| 145 base::AutoLock auto_lock(lock_); | 145 base::AutoLock auto_lock(lock_); |
| 146 return buffer_size_; | 146 return buffer_size_; |
| 147 } | 147 } |
| 148 | 148 |
| 149 int BufferedSocketWriterBase::GetBufferChunks() { | 149 int BufferedSocketWriterBase::GetBufferChunks() { |
| 150 base::AutoLock auto_lock(lock_); | 150 base::AutoLock auto_lock(lock_); |
| 151 return queue_.size(); | 151 return queue_.size(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void BufferedSocketWriterBase::Close() { | 154 void BufferedSocketWriterBase::Close() { |
| 155 base::AutoLock auto_lock(lock_); | 155 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 156 closed_ = true; | 156 closed_ = true; |
| 157 } | 157 } |
| 158 | 158 |
| 159 void BufferedSocketWriterBase::PopQueue() { | 159 void BufferedSocketWriterBase::PopQueue() { |
| 160 // This also calls |done_task|. | 160 // This also calls |done_task|. |
| 161 delete queue_.front(); | 161 delete queue_.front(); |
| 162 queue_.pop_front(); | 162 queue_.pop_front(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 BufferedSocketWriter::BufferedSocketWriter() { } | 165 BufferedSocketWriter::BufferedSocketWriter() { } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 buffer_size_ -= queue_.front()->data()->size(); | 215 buffer_size_ -= queue_.front()->data()->size(); |
| 216 PopQueue(); | 216 PopQueue(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void BufferedDatagramWriter::OnError_Locked(int result) { | 219 void BufferedDatagramWriter::OnError_Locked(int result) { |
| 220 // Nothing to do here. | 220 // Nothing to do here. |
| 221 } | 221 } |
| 222 | 222 |
| 223 } // namespace protocol | 223 } // namespace protocol |
| 224 } // namespace remoting | 224 } // namespace remoting |
| OLD | NEW |