1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/protocol/message_reader.h"
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/location.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/single_thread_task_runner.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h"
16 #include "remoting/base/compound_buffer.h"
17 #include "remoting/proto/internal.pb.h"
22 static const int kReadBufferSize
= 4096;
24 MessageReader::MessageReader()
32 void MessageReader::Init(net::Socket
* socket
,
33 const MessageReceivedCallback
& callback
) {
34 DCHECK(CalledOnValidThread());
35 message_received_callback_
= callback
;
41 MessageReader::~MessageReader() {
44 void MessageReader::DoRead() {
45 DCHECK(CalledOnValidThread());
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) {
49 read_buffer_
= new net::IOBuffer(kReadBufferSize
);
50 int result
= socket_
->Read(
53 base::Bind(&MessageReader::OnRead
, weak_factory_
.GetWeakPtr()));
54 HandleReadResult(result
);
58 void MessageReader::OnRead(int result
) {
59 DCHECK(CalledOnValidThread());
60 DCHECK(read_pending_
);
61 read_pending_
= false;
64 HandleReadResult(result
);
69 void MessageReader::HandleReadResult(int result
) {
70 DCHECK(CalledOnValidThread());
75 OnDataReceived(read_buffer_
.get(), result
);
76 } else if (result
== net::ERR_IO_PENDING
) {
79 if (result
!= net::ERR_CONNECTION_CLOSED
) {
80 LOG(ERROR
) << "Read() returned error " << result
;
82 // Stop reading after any error.
87 void MessageReader::OnDataReceived(net::IOBuffer
* data
, int data_size
) {
88 DCHECK(CalledOnValidThread());
89 message_decoder_
.AddData(data
, data_size
);
91 // Get list of all new messages first, and then call the callback
94 CompoundBuffer
* buffer
= message_decoder_
.GetNextMessage();
98 base::ThreadTaskRunnerHandle::Get()->PostTask(
100 base::Bind(&MessageReader::RunCallback
,
101 weak_factory_
.GetWeakPtr(),
102 base::Passed(scoped_ptr
<CompoundBuffer
>(buffer
))));
106 void MessageReader::RunCallback(scoped_ptr
<CompoundBuffer
> message
) {
107 message_received_callback_
.Run(
108 message
.Pass(), base::Bind(&MessageReader::OnMessageDone
,
109 weak_factory_
.GetWeakPtr()));
112 void MessageReader::OnMessageDone() {
113 DCHECK(CalledOnValidThread());
115 DCHECK_GE(pending_messages_
, 0);
117 // Start next read if necessary.
121 } // namespace protocol
122 } // namespace remoting