| 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/message_reader.h" | 5 #include "remoting/protocol/message_reader.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | |
| 8 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
| 9 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 10 #include "net/socket/socket.h" | 9 #include "net/socket/socket.h" |
| 11 #include "remoting/base/compound_buffer.h" | 10 #include "remoting/base/compound_buffer.h" |
| 12 #include "remoting/proto/internal.pb.h" | 11 #include "remoting/proto/internal.pb.h" |
| 13 | 12 |
| 14 namespace remoting { | 13 namespace remoting { |
| 15 namespace protocol { | 14 namespace protocol { |
| 16 | 15 |
| 17 static const int kReadBufferSize = 4096; | 16 static const int kReadBufferSize = 4096; |
| 18 | 17 |
| 19 MessageReader::MessageReader() | 18 MessageReader::MessageReader() |
| 20 : socket_(NULL), | 19 : socket_(NULL), |
| 21 message_loop_(NULL), | |
| 22 read_pending_(false), | 20 read_pending_(false), |
| 23 pending_messages_(0), | 21 pending_messages_(0), |
| 24 closed_(false), | 22 closed_(false), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST( | 23 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 26 read_callback_(this, &MessageReader::OnRead)) { | 24 read_callback_(this, &MessageReader::OnRead)) { |
| 27 } | 25 } |
| 28 | 26 |
| 29 MessageReader::~MessageReader() { | 27 MessageReader::~MessageReader() { |
| 30 CHECK_EQ(pending_messages_, 0); | 28 CHECK_EQ(pending_messages_, 0); |
| 31 } | 29 } |
| 32 | 30 |
| 33 void MessageReader::Init(net::Socket* socket, | 31 void MessageReader::Init(net::Socket* socket, |
| 34 MessageReceivedCallback* callback) { | 32 MessageReceivedCallback* callback) { |
| 35 message_received_callback_.reset(callback); | 33 message_received_callback_.reset(callback); |
| 36 DCHECK(socket); | 34 DCHECK(socket); |
| 37 socket_ = socket; | 35 socket_ = socket; |
| 38 message_loop_ = MessageLoop::current(); | |
| 39 DoRead(); | 36 DoRead(); |
| 40 } | 37 } |
| 41 | 38 |
| 42 void MessageReader::DoRead() { | 39 void MessageReader::DoRead() { |
| 43 // Don't try to read again if there is another read pending or we | 40 // Don't try to read again if there is another read pending or we |
| 44 // have messages that we haven't finished processing yet. | 41 // have messages that we haven't finished processing yet. |
| 45 while (!closed_ && !read_pending_ && pending_messages_ == 0) { | 42 while (!closed_ && !read_pending_ && pending_messages_ == 0) { |
| 46 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 43 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 47 int result = socket_->Read( | 44 int result = socket_->Read( |
| 48 read_buffer_, kReadBufferSize, &read_callback_); | 45 read_buffer_, kReadBufferSize, &read_callback_); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 this, &MessageReader::OnMessageDone, *it)); | 95 this, &MessageReader::OnMessageDone, *it)); |
| 99 } | 96 } |
| 100 } | 97 } |
| 101 | 98 |
| 102 void MessageReader::OnMessageDone(CompoundBuffer* message) { | 99 void MessageReader::OnMessageDone(CompoundBuffer* message) { |
| 103 delete message; | 100 delete message; |
| 104 ProcessDoneEvent(); | 101 ProcessDoneEvent(); |
| 105 } | 102 } |
| 106 | 103 |
| 107 void MessageReader::ProcessDoneEvent() { | 104 void MessageReader::ProcessDoneEvent() { |
| 108 if (MessageLoop::current() != message_loop_) { | |
| 109 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | |
| 110 this, &MessageReader::ProcessDoneEvent)); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 pending_messages_--; | 105 pending_messages_--; |
| 115 DCHECK_GE(pending_messages_, 0); | 106 DCHECK_GE(pending_messages_, 0); |
| 116 | 107 |
| 117 DoRead(); // Start next read if neccessary. | 108 DoRead(); // Start next read if neccessary. |
| 118 } | 109 } |
| 119 | 110 |
| 120 } // namespace protocol | 111 } // namespace protocol |
| 121 } // namespace remoting | 112 } // namespace remoting |
| OLD | NEW |