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 <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/sequence_checker.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 namespace net { | 17 namespace net { |
18 class IOBuffer; | 18 class IOBuffer; |
19 } // namespace net | 19 } // namespace net |
20 | 20 |
21 namespace remoting { | 21 namespace remoting { |
22 namespace protocol { | 22 namespace protocol { |
23 | 23 |
24 class P2PStreamSocket; | 24 class P2PStreamSocket; |
25 | 25 |
26 // MessageReader reads data from the socket asynchronously and calls | 26 // MessageReader reads data from the socket asynchronously and calls |
27 // callback for each message it receives. It stops calling the | 27 // callback for each message it receives. It stops calling the |
28 // callback as soon as the socket is closed, so the socket should | 28 // callback as soon as the socket is closed, so the socket should |
29 // always be closed before the callback handler is destroyed. | 29 // always be closed before the callback handler is destroyed. |
30 // | 30 // |
31 // In order to throttle the stream, MessageReader doesn't try to read | 31 // In order to throttle the stream, MessageReader doesn't try to read |
32 // new data from the socket until all previously received messages are | 32 // new data from the socket until all previously received messages are |
33 // processed by the receiver (|done_task| is called for each message). | 33 // processed by the receiver (|done_task| is called for each message). |
34 // It is still possible that the MessageReceivedCallback is called | 34 // It is still possible that the MessageReceivedCallback is called |
35 // twice (so that there is more than one outstanding message), | 35 // twice (so that there is more than one outstanding message), |
36 // e.g. when we the sender sends multiple messages in one TCP packet. | 36 // e.g. when we the sender sends multiple messages in one TCP packet. |
37 class MessageReader : public base::NonThreadSafe { | 37 class MessageReader { |
38 public: | 38 public: |
39 typedef base::Callback<void(std::unique_ptr<CompoundBuffer> message)> | 39 typedef base::Callback<void(std::unique_ptr<CompoundBuffer> message)> |
40 MessageReceivedCallback; | 40 MessageReceivedCallback; |
41 typedef base::Callback<void(int)> ReadFailedCallback; | 41 typedef base::Callback<void(int)> ReadFailedCallback; |
42 | 42 |
43 MessageReader(); | 43 MessageReader(); |
44 virtual ~MessageReader(); | 44 virtual ~MessageReader(); |
45 | 45 |
46 // Starts reading from |socket|. | 46 // Starts reading from |socket|. |
47 void StartReading(P2PStreamSocket* socket, | 47 void StartReading(P2PStreamSocket* socket, |
(...skipping 16 matching lines...) Expand all Loading... |
64 bool read_pending_ = false; | 64 bool read_pending_ = false; |
65 | 65 |
66 bool closed_ = false; | 66 bool closed_ = false; |
67 scoped_refptr<net::IOBuffer> read_buffer_; | 67 scoped_refptr<net::IOBuffer> read_buffer_; |
68 | 68 |
69 MessageDecoder message_decoder_; | 69 MessageDecoder message_decoder_; |
70 | 70 |
71 // Callback is called when a message is received. | 71 // Callback is called when a message is received. |
72 MessageReceivedCallback message_received_callback_; | 72 MessageReceivedCallback message_received_callback_; |
73 | 73 |
| 74 SEQUENCE_CHECKER(sequence_checker_); |
| 75 |
74 base::WeakPtrFactory<MessageReader> weak_factory_; | 76 base::WeakPtrFactory<MessageReader> weak_factory_; |
75 | 77 |
76 DISALLOW_COPY_AND_ASSIGN(MessageReader); | 78 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
77 }; | 79 }; |
78 | 80 |
79 } // namespace protocol | 81 } // namespace protocol |
80 } // namespace remoting | 82 } // namespace remoting |
81 | 83 |
82 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ | 84 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ |
OLD | NEW |