| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // always be closed before the callback handler is destroyed. | 28 // always be closed before the callback handler is destroyed. |
| 29 // | 29 // |
| 30 // 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 |
| 31 // new data from the socket until all previously received messages are | 31 // new data from the socket until all previously received messages are |
| 32 // processed by the receiver (|done_task| is called for each message). | 32 // processed by the receiver (|done_task| is called for each message). |
| 33 // It is still possible that the MessageReceivedCallback is called | 33 // It is still possible that the MessageReceivedCallback is called |
| 34 // twice (so that there is more than one outstanding message), | 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. | 35 // e.g. when we the sender sends multiple messages in one TCP packet. |
| 36 class MessageReader : public base::NonThreadSafe { | 36 class MessageReader : public base::NonThreadSafe { |
| 37 public: | 37 public: |
| 38 typedef base::Callback<void(scoped_ptr<CompoundBuffer>, const base::Closure&)> | 38 typedef base::Callback<void(scoped_ptr<CompoundBuffer> message)> |
| 39 MessageReceivedCallback; | 39 MessageReceivedCallback; |
| 40 typedef base::Callback<void(int)> ReadFailedCallback; | 40 typedef base::Callback<void(int)> ReadFailedCallback; |
| 41 | 41 |
| 42 MessageReader(); | 42 MessageReader(); |
| 43 virtual ~MessageReader(); | 43 virtual ~MessageReader(); |
| 44 | 44 |
| 45 // Sets the callback to be called for each incoming message. | 45 // Sets the callback to be called for each incoming message. |
| 46 void SetMessageReceivedCallback(const MessageReceivedCallback& callback); | 46 void SetMessageReceivedCallback(const MessageReceivedCallback& callback); |
| 47 | 47 |
| 48 // Starts reading from |socket|. | 48 // Starts reading from |socket|. |
| 49 void StartReading(P2PStreamSocket* socket, | 49 void StartReading(P2PStreamSocket* socket, |
| 50 const ReadFailedCallback& read_failed_callback); | 50 const ReadFailedCallback& read_failed_callback); |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 void DoRead(); | 53 void DoRead(); |
| 54 void OnRead(int result); | 54 void OnRead(int result); |
| 55 void HandleReadResult(int result, bool* read_succeeded); | 55 void HandleReadResult(int result, bool* read_succeeded); |
| 56 void OnDataReceived(net::IOBuffer* data, int data_size); | 56 void OnDataReceived(net::IOBuffer* data, int data_size); |
| 57 void RunCallback(scoped_ptr<CompoundBuffer> message); | 57 void RunCallback(scoped_ptr<CompoundBuffer> message); |
| 58 void OnMessageDone(); | |
| 59 | 58 |
| 60 ReadFailedCallback read_failed_callback_; | 59 ReadFailedCallback read_failed_callback_; |
| 61 | 60 |
| 62 P2PStreamSocket* socket_; | 61 P2PStreamSocket* socket_; |
| 63 | 62 |
| 64 // 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 |
| 65 // OnRead() to be called when new data is received. | 64 // OnRead() to be called when new data is received. |
| 66 bool read_pending_; | 65 bool read_pending_; |
| 67 | 66 |
| 68 // Number of messages that we received, but haven't finished | |
| 69 // processing yet, i.e. |done_task| hasn't been called for these | |
| 70 // messages. | |
| 71 int pending_messages_; | |
| 72 | |
| 73 bool closed_; | 67 bool closed_; |
| 74 scoped_refptr<net::IOBuffer> read_buffer_; | 68 scoped_refptr<net::IOBuffer> read_buffer_; |
| 75 | 69 |
| 76 MessageDecoder message_decoder_; | 70 MessageDecoder message_decoder_; |
| 77 | 71 |
| 78 // Callback is called when a message is received. | 72 // Callback is called when a message is received. |
| 79 MessageReceivedCallback message_received_callback_; | 73 MessageReceivedCallback message_received_callback_; |
| 80 | 74 |
| 81 base::WeakPtrFactory<MessageReader> weak_factory_; | 75 base::WeakPtrFactory<MessageReader> weak_factory_; |
| 82 | 76 |
| 83 DISALLOW_COPY_AND_ASSIGN(MessageReader); | 77 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
| 84 }; | 78 }; |
| 85 | 79 |
| 86 } // namespace protocol | 80 } // namespace protocol |
| 87 } // namespace remoting | 81 } // namespace remoting |
| 88 | 82 |
| 89 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ | 83 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ |
| OLD | NEW |