| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/location.h" | 9 #include "base/location.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 closed_(false), | 25 closed_(false), |
| 26 ALLOW_THIS_IN_INITIALIZER_LIST( | 26 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 27 read_callback_(this, &MessageReader::OnRead)) { | 27 read_callback_(this, &MessageReader::OnRead)) { |
| 28 } | 28 } |
| 29 | 29 |
| 30 MessageReader::~MessageReader() { | 30 MessageReader::~MessageReader() { |
| 31 CHECK_EQ(pending_messages_, 0); | 31 CHECK_EQ(pending_messages_, 0); |
| 32 } | 32 } |
| 33 | 33 |
| 34 void MessageReader::Init(net::Socket* socket, | 34 void MessageReader::Init(net::Socket* socket, |
| 35 MessageReceivedCallback* callback) { | 35 const MessageReceivedCallback& callback) { |
| 36 message_received_callback_.reset(callback); | 36 message_received_callback_ = callback; |
| 37 DCHECK(socket); | 37 DCHECK(socket); |
| 38 socket_ = socket; | 38 socket_ = socket; |
| 39 DoRead(); | 39 DoRead(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void MessageReader::DoRead() { | 42 void MessageReader::DoRead() { |
| 43 // Don't try to read again if there is another read pending or we | 43 // Don't try to read again if there is another read pending or we |
| 44 // have messages that we haven't finished processing yet. | 44 // have messages that we haven't finished processing yet. |
| 45 while (!closed_ && !read_pending_ && pending_messages_ == 0) { | 45 while (!closed_ && !read_pending_ && pending_messages_ == 0) { |
| 46 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 46 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 break; | 89 break; |
| 90 new_messages.push_back(buffer); | 90 new_messages.push_back(buffer); |
| 91 } | 91 } |
| 92 | 92 |
| 93 pending_messages_ += new_messages.size(); | 93 pending_messages_ += new_messages.size(); |
| 94 | 94 |
| 95 // TODO(lambroslambrou): MessageLoopProxy::current() will not work from the | 95 // TODO(lambroslambrou): MessageLoopProxy::current() will not work from the |
| 96 // plugin thread if this code is compiled into a separate binary. Fix this. | 96 // plugin thread if this code is compiled into a separate binary. Fix this. |
| 97 for (std::vector<CompoundBuffer*>::iterator it = new_messages.begin(); | 97 for (std::vector<CompoundBuffer*>::iterator it = new_messages.begin(); |
| 98 it != new_messages.end(); ++it) { | 98 it != new_messages.end(); ++it) { |
| 99 message_received_callback_->Run(*it, NewRunnableMethod( | 99 message_received_callback_.Run(*it, base::Bind( |
| 100 this, &MessageReader::OnMessageDone, *it, | 100 &MessageReader::OnMessageDone, this, |
| 101 base::MessageLoopProxy::current())); | 101 *it, base::MessageLoopProxy::current())); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 void MessageReader::OnMessageDone( | 105 void MessageReader::OnMessageDone( |
| 106 CompoundBuffer* message, | 106 CompoundBuffer* message, |
| 107 scoped_refptr<base::MessageLoopProxy> message_loop) { | 107 scoped_refptr<base::MessageLoopProxy> message_loop) { |
| 108 if (!message_loop->BelongsToCurrentThread()) { | 108 if (!message_loop->BelongsToCurrentThread()) { |
| 109 message_loop->PostTask( | 109 message_loop->PostTask( |
| 110 FROM_HERE, | 110 FROM_HERE, |
| 111 base::Bind(&MessageReader::OnMessageDone, this, message, message_loop)); | 111 base::Bind(&MessageReader::OnMessageDone, this, message, message_loop)); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 delete message; | 114 delete message; |
| 115 ProcessDoneEvent(); | 115 ProcessDoneEvent(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void MessageReader::ProcessDoneEvent() { | 118 void MessageReader::ProcessDoneEvent() { |
| 119 pending_messages_--; | 119 pending_messages_--; |
| 120 DCHECK_GE(pending_messages_, 0); | 120 DCHECK_GE(pending_messages_, 0); |
| 121 | 121 |
| 122 DoRead(); // Start next read if neccessary. | 122 DoRead(); // Start next read if neccessary. |
| 123 } | 123 } |
| 124 | 124 |
| 125 } // namespace protocol | 125 } // namespace protocol |
| 126 } // namespace remoting | 126 } // namespace remoting |
| OLD | NEW |