[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), |
[email protected] | aa46ba5 | 2013-04-27 00:00:44 | [diff] [blame] | 29 | 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] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame] | 51 | read_buffer_.get(), |
| 52 | kReadBufferSize, |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 53 | base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr())); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 54 | HandleReadResult(result); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void MessageReader::OnRead(int result) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 59 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 60 | DCHECK(read_pending_); |
| 61 | read_pending_ = false; |
| 62 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 63 | if (!closed_) { |
| 64 | HandleReadResult(result); |
| 65 | DoRead(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void MessageReader::HandleReadResult(int result) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 70 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 71 | if (closed_) |
| 72 | return; |
| 73 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 74 | if (result > 0) { |
[email protected] | f9d8a77 | 2013-06-01 04:33:17 | [diff] [blame] | 75 | OnDataReceived(read_buffer_.get(), result); |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 76 | } else if (result == net::ERR_IO_PENDING) { |
| 77 | read_pending_ = true; |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 78 | } else { |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 79 | if (result != net::ERR_CONNECTION_CLOSED) { |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 80 | LOG(ERROR) << "Read() returned error " << result; |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 81 | } |
[email protected] | c712866e | 2012-07-14 03:55:21 | [diff] [blame] | 82 | // Stop reading after any error. |
| 83 | closed_ = true; |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) { |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 88 | DCHECK(CalledOnValidThread()); |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 89 | message_decoder_.AddData(data, data_size); |
| 90 | |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 91 | // Get list of all new messages first, and then call the callback |
| 92 | // for all of them. |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 93 | while (true) { |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 94 | CompoundBuffer* buffer = message_decoder_.GetNextMessage(); |
| 95 | if (!buffer) |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 96 | break; |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame] | 97 | pending_messages_++; |
| 98 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 99 | FROM_HERE, |
| 100 | base::Bind(&MessageReader::RunCallback, |
| 101 | weak_factory_.GetWeakPtr(), |
sergeyu | 2d69088 | 2014-10-01 02:36:43 | [diff] [blame] | 102 | base::Passed(make_scoped_ptr(buffer)))); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 103 | } |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame] | 104 | } |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 105 | |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame] | 106 | void MessageReader::RunCallback(scoped_ptr<CompoundBuffer> message) { |
| 107 | message_received_callback_.Run( |
| 108 | message.Pass(), base::Bind(&MessageReader::OnMessageDone, |
| 109 | weak_factory_.GetWeakPtr())); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | eeda031 | 2012-08-23 21:37:04 | [diff] [blame] | 112 | void MessageReader::OnMessageDone() { |
| 113 | DCHECK(CalledOnValidThread()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 114 | pending_messages_--; |
| 115 | DCHECK_GE(pending_messages_, 0); |
| 116 | |
[email protected] | 6e60bb0 | 2012-09-13 03:44:38 | [diff] [blame] | 117 | // Start next read if necessary. |
| 118 | DoRead(); |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 121 | } // namespace protocol |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 122 | } // namespace remoting |