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 "remoting/base/compound_buffer.h"
16 #include "remoting/proto/internal.pb.h"
17 #include "remoting/protocol/p2p_stream_socket.h"
22 static const int kReadBufferSize
= 4096;
24 MessageReader::MessageReader()
32 MessageReader::~MessageReader() {
35 void MessageReader::SetMessageReceivedCallback(
36 const MessageReceivedCallback
& callback
) {
37 DCHECK(CalledOnValidThread());
38 message_received_callback_
= callback
;
41 void MessageReader::StartReading(
42 P2PStreamSocket
* socket
,
43 const ReadFailedCallback
& read_failed_callback
) {
44 DCHECK(CalledOnValidThread());
46 DCHECK(!read_failed_callback
.is_null());
49 read_failed_callback_
= read_failed_callback
;
53 void MessageReader::DoRead() {
54 DCHECK(CalledOnValidThread());
55 // Don't try to read again if there is another read pending or we
56 // have messages that we haven't finished processing yet.
57 bool read_succeeded
= true;
58 while (read_succeeded
&& !closed_
&& !read_pending_
&&
59 pending_messages_
== 0) {
60 read_buffer_
= new net::IOBuffer(kReadBufferSize
);
61 int result
= socket_
->Read(
64 base::Bind(&MessageReader::OnRead
, weak_factory_
.GetWeakPtr()));
66 HandleReadResult(result
, &read_succeeded
);
70 void MessageReader::OnRead(int result
) {
71 DCHECK(CalledOnValidThread());
72 DCHECK(read_pending_
);
73 read_pending_
= false;
77 HandleReadResult(result
, &read_succeeded
);
83 void MessageReader::HandleReadResult(int result
, bool* read_succeeded
) {
84 DCHECK(CalledOnValidThread());
88 *read_succeeded
= true;
91 OnDataReceived(read_buffer_
.get(), result
);
92 *read_succeeded
= true;
93 } else if (result
== net::ERR_IO_PENDING
) {
98 // Stop reading after any error.
100 *read_succeeded
= false;
102 LOG(ERROR
) << "Read() returned error " << result
;
103 read_failed_callback_
.Run(result
);
107 void MessageReader::OnDataReceived(net::IOBuffer
* data
, int data_size
) {
108 DCHECK(CalledOnValidThread());
109 message_decoder_
.AddData(data
, data_size
);
111 // Get list of all new messages first, and then call the callback
114 CompoundBuffer
* buffer
= message_decoder_
.GetNextMessage();
118 base::ThreadTaskRunnerHandle::Get()->PostTask(
120 base::Bind(&MessageReader::RunCallback
,
121 weak_factory_
.GetWeakPtr(),
122 base::Passed(make_scoped_ptr(buffer
))));
126 void MessageReader::RunCallback(scoped_ptr
<CompoundBuffer
> message
) {
127 if (!message_received_callback_
.is_null()){
128 message_received_callback_
.Run(
130 base::Bind(&MessageReader::OnMessageDone
, weak_factory_
.GetWeakPtr()));
134 void MessageReader::OnMessageDone() {
135 DCHECK(CalledOnValidThread());
137 DCHECK_GE(pending_messages_
, 0);
139 // Start next read if necessary.
143 } // namespace protocol
144 } // namespace remoting