Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: remoting/protocol/message_reader.h

Issue 10870021: Make MessageReader class not ref-counted. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | remoting/protocol/message_reader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef REMOTING_PROTOCOL_MESSAGE_READER_H_ 5 #ifndef REMOTING_PROTOCOL_MESSAGE_READER_H_
6 #define REMOTING_PROTOCOL_MESSAGE_READER_H_ 6 #define REMOTING_PROTOCOL_MESSAGE_READER_H_
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop_proxy.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/completion_callback.h" 13 #include "net/base/completion_callback.h"
14 #include "remoting/base/compound_buffer.h" 14 #include "remoting/base/compound_buffer.h"
15 #include "remoting/protocol/message_decoder.h" 15 #include "remoting/protocol/message_decoder.h"
16 16
17 class MessageLoop;
18
19 namespace net { 17 namespace net {
20 class IOBuffer; 18 class IOBuffer;
21 class Socket; 19 class Socket;
22 } // namespace net 20 } // namespace net
23 21
24 namespace remoting { 22 namespace remoting {
25 namespace protocol { 23 namespace protocol {
26 24
27 // MessageReader reads data from the socket asynchronously and calls 25 // MessageReader reads data from the socket asynchronously and calls
28 // callback for each message it receives. It stops calling the 26 // callback for each message it receives. It stops calling the
29 // callback as soon as the socket is closed, so the socket should 27 // callback as soon as the socket is closed, so the socket should
30 // always be closed before the callback handler is destroyed. 28 // always be closed before the callback handler is destroyed.
31 // 29 //
32 // In order to throttle the stream, MessageReader doesn't try to read 30 // In order to throttle the stream, MessageReader doesn't try to read
33 // new data from the socket until all previously received messages are 31 // new data from the socket until all previously received messages are
34 // processed by the receiver (|done_task| is called for each message). 32 // processed by the receiver (|done_task| is called for each message).
35 // It is still possible that the MessageReceivedCallback is called 33 // It is still possible that the MessageReceivedCallback is called
36 // twice (so that there is more than one outstanding message), 34 // twice (so that there is more than one outstanding message),
37 // e.g. when we the sender sends multiple messages in one TCP packet. 35 // e.g. when we the sender sends multiple messages in one TCP packet.
38 class MessageReader : public base::RefCountedThreadSafe<MessageReader> { 36 class MessageReader : public base::SupportsWeakPtr<MessageReader>,
Wez 2012/08/22 22:36:15 Use a WeakPtrFactory member rather than inheriting
Sergey Ulanov 2012/08/22 22:41:42 Done.
37 public base::NonThreadSafe {
39 public: 38 public:
40 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> 39 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)>
41 MessageReceivedCallback; 40 MessageReceivedCallback;
42 41
43 MessageReader(); 42 MessageReader();
43 virtual ~MessageReader();
44 44
45 // Initialize the MessageReader with a socket. If a message is received 45 // Initialize the MessageReader with a socket. If a message is received
46 // |callback| is called. 46 // |callback| is called.
47 void Init(net::Socket* socket, const MessageReceivedCallback& callback); 47 void Init(net::Socket* socket, const MessageReceivedCallback& callback);
48 48
49 private: 49 private:
50 friend class base::RefCountedThreadSafe<MessageReader>;
51 virtual ~MessageReader();
52
53 void DoRead(); 50 void DoRead();
54 void OnRead(int result); 51 void OnRead(int result);
55 void HandleReadResult(int result); 52 void HandleReadResult(int result);
56 void OnDataReceived(net::IOBuffer* data, int data_size); 53 void OnDataReceived(net::IOBuffer* data, int data_size);
57 void OnMessageDone(scoped_refptr<base::SingleThreadTaskRunner> task_runner); 54 void OnMessageDone();
58 void ProcessDoneEvent();
59 55
60 net::Socket* socket_; 56 net::Socket* socket_;
61 57
62 // Set to true, when we have a socket read pending, and expecting 58 // Set to true, when we have a socket read pending, and expecting
63 // OnRead() to be called when new data is received. 59 // OnRead() to be called when new data is received.
64 bool read_pending_; 60 bool read_pending_;
65 61
66 // Number of messages that we received, but haven't finished 62 // Number of messages that we received, but haven't finished
67 // processing yet, i.e. |done_task| hasn't been called for these 63 // processing yet, i.e. |done_task| hasn't been called for these
68 // messages. 64 // messages.
69 int pending_messages_; 65 int pending_messages_;
70 66
71 bool closed_; 67 bool closed_;
72 scoped_refptr<net::IOBuffer> read_buffer_; 68 scoped_refptr<net::IOBuffer> read_buffer_;
73 69
74 MessageDecoder message_decoder_; 70 MessageDecoder message_decoder_;
75 71
76 // Callback is called when a message is received. 72 // Callback is called when a message is received.
77 MessageReceivedCallback message_received_callback_; 73 MessageReceivedCallback message_received_callback_;
78 }; 74 };
79 75
80 // Version of MessageReader for protocol buffer messages, that parses 76 // Version of MessageReader for protocol buffer messages, that parses
81 // each incoming message. 77 // each incoming message.
82 template <class T> 78 template <class T>
83 class ProtobufMessageReader { 79 class ProtobufMessageReader {
84 public: 80 public:
85 typedef typename base::Callback<void(scoped_ptr<T>, const base::Closure&)> 81 // The callback that is called when a new message is received. |done_task|
82 // must be called by the callback when it's done processing the |message|.
83 typedef typename base::Callback<void(scoped_ptr<T> message,
84 const base::Closure& done_task)>
86 MessageReceivedCallback; 85 MessageReceivedCallback;
87 86
88 ProtobufMessageReader() { }; 87 ProtobufMessageReader() { };
89 ~ProtobufMessageReader() { }; 88 ~ProtobufMessageReader() { };
90 89
91 void Init(net::Socket* socket, const MessageReceivedCallback& callback) { 90 void Init(net::Socket* socket, const MessageReceivedCallback& callback) {
92 DCHECK(!callback.is_null()); 91 DCHECK(!callback.is_null());
93 message_received_callback_ = callback; 92 message_received_callback_ = callback;
94 message_reader_ = new MessageReader(); 93 message_reader_.reset(new MessageReader());
95 message_reader_->Init( 94 message_reader_->Init(
96 socket, base::Bind(&ProtobufMessageReader<T>::OnNewData, 95 socket, base::Bind(&ProtobufMessageReader<T>::OnNewData,
97 base::Unretained(this))); 96 base::Unretained(this)));
98 } 97 }
99 98
100 private: 99 private:
101 void OnNewData(scoped_ptr<CompoundBuffer> buffer, 100 void OnNewData(scoped_ptr<CompoundBuffer> buffer,
102 const base::Closure& done_task) { 101 const base::Closure& done_task) {
103 scoped_ptr<T> message(new T()); 102 scoped_ptr<T> message(new T());
104 CompoundBufferInputStream stream(buffer.get()); 103 CompoundBufferInputStream stream(buffer.get());
105 bool ret = message->ParseFromZeroCopyStream(&stream); 104 bool ret = message->ParseFromZeroCopyStream(&stream);
106 if (!ret) { 105 if (!ret) {
107 LOG(WARNING) << "Received message that is not a valid protocol buffer."; 106 LOG(WARNING) << "Received message that is not a valid protocol buffer.";
108 } else { 107 } else {
109 DCHECK_EQ(stream.position(), buffer->total_bytes()); 108 DCHECK_EQ(stream.position(), buffer->total_bytes());
110 message_received_callback_.Run(message.Pass(), done_task); 109 message_received_callback_.Run(message.Pass(), done_task);
111 } 110 }
112 } 111 }
113 112
114 scoped_refptr<MessageReader> message_reader_; 113 scoped_ptr<MessageReader> message_reader_;
115 MessageReceivedCallback message_received_callback_; 114 MessageReceivedCallback message_received_callback_;
116 }; 115 };
117 116
118 } // namespace protocol 117 } // namespace protocol
119 } // namespace remoting 118 } // namespace remoting
120 119
121 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ 120 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_
OLDNEW
« no previous file with comments | « no previous file | remoting/protocol/message_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698