Chromium Code Reviews| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 buffer_size_ += data->size(); | 63 buffer_size_ += data->size(); |
| 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. |
|
Sergey Ulanov
2011/01/20 19:14:17
Remove extra spaces
Alpha Left Google
2011/01/20 19:32:08
weird... done.
| |
| 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 { |
| 79 AutoLock auto_lock(lock_); | 79 AutoLock auto_lock(lock_); |
| 80 if (closed_) | 80 if (closed_) |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 while (true) { | 84 while (true) { |
| 85 net::IOBuffer* current_packet; | 85 net::IOBuffer* current_packet; |
| 86 int current_packet_size; | 86 int current_packet_size; |
| 87 { | 87 { |
| 88 AutoLock auto_lock(lock_); | 88 AutoLock auto_lock(lock_); |
| 89 GetNextPacket_Locked(¤t_packet, ¤t_packet_size); | 89 GetNextPacket_Locked(¤t_packet, ¤t_packet_size); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Return if the queue is empty. | 92 // Return if the queue is empty. |
| 93 if (!current_packet) | 93 if (!current_packet) |
| 94 return; | 94 return; |
| 95 | 95 |
| 96 int result = socket_->Write(current_packet, current_packet_size, | 96 int result = socket_->Write(current_packet, current_packet_size, |
| 97 &written_callback_); | 97 &written_callback_); |
| 98 if (result >= 0) { | 98 if (result >= 0) { |
| 99 AutoLock auto_lock(lock_); | 99 AutoLock auto_lock(lock_); |
| 100 AdvanceBufferPosition_Locked(result); | 100 AdvanceBufferPosition_Locked(result); |
| 101 } else { | 101 } else { |
| 102 if (result == net::ERR_IO_PENDING) { | 102 if (result == net::ERR_IO_PENDING) { |
| 103 write_pending_ = true; | 103 write_pending_ = true; |
| 104 } else { | 104 } else { |
| 105 HandleError(result); | 105 HandleError(result); |
| 106 if (write_failed_callback_.get()) | |
| 107 write_failed_callback_->Run(result); | |
| 108 } | 106 } |
| 109 return; | 107 return; |
| 110 } | 108 } |
| 111 } | 109 } |
| 112 } | 110 } |
| 113 | 111 |
| 114 void BufferedSocketWriterBase::OnWritten(int result) { | 112 void BufferedSocketWriterBase::OnWritten(int result) { |
| 115 DCHECK_EQ(message_loop_, MessageLoop::current()); | 113 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 116 write_pending_ = false; | 114 write_pending_ = false; |
| 117 | 115 |
| 118 if (result < 0) { | 116 if (result < 0) { |
| 119 HandleError(result); | 117 HandleError(result); |
| 120 if (write_failed_callback_.get()) | |
| 121 write_failed_callback_->Run(result); | |
| 122 return; | 118 return; |
| 123 } | 119 } |
| 124 | 120 |
| 125 { | 121 { |
| 126 AutoLock auto_lock(lock_); | 122 AutoLock auto_lock(lock_); |
| 127 AdvanceBufferPosition_Locked(result); | 123 AdvanceBufferPosition_Locked(result); |
| 128 } | 124 } |
| 129 | 125 |
| 130 // Schedule next write. | 126 // Schedule next write. |
| 131 message_loop_->PostTask( | 127 message_loop_->PostTask( |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 buffer_size_ -= queue_.front()->data()->size(); | 211 buffer_size_ -= queue_.front()->data()->size(); |
| 216 PopQueue(); | 212 PopQueue(); |
| 217 } | 213 } |
| 218 | 214 |
| 219 void BufferedDatagramWriter::OnError_Locked(int result) { | 215 void BufferedDatagramWriter::OnError_Locked(int result) { |
| 220 // Nothing to do here. | 216 // Nothing to do here. |
| 221 } | 217 } |
| 222 | 218 |
| 223 } // namespace protocol | 219 } // namespace protocol |
| 224 } // namespace remoting | 220 } // namespace remoting |
| OLD | NEW |