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> |
| 9 |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
13 #include "remoting/base/compound_buffer.h" | 14 #include "remoting/base/compound_buffer.h" |
14 #include "remoting/protocol/message_decoder.h" | 15 #include "remoting/protocol/message_decoder.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 class IOBuffer; | 18 class IOBuffer; |
18 } // namespace net | 19 } // namespace net |
19 | 20 |
20 namespace remoting { | 21 namespace remoting { |
21 namespace protocol { | 22 namespace protocol { |
22 | 23 |
23 class P2PStreamSocket; | 24 class P2PStreamSocket; |
24 | 25 |
25 // MessageReader reads data from the socket asynchronously and calls | 26 // MessageReader reads data from the socket asynchronously and calls |
26 // callback for each message it receives. It stops calling the | 27 // callback for each message it receives. It stops calling the |
27 // 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 |
28 // always be closed before the callback handler is destroyed. | 29 // always be closed before the callback handler is destroyed. |
29 // | 30 // |
30 // 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 |
31 // new data from the socket until all previously received messages are | 32 // new data from the socket until all previously received messages are |
32 // processed by the receiver (|done_task| is called for each message). | 33 // processed by the receiver (|done_task| is called for each message). |
33 // It is still possible that the MessageReceivedCallback is called | 34 // It is still possible that the MessageReceivedCallback is called |
34 // twice (so that there is more than one outstanding message), | 35 // twice (so that there is more than one outstanding message), |
35 // 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. |
36 class MessageReader : public base::NonThreadSafe { | 37 class MessageReader : public base::NonThreadSafe { |
37 public: | 38 public: |
38 typedef base::Callback<void(scoped_ptr<CompoundBuffer> message)> | 39 typedef base::Callback<void(std::unique_ptr<CompoundBuffer> message)> |
39 MessageReceivedCallback; | 40 MessageReceivedCallback; |
40 typedef base::Callback<void(int)> ReadFailedCallback; | 41 typedef base::Callback<void(int)> ReadFailedCallback; |
41 | 42 |
42 MessageReader(); | 43 MessageReader(); |
43 virtual ~MessageReader(); | 44 virtual ~MessageReader(); |
44 | 45 |
45 // Starts reading from |socket|. | 46 // Starts reading from |socket|. |
46 void StartReading(P2PStreamSocket* socket, | 47 void StartReading(P2PStreamSocket* socket, |
47 const MessageReceivedCallback& message_received_callback, | 48 const MessageReceivedCallback& message_received_callback, |
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(std::unique_ptr<CompoundBuffer> message); |
56 | 57 |
57 ReadFailedCallback read_failed_callback_; | 58 ReadFailedCallback read_failed_callback_; |
58 | 59 |
59 P2PStreamSocket* socket_ = nullptr; | 60 P2PStreamSocket* socket_ = nullptr; |
60 | 61 |
61 // 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 |
62 // OnRead() to be called when new data is received. | 63 // OnRead() to be called when new data is received. |
63 bool read_pending_ = false; | 64 bool read_pending_ = false; |
64 | 65 |
65 bool closed_ = false; | 66 bool closed_ = false; |
66 scoped_refptr<net::IOBuffer> read_buffer_; | 67 scoped_refptr<net::IOBuffer> read_buffer_; |
67 | 68 |
68 MessageDecoder message_decoder_; | 69 MessageDecoder message_decoder_; |
69 | 70 |
70 // Callback is called when a message is received. | 71 // Callback is called when a message is received. |
71 MessageReceivedCallback message_received_callback_; | 72 MessageReceivedCallback message_received_callback_; |
72 | 73 |
73 base::WeakPtrFactory<MessageReader> weak_factory_; | 74 base::WeakPtrFactory<MessageReader> weak_factory_; |
74 | 75 |
75 DISALLOW_COPY_AND_ASSIGN(MessageReader); | 76 DISALLOW_COPY_AND_ASSIGN(MessageReader); |
76 }; | 77 }; |
77 | 78 |
78 } // namespace protocol | 79 } // namespace protocol |
79 } // namespace remoting | 80 } // namespace remoting |
80 | 81 |
81 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ | 82 #endif // REMOTING_PROTOCOL_MESSAGE_READER_H_ |
OLD | NEW |