blob: 926b05870ed3ded50b67c9368c3041f0f5952b90 [file] [log] [blame]
[email protected]9302fce2012-03-28 03:57:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4d10ede2010-10-28 18:43:372// 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]9241e6b2011-08-24 02:25:277#include "base/bind.h"
8#include "base/callback.h"
[email protected]eeda0312012-08-23 21:37:049#include "base/compiler_specific.h"
[email protected]c62dd9d2011-09-21 18:05:4110#include "base/location.h"
[email protected]0a4efa72012-05-24 04:15:4511#include "base/thread_task_runner_handle.h"
[email protected]6e60bb02012-09-13 03:44:3812#include "base/single_thread_task_runner.h"
[email protected]4d10ede2010-10-28 18:43:3713#include "net/base/io_buffer.h"
14#include "net/base/net_errors.h"
15#include "net/socket/socket.h"
[email protected]b3c03402010-11-16 01:27:4616#include "remoting/base/compound_buffer.h"
[email protected]4d10ede2010-10-28 18:43:3717#include "remoting/proto/internal.pb.h"
[email protected]4d10ede2010-10-28 18:43:3718
19namespace remoting {
[email protected]2c229682010-12-02 20:23:3520namespace protocol {
[email protected]4d10ede2010-10-28 18:43:3721
22static const int kReadBufferSize = 4096;
23
24MessageReader::MessageReader()
25 : socket_(NULL),
[email protected]6852d7d2011-01-22 02:34:5626 read_pending_(false),
27 pending_messages_(0),
[email protected]eeda0312012-08-23 21:37:0428 closed_(false),
29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]4d10ede2010-10-28 18:43:3730}
31
[email protected]051916e62011-01-14 21:58:0132void MessageReader::Init(net::Socket* socket,
[email protected]9e2a3132011-10-07 05:07:4033 const MessageReceivedCallback& callback) {
[email protected]eeda0312012-08-23 21:37:0434 DCHECK(CalledOnValidThread());
[email protected]9e2a3132011-10-07 05:07:4035 message_received_callback_ = callback;
[email protected]4d10ede2010-10-28 18:43:3736 DCHECK(socket);
37 socket_ = socket;
38 DoRead();
39}
40
[email protected]256d2732012-04-24 23:26:3741MessageReader::~MessageReader() {
[email protected]256d2732012-04-24 23:26:3742}
43
[email protected]4d10ede2010-10-28 18:43:3744void MessageReader::DoRead() {
[email protected]eeda0312012-08-23 21:37:0445 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:5646 // 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]4d10ede2010-10-28 18:43:3749 read_buffer_ = new net::IOBuffer(kReadBufferSize);
50 int result = socket_->Read(
[email protected]eeda0312012-08-23 21:37:0451 read_buffer_, kReadBufferSize,
52 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr()));
[email protected]4d10ede2010-10-28 18:43:3753 HandleReadResult(result);
[email protected]4d10ede2010-10-28 18:43:3754 }
55}
56
57void MessageReader::OnRead(int result) {
[email protected]eeda0312012-08-23 21:37:0458 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:5659 DCHECK(read_pending_);
60 read_pending_ = false;
61
[email protected]4d10ede2010-10-28 18:43:3762 if (!closed_) {
63 HandleReadResult(result);
64 DoRead();
65 }
66}
67
68void MessageReader::HandleReadResult(int result) {
[email protected]eeda0312012-08-23 21:37:0469 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:5670 if (closed_)
71 return;
72
[email protected]4d10ede2010-10-28 18:43:3773 if (result > 0) {
[email protected]051916e62011-01-14 21:58:0174 OnDataReceived(read_buffer_, result);
[email protected]c712866e2012-07-14 03:55:2175 } else if (result == net::ERR_IO_PENDING) {
76 read_pending_ = true;
[email protected]4d10ede2010-10-28 18:43:3777 } else {
[email protected]c712866e2012-07-14 03:55:2178 if (result != net::ERR_CONNECTION_CLOSED) {
[email protected]4d10ede2010-10-28 18:43:3779 LOG(ERROR) << "Read() returned error " << result;
[email protected]051916e62011-01-14 21:58:0180 }
[email protected]c712866e2012-07-14 03:55:2181 // Stop reading after any error.
82 closed_ = true;
[email protected]051916e62011-01-14 21:58:0183 }
84}
85
86void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) {
[email protected]eeda0312012-08-23 21:37:0487 DCHECK(CalledOnValidThread());
[email protected]051916e62011-01-14 21:58:0188 message_decoder_.AddData(data, data_size);
89
[email protected]6852d7d2011-01-22 02:34:5690 // Get list of all new messages first, and then call the callback
91 // for all of them.
[email protected]051916e62011-01-14 21:58:0192 while (true) {
[email protected]6852d7d2011-01-22 02:34:5693 CompoundBuffer* buffer = message_decoder_.GetNextMessage();
94 if (!buffer)
[email protected]051916e62011-01-14 21:58:0195 break;
[email protected]6e60bb02012-09-13 03:44:3896 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]4d10ede2010-10-28 18:43:37102 }
[email protected]6e60bb02012-09-13 03:44:38103}
[email protected]6852d7d2011-01-22 02:34:56104
[email protected]6e60bb02012-09-13 03:44:38105void MessageReader::RunCallback(scoped_ptr<CompoundBuffer> message) {
106 message_received_callback_.Run(
107 message.Pass(), base::Bind(&MessageReader::OnMessageDone,
108 weak_factory_.GetWeakPtr()));
[email protected]6852d7d2011-01-22 02:34:56109}
110
[email protected]eeda0312012-08-23 21:37:04111void MessageReader::OnMessageDone() {
112 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:56113 pending_messages_--;
114 DCHECK_GE(pending_messages_, 0);
115
[email protected]6e60bb02012-09-13 03:44:38116 // Start next read if necessary.
117 DoRead();
[email protected]4d10ede2010-10-28 18:43:37118}
119
[email protected]2c229682010-12-02 20:23:35120} // namespace protocol
[email protected]4d10ede2010-10-28 18:43:37121} // namespace remoting