[email protected] | 9302fce | 2012-03-28 03:57:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/protocol/message_reader.h" |
| 6 | |
[email protected] | 9241e6b | 2011-08-24 02:25:27 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/callback.h" |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 10 | #include "base/location.h" |
[email protected] | 0a4efa7 | 2012-05-24 04:15:45 | [diff] [blame] | 11 | #include "base/thread_task_runner_handle.h" |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame^] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 13 | #include "net/base/io_buffer.h" |
| 14 | #include "net/base/net_errors.h" |
| 15 | #include "net/socket/socket.h" |
[email protected] | b3c0340 | 2010-11-16 01:27:46 | [diff] [blame] | 16 | #include "remoting/base/compound_buffer.h" |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 17 | #include "remoting/proto/internal.pb.h" |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 18 | |
| 19 | namespace remoting { |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 20 | namespace protocol { |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 21 | |
| 22 | static const int kReadBufferSize = 4096; |
| 23 | |
| 24 | MessageReader::MessageReader() |
| 25 | : socket_(NULL), |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 26 | read_pending_(false), |
| 27 | pending_messages_(0), |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 28 | closed_(false), |
| 29 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 32 | void MessageReader::Init(net::Socket* socket, |
[email protected] | 9e2a313 | 2011-10-07 05:07:40 | [diff] [blame] | 33 | const MessageReceivedCallback& callback) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 34 | DCHECK(CalledOnValidThread()); |
[email protected] | 9e2a313 | 2011-10-07 05:07:40 | [diff] [blame] | 35 | message_received_callback_ = callback; |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 36 | DCHECK(socket); |
| 37 | socket_ = socket; |
| 38 | DoRead(); |
| 39 | } |
| 40 | |
[email protected] | 256d273 | 2012-04-24 23:26:37 | [diff] [blame] | 41 | MessageReader::~MessageReader() { |
[email protected] | 256d273 | 2012-04-24 23:26:37 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 44 | void MessageReader::DoRead() { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 45 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 46 | // Don't try to read again if there is another read pending or we |
| 47 | // have messages that we haven't finished processing yet. |
| 48 | while (!closed_ && !read_pending_ && pending_messages_ == 0) { |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 49 | read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 50 | int result = socket_->Read( |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 51 | read_buffer_, kReadBufferSize, |
| 52 | base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 53 | HandleReadResult(result); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void MessageReader::OnRead(int result) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 58 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 59 | DCHECK(read_pending_); |
| 60 | read_pending_ = false; |
| 61 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 62 | if (!closed_) { |
| 63 | HandleReadResult(result); |
| 64 | DoRead(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void MessageReader::HandleReadResult(int result) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 69 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 70 | if (closed_) |
| 71 | return; |
| 72 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 73 | if (result > 0) { |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 74 | OnDataReceived(read_buffer_, result); |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 75 | } else if (result == net::ERR_IO_PENDING) { |
| 76 | read_pending_ = true; |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 77 | } else { |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 78 | if (result != net::ERR_CONNECTION_CLOSED) { |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 79 | LOG(ERROR) << "Read() returned error " << result; |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 80 | } |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 81 | // Stop reading after any error. |
| 82 | closed_ = true; |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 87 | DCHECK(CalledOnValidThread()); |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 88 | message_decoder_.AddData(data, data_size); |
| 89 | |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 90 | // Get list of all new messages first, and then call the callback |
| 91 | // for all of them. |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 92 | while (true) { |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 93 | CompoundBuffer* buffer = message_decoder_.GetNextMessage(); |
| 94 | if (!buffer) |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 95 | break; |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame^] | 96 | pending_messages_++; |
| 97 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 98 | FROM_HERE, |
| 99 | base::Bind(&MessageReader::RunCallback, |
| 100 | weak_factory_.GetWeakPtr(), |
| 101 | base::Passed(scoped_ptr<CompoundBuffer>(buffer)))); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 102 | } |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame^] | 103 | } |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 104 | |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame^] | 105 | void MessageReader::RunCallback(scoped_ptr<CompoundBuffer> message) { |
| 106 | message_received_callback_.Run( |
| 107 | message.Pass(), base::Bind(&MessageReader::OnMessageDone, |
| 108 | weak_factory_.GetWeakPtr())); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 111 | void MessageReader::OnMessageDone() { |
| 112 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 113 | pending_messages_--; |
| 114 | DCHECK_GE(pending_messages_, 0); |
| 115 | |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame^] | 116 | // Start next read if necessary. |
| 117 | DoRead(); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 120 | } // namespace protocol |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 121 | } // namespace remoting |