blob: 12842737f2f1eac697f47fe521a3aa17a0e39311 [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),
[email protected]aa46ba52013-04-27 00:00:4429 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]f9d8a772013-06-01 04:33:1751 read_buffer_.get(),
52 kReadBufferSize,
[email protected]eeda0312012-08-23 21:37:0453 base::Bind(&MessageReader::OnRead, weak_factory_.GetWeakPtr()));
[email protected]4d10ede2010-10-28 18:43:3754 HandleReadResult(result);
[email protected]4d10ede2010-10-28 18:43:3755 }
56}
57
58void MessageReader::OnRead(int result) {
[email protected]eeda0312012-08-23 21:37:0459 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:5660 DCHECK(read_pending_);
61 read_pending_ = false;
62
[email protected]4d10ede2010-10-28 18:43:3763 if (!closed_) {
64 HandleReadResult(result);
65 DoRead();
66 }
67}
68
69void MessageReader::HandleReadResult(int result) {
[email protected]eeda0312012-08-23 21:37:0470 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:5671 if (closed_)
72 return;
73
[email protected]4d10ede2010-10-28 18:43:3774 if (result > 0) {
[email protected]f9d8a772013-06-01 04:33:1775 OnDataReceived(read_buffer_.get(), result);
[email protected]c712866e2012-07-14 03:55:2176 } else if (result == net::ERR_IO_PENDING) {
77 read_pending_ = true;
[email protected]4d10ede2010-10-28 18:43:3778 } else {
[email protected]c712866e2012-07-14 03:55:2179 if (result != net::ERR_CONNECTION_CLOSED) {
[email protected]4d10ede2010-10-28 18:43:3780 LOG(ERROR) << "Read() returned error " << result;
[email protected]051916e62011-01-14 21:58:0181 }
[email protected]c712866e2012-07-14 03:55:2182 // Stop reading after any error.
83 closed_ = true;
[email protected]051916e62011-01-14 21:58:0184 }
85}
86
87void MessageReader::OnDataReceived(net::IOBuffer* data, int data_size) {
[email protected]eeda0312012-08-23 21:37:0488 DCHECK(CalledOnValidThread());
[email protected]051916e62011-01-14 21:58:0189 message_decoder_.AddData(data, data_size);
90
[email protected]6852d7d2011-01-22 02:34:5691 // Get list of all new messages first, and then call the callback
92 // for all of them.
[email protected]051916e62011-01-14 21:58:0193 while (true) {
[email protected]6852d7d2011-01-22 02:34:5694 CompoundBuffer* buffer = message_decoder_.GetNextMessage();
95 if (!buffer)
[email protected]051916e62011-01-14 21:58:0196 break;
[email protected]6e60bb02012-09-13 03:44:3897 pending_messages_++;
98 base::ThreadTaskRunnerHandle::Get()->PostTask(
99 FROM_HERE,
100 base::Bind(&MessageReader::RunCallback,
101 weak_factory_.GetWeakPtr(),
sergeyu2d690882014-10-01 02:36:43102 base::Passed(make_scoped_ptr(buffer))));
[email protected]4d10ede2010-10-28 18:43:37103 }
[email protected]6e60bb02012-09-13 03:44:38104}
[email protected]6852d7d2011-01-22 02:34:56105
[email protected]6e60bb02012-09-13 03:44:38106void MessageReader::RunCallback(scoped_ptr<CompoundBuffer> message) {
107 message_received_callback_.Run(
108 message.Pass(), base::Bind(&MessageReader::OnMessageDone,
109 weak_factory_.GetWeakPtr()));
[email protected]6852d7d2011-01-22 02:34:56110}
111
[email protected]eeda0312012-08-23 21:37:04112void MessageReader::OnMessageDone() {
113 DCHECK(CalledOnValidThread());
[email protected]6852d7d2011-01-22 02:34:56114 pending_messages_--;
115 DCHECK_GE(pending_messages_, 0);
116
[email protected]6e60bb02012-09-13 03:44:38117 // Start next read if necessary.
118 DoRead();
[email protected]4d10ede2010-10-28 18:43:37119}
120
[email protected]2c229682010-12-02 20:23:35121} // namespace protocol
[email protected]4d10ede2010-10-28 18:43:37122} // namespace remoting