| OLD | NEW |
| 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::NonThreadSafe { |
| 39 public: | 37 public: |
| 40 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> | 38 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> |
| 41 MessageReceivedCallback; | 39 MessageReceivedCallback; |
| 42 | 40 |
| 43 MessageReader(); | 41 MessageReader(); |
| 42 virtual ~MessageReader(); |
| 44 | 43 |
| 45 // Initialize the MessageReader with a socket. If a message is received | 44 // Initialize the MessageReader with a socket. If a message is received |
| 46 // |callback| is called. | 45 // |callback| is called. |
| 47 void Init(net::Socket* socket, const MessageReceivedCallback& callback); | 46 void Init(net::Socket* socket, const MessageReceivedCallback& callback); |
| 48 | 47 |
| 49 private: | 48 private: |
| 50 friend class base::RefCountedThreadSafe<MessageReader>; | |
| 51 virtual ~MessageReader(); | |
| 52 | |
| 53 void DoRead(); | 49 void DoRead(); |
| 54 void OnRead(int result); | 50 void OnRead(int result); |
| 55 void HandleReadResult(int result); | 51 void HandleReadResult(int result); |
| 56 void OnDataReceived(net::IOBuffer* data, int data_size); | 52 void OnDataReceived(net::IOBuffer* data, int data_size); |
| 57 void OnMessageDone(scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 53 void OnMessageDone(); |
| 58 void ProcessDoneEvent(); | |
| 59 | 54 |
| 60 net::Socket* socket_; | 55 net::Socket* socket_; |
| 61 | 56 |
| 62 // Set to true, when we have a socket read pending, and expecting | 57 // Set to true, when we have a socket read pending, and expecting |
| 63 // OnRead() to be called when new data is received. | 58 // OnRead() to be called when new data is received. |
| 64 bool read_pending_; | 59 bool read_pending_; |
| 65 | 60 |
| 66 // Number of messages that we received, but haven't finished | 61 // Number of messages that we received, but haven't finished |
| 67 // processing yet, i.e. |done_task| hasn't been called for these | 62 // processing yet, i.e. |done_task| hasn't been called for these |
| 68 // messages. | 63 // messages. |
| 69 int pending_messages_; | 64 int pending_messages_; |
| 70 | 65 |
| 71 bool closed_; | 66 bool closed_; |
| 72 scoped_refptr<net::IOBuffer> read_buffer_; | 67 scoped_refptr<net::IOBuffer> read_buffer_; |
| 73 | 68 |
| 74 MessageDecoder message_decoder_; | 69 MessageDecoder message_decoder_; |
| 75 | 70 |
| 76 // Callback is called when a message is received. | 71 // Callback is called when a message is received. |
| 77 MessageReceivedCallback message_received_callback_; | 72 MessageReceivedCallback message_received_callback_; |
| 73 |
| 74 base::WeakPtrFactory<MessageReader> weak_factory_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
| 78 }; | 77 }; |
| 79 | 78 |
| 80 // Version of MessageReader for protocol buffer messages, that parses | 79 // Version of MessageReader for protocol buffer messages, that parses |
| 81 // each incoming message. | 80 // each incoming message. |
| 82 template <class T> | 81 template <class T> |
| 83 class ProtobufMessageReader { | 82 class ProtobufMessageReader { |
| 84 public: | 83 public: |
| 85 typedef typename base::Callback<void(scoped_ptr<T>, const base::Closure&)> | 84 // The callback that is called when a new message is received. |done_task| |
| 85 // must be called by the callback when it's done processing the |message|. |
| 86 typedef typename base::Callback<void(scoped_ptr<T> message, |
| 87 const base::Closure& done_task)> |
| 86 MessageReceivedCallback; | 88 MessageReceivedCallback; |
| 87 | 89 |
| 88 ProtobufMessageReader() { }; | 90 ProtobufMessageReader() { }; |
| 89 ~ProtobufMessageReader() { }; | 91 ~ProtobufMessageReader() { }; |
| 90 | 92 |
| 91 void Init(net::Socket* socket, const MessageReceivedCallback& callback) { | 93 void Init(net::Socket* socket, const MessageReceivedCallback& callback) { |
| 92 DCHECK(!callback.is_null()); | 94 DCHECK(!callback.is_null()); |
| 93 message_received_callback_ = callback; | 95 message_received_callback_ = callback; |
| 94 message_reader_ = new MessageReader(); | 96 message_reader_.reset(new MessageReader()); |
| 95 message_reader_->Init( | 97 message_reader_->Init( |
| 96 socket, base::Bind(&ProtobufMessageReader<T>::OnNewData, | 98 socket, base::Bind(&ProtobufMessageReader<T>::OnNewData, |
| 97 base::Unretained(this))); | 99 base::Unretained(this))); |
| 98 } | 100 } |
| 99 | 101 |
| 100 private: | 102 private: |
| 101 void OnNewData(scoped_ptr<CompoundBuffer> buffer, | 103 void OnNewData(scoped_ptr<CompoundBuffer> buffer, |
| 102 const base::Closure& done_task) { | 104 const base::Closure& done_task) { |
| 103 scoped_ptr<T> message(new T()); | 105 scoped_ptr<T> message(new T()); |
| 104 CompoundBufferInputStream stream(buffer.get()); | 106 CompoundBufferInputStream stream(buffer.get()); |
| 105 bool ret = message->ParseFromZeroCopyStream(&stream); | 107 bool ret = message->ParseFromZeroCopyStream(&stream); |
| 106 if (!ret) { | 108 if (!ret) { |
| 107 LOG(WARNING) << "Received message that is not a valid protocol buffer."; | 109 LOG(WARNING) << "Received message that is not a valid protocol buffer."; |
| 108 } else { | 110 } else { |
| 109 DCHECK_EQ(stream.position(), buffer->total_bytes()); | 111 DCHECK_EQ(stream.position(), buffer->total_bytes()); |
| 110 message_received_callback_.Run(message.Pass(), done_task); | 112 message_received_callback_.Run(message.Pass(), done_task); |
| 111 } | 113 } |
| 112 } | 114 } |
| 113 | 115 |
| 114 scoped_refptr<MessageReader> message_reader_; | 116 scoped_ptr<MessageReader> message_reader_; |
| 115 MessageReceivedCallback message_received_callback_; | 117 MessageReceivedCallback message_received_callback_; |
| 116 }; | 118 }; |
| 117 | 119 |
| 118 } // namespace protocol | 120 } // namespace protocol |
| 119 } // namespace remoting | 121 } // namespace remoting |
| 120 | 122 |
| 121 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ | 123 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ |
| OLD | NEW |