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 #ifndef REMOTING_PROTOCOL_MESSAGE_READER_H_
6 #define REMOTING_PROTOCOL_MESSAGE_READER_H_
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/completion_callback.h"
14 #include "remoting/base/compound_buffer.h"
15 #include "remoting/protocol/message_decoder.h"
25 // MessageReader reads data from the socket asynchronously and calls
26 // callback for each message it receives. It stops calling the
27 // callback as soon as the socket is closed, so the socket should
28 // always be closed before the callback handler is destroyed.
30 // In order to throttle the stream, MessageReader doesn't try to read
31 // new data from the socket until all previously received messages are
32 // processed by the receiver (|done_task| is called for each message).
33 // It is still possible that the MessageReceivedCallback is called
34 // twice (so that there is more than one outstanding message),
35 // e.g. when we the sender sends multiple messages in one TCP packet.
36 class MessageReader
: public base::NonThreadSafe
{
38 typedef base::Callback
<void(scoped_ptr
<CompoundBuffer
>, const base::Closure
&)>
39 MessageReceivedCallback
;
42 virtual ~MessageReader();
44 // Initialize the MessageReader with a socket. If a message is received
45 // |callback| is called.
46 void Init(net::Socket
* socket
, const MessageReceivedCallback
& callback
);
50 void OnRead(int result
);
51 void HandleReadResult(int result
);
52 void OnDataReceived(net::IOBuffer
* data
, int data_size
);
53 void RunCallback(scoped_ptr
<CompoundBuffer
> message
);
58 // Set to true, when we have a socket read pending, and expecting
59 // OnRead() to be called when new data is received.
62 // Number of messages that we received, but haven't finished
63 // processing yet, i.e. |done_task| hasn't been called for these
65 int pending_messages_
;
68 scoped_refptr
<net::IOBuffer
> read_buffer_
;
70 MessageDecoder message_decoder_
;
72 // Callback is called when a message is received.
73 MessageReceivedCallback message_received_callback_
;
75 base::WeakPtrFactory
<MessageReader
> weak_factory_
;
77 DISALLOW_COPY_AND_ASSIGN(MessageReader
);
80 // Version of MessageReader for protocol buffer messages, that parses
81 // each incoming message.
83 class ProtobufMessageReader
{
85 // The callback that is called when a new message is received. |done_task|
86 // must be called by the callback when it's done processing the |message|.
87 typedef typename
base::Callback
<void(scoped_ptr
<T
> message
,
88 const base::Closure
& done_task
)>
89 MessageReceivedCallback
;
91 ProtobufMessageReader() { };
92 ~ProtobufMessageReader() { };
94 void Init(net::Socket
* socket
, const MessageReceivedCallback
& callback
) {
95 DCHECK(!callback
.is_null());
96 message_received_callback_
= callback
;
97 message_reader_
.reset(new MessageReader());
98 message_reader_
->Init(
99 socket
, base::Bind(&ProtobufMessageReader
<T
>::OnNewData
,
100 base::Unretained(this)));
104 void OnNewData(scoped_ptr
<CompoundBuffer
> buffer
,
105 const base::Closure
& done_task
) {
106 scoped_ptr
<T
> message(new T());
107 CompoundBufferInputStream
stream(buffer
.get());
108 bool ret
= message
->ParseFromZeroCopyStream(&stream
);
110 LOG(WARNING
) << "Received message that is not a valid protocol buffer.";
112 DCHECK_EQ(stream
.position(), buffer
->total_bytes());
113 message_received_callback_
.Run(message
.Pass(), done_task
);
117 scoped_ptr
<MessageReader
> message_reader_
;
118 MessageReceivedCallback message_received_callback_
;
121 } // namespace protocol
122 } // namespace remoting
124 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_