| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/message_reader.h" | 5 #include "remoting/protocol/message_reader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 MessageReader::~MessageReader() { | 32 MessageReader::~MessageReader() { |
| 33 } | 33 } |
| 34 | 34 |
| 35 void MessageReader::SetMessageReceivedCallback( | 35 void MessageReader::SetMessageReceivedCallback( |
| 36 const MessageReceivedCallback& callback) { | 36 const MessageReceivedCallback& callback) { |
| 37 DCHECK(CalledOnValidThread()); | 37 DCHECK(CalledOnValidThread()); |
| 38 message_received_callback_ = callback; | 38 message_received_callback_ = callback; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void MessageReader::StartReading(net::Socket* socket) { | 41 void MessageReader::StartReading( |
| 42 net::Socket* socket, |
| 43 const ReadFailedCallback& read_failed_callback) { |
| 42 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
| 43 DCHECK(socket); | 45 DCHECK(socket); |
| 46 DCHECK(!read_failed_callback.is_null()); |
| 47 |
| 44 socket_ = socket; | 48 socket_ = socket; |
| 49 read_failed_callback_ = read_failed_callback; |
| 45 DoRead(); | 50 DoRead(); |
| 46 } | 51 } |
| 47 | 52 |
| 48 void MessageReader::DoRead() { | 53 void MessageReader::DoRead() { |
| 49 DCHECK(CalledOnValidThread()); | 54 DCHECK(CalledOnValidThread()); |
| 50 // Don't try to read again if there is another read pending or we | 55 // Don't try to read again if there is another read pending or we |
| 51 // have messages that we haven't finished processing yet. | 56 // have messages that we haven't finished processing yet. |
| 52 while (!closed_ && !read_pending_ && pending_messages_ == 0) { | 57 bool read_again = true; |
| 58 while (read_again && !closed_ && !read_pending_ && pending_messages_ == 0) { |
| 53 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 59 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 54 int result = socket_->Read( | 60 int result = socket_->Read( |
| 55 read_buffer_.get(), | 61 read_buffer_.get(), |
| 56 kReadBufferSize, | 62 kReadBufferSize, |
| 57 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); | 63 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); |
| 58 HandleReadResult(result); | 64 |
| 65 HandleReadResult(result, &read_again); |
| 59 } | 66 } |
| 60 } | 67 } |
| 61 | 68 |
| 62 void MessageReader::OnRead(int result) { | 69 void MessageReader::OnRead(int result) { |
| 63 DCHECK(CalledOnValidThread()); | 70 DCHECK(CalledOnValidThread()); |
| 64 DCHECK(read_pending_); | 71 DCHECK(read_pending_); |
| 65 read_pending_ = false; | 72 read_pending_ = false; |
| 66 | 73 |
| 67 if (!closed_) { | 74 if (!closed_) { |
| 68 HandleReadResult(result); | 75 bool read_again; |
| 69 DoRead(); | 76 HandleReadResult(result, &read_again); |
| 77 if (read_again) |
| 78 DoRead(); |
| 70 } | 79 } |
| 71 } | 80 } |
| 72 | 81 |
| 73 void MessageReader::HandleReadResult(int result) { | 82 void MessageReader::HandleReadResult(int result, bool* read_again) { |
| 74 DCHECK(CalledOnValidThread()); | 83 DCHECK(CalledOnValidThread()); |
| 75 if (closed_) | 84 if (closed_) |
| 76 return; | 85 return; |
| 77 | 86 |
| 87 *read_again = false; |
| 88 |
| 78 if (result > 0) { | 89 if (result > 0) { |
| 79 OnDataReceived(read_buffer_.get(), result); | 90 OnDataReceived(read_buffer_.get(), result); |
| 91 *read_again = true; |
| 80 } else if (result == net::ERR_IO_PENDING) { | 92 } else if (result == net::ERR_IO_PENDING) { |
| 81 read_pending_ = true; | 93 read_pending_ = true; |
| 82 } else { | 94 } else { |
| 83 if (result != net::ERR_CONNECTION_CLOSED) { | 95 DCHECK_LT(result, 0); |
| 84 LOG(ERROR) << "Read() returned error " << result; | 96 |
| 85 } | |
| 86 // Stop reading after any error. | 97 // Stop reading after any error. |
| 87 closed_ = true; | 98 closed_ = true; |
| 99 |
| 100 LOG(ERROR) << "Read() returned error " << result; |
| 101 read_failed_callback_.Run(result); |
| 88 } | 102 } |
| 89 } | 103 } |
| 90 | 104 |
| 91 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { | 105 void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { |
| 92 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
| 93 message_decoder_.AddData(data, data_size); | 107 message_decoder_.AddData(data, data_size); |
| 94 | 108 |
| 95 // Get list of all new messages first, and then call the callback | 109 // Get list of all new messages first, and then call the callback |
| 96 // for all of them. | 110 // for all of them. |
| 97 while (true) { | 111 while (true) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 119 DCHECK(CalledOnValidThread()); | 133 DCHECK(CalledOnValidThread()); |
| 120 pending_messages_--; | 134 pending_messages_--; |
| 121 DCHECK_GE(pending_messages_, 0); | 135 DCHECK_GE(pending_messages_, 0); |
| 122 | 136 |
| 123 // Start next read if necessary. | 137 // Start next read if necessary. |
| 124 DoRead(); | 138 DoRead(); |
| 125 } | 139 } |
| 126 | 140 |
| 127 } // namespace protocol | 141 } // namespace protocol |
| 128 } // namespace remoting | 142 } // namespace remoting |
| OLD | NEW |