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