| 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/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 12 #include "remoting/base/compound_buffer.h" | 12 #include "remoting/base/compound_buffer.h" |
| 13 #include "remoting/protocol/message_decoder.h" | 13 #include "remoting/protocol/message_decoder.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 class IOBuffer; | 16 class IOBuffer; |
| 17 class Socket; | |
| 18 } // namespace net | 17 } // namespace net |
| 19 | 18 |
| 20 namespace remoting { | 19 namespace remoting { |
| 21 namespace protocol { | 20 namespace protocol { |
| 22 | 21 |
| 22 class P2PStreamSocket; |
| 23 |
| 23 // MessageReader reads data from the socket asynchronously and calls | 24 // MessageReader reads data from the socket asynchronously and calls |
| 24 // callback for each message it receives. It stops calling the | 25 // callback for each message it receives. It stops calling the |
| 25 // callback as soon as the socket is closed, so the socket should | 26 // callback as soon as the socket is closed, so the socket should |
| 26 // always be closed before the callback handler is destroyed. | 27 // always be closed before the callback handler is destroyed. |
| 27 // | 28 // |
| 28 // In order to throttle the stream, MessageReader doesn't try to read | 29 // In order to throttle the stream, MessageReader doesn't try to read |
| 29 // new data from the socket until all previously received messages are | 30 // new data from the socket until all previously received messages are |
| 30 // processed by the receiver (|done_task| is called for each message). | 31 // processed by the receiver (|done_task| is called for each message). |
| 31 // It is still possible that the MessageReceivedCallback is called | 32 // It is still possible that the MessageReceivedCallback is called |
| 32 // twice (so that there is more than one outstanding message), | 33 // twice (so that there is more than one outstanding message), |
| 33 // e.g. when we the sender sends multiple messages in one TCP packet. | 34 // e.g. when we the sender sends multiple messages in one TCP packet. |
| 34 class MessageReader : public base::NonThreadSafe { | 35 class MessageReader : public base::NonThreadSafe { |
| 35 public: | 36 public: |
| 36 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> | 37 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> |
| 37 MessageReceivedCallback; | 38 MessageReceivedCallback; |
| 38 typedef base::Callback<void(int)> ReadFailedCallback; | 39 typedef base::Callback<void(int)> ReadFailedCallback; |
| 39 | 40 |
| 40 MessageReader(); | 41 MessageReader(); |
| 41 virtual ~MessageReader(); | 42 virtual ~MessageReader(); |
| 42 | 43 |
| 43 // Sets the callback to be called for each incoming message. | 44 // Sets the callback to be called for each incoming message. |
| 44 void SetMessageReceivedCallback(const MessageReceivedCallback& callback); | 45 void SetMessageReceivedCallback(const MessageReceivedCallback& callback); |
| 45 | 46 |
| 46 // Starts reading from |socket|. | 47 // Starts reading from |socket|. |
| 47 void StartReading(net::Socket* socket, | 48 void StartReading(P2PStreamSocket* socket, |
| 48 const ReadFailedCallback& read_failed_callback); | 49 const ReadFailedCallback& read_failed_callback); |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 void DoRead(); | 52 void DoRead(); |
| 52 void OnRead(int result); | 53 void OnRead(int result); |
| 53 void HandleReadResult(int result, bool* read_succeeded); | 54 void HandleReadResult(int result, bool* read_succeeded); |
| 54 void OnDataReceived(net::IOBuffer* data, int data_size); | 55 void OnDataReceived(net::IOBuffer* data, int data_size); |
| 55 void RunCallback(scoped_ptr<CompoundBuffer> message); | 56 void RunCallback(scoped_ptr<CompoundBuffer> message); |
| 56 void OnMessageDone(); | 57 void OnMessageDone(); |
| 57 | 58 |
| 58 ReadFailedCallback read_failed_callback_; | 59 ReadFailedCallback read_failed_callback_; |
| 59 | 60 |
| 60 net::Socket* socket_; | 61 P2PStreamSocket* socket_; |
| 61 | 62 |
| 62 // Set to true, when we have a socket read pending, and expecting | 63 // Set to true, when we have a socket read pending, and expecting |
| 63 // OnRead() to be called when new data is received. | 64 // OnRead() to be called when new data is received. |
| 64 bool read_pending_; | 65 bool read_pending_; |
| 65 | 66 |
| 66 // Number of messages that we received, but haven't finished | 67 // Number of messages that we received, but haven't finished |
| 67 // processing yet, i.e. |done_task| hasn't been called for these | 68 // processing yet, i.e. |done_task| hasn't been called for these |
| 68 // messages. | 69 // messages. |
| 69 int pending_messages_; | 70 int pending_messages_; |
| 70 | 71 |
| 71 bool closed_; | 72 bool closed_; |
| 72 scoped_refptr<net::IOBuffer> read_buffer_; | 73 scoped_refptr<net::IOBuffer> read_buffer_; |
| 73 | 74 |
| 74 MessageDecoder message_decoder_; | 75 MessageDecoder message_decoder_; |
| 75 | 76 |
| 76 // Callback is called when a message is received. | 77 // Callback is called when a message is received. |
| 77 MessageReceivedCallback message_received_callback_; | 78 MessageReceivedCallback message_received_callback_; |
| 78 | 79 |
| 79 base::WeakPtrFactory<MessageReader> weak_factory_; | 80 base::WeakPtrFactory<MessageReader> weak_factory_; |
| 80 | 81 |
| 81 DISALLOW_COPY_AND_ASSIGN(MessageReader); | 82 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 } // namespace protocol | 85 } // namespace protocol |
| 85 } // namespace remoting | 86 } // namespace remoting |
| 86 | 87 |
| 87 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ | 88 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ |
| OLD | NEW |