OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_PROTOBUF_MESSAGE_PARSER_H_ | 5 #ifndef REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_ |
6 #define REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_ | 6 #define REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "remoting/base/compound_buffer.h" | 13 #include "remoting/base/compound_buffer.h" |
14 #include "remoting/protocol/message_reader.h" | 14 #include "remoting/protocol/message_reader.h" |
15 | 15 |
16 namespace remoting { | 16 namespace remoting { |
17 namespace protocol { | 17 namespace protocol { |
18 | 18 |
19 // Version of MessageReader for protocol buffer messages, that parses | 19 // Version of MessageReader for protocol buffer messages, that parses |
20 // each incoming message. | 20 // each incoming message. |
21 template <class T> | 21 template <class T> |
22 class ProtobufMessageParser { | 22 class ProtobufMessageParser { |
23 public: | 23 public: |
24 // The callback that is called when a new message is received. |done_task| | 24 // The callback that is called when a new message is received. |done_task| |
25 // must be called by the callback when it's done processing the |message|. | 25 // must be called by the callback when it's done processing the |message|. |
26 typedef typename base::Callback<void(scoped_ptr<T> message, | 26 typedef typename base::Callback<void(scoped_ptr<T> message)> |
27 const base::Closure& done_task)> | |
28 MessageReceivedCallback; | 27 MessageReceivedCallback; |
29 | 28 |
30 // |message_reader| must outlive ProtobufMessageParser. | 29 // |message_reader| must outlive ProtobufMessageParser. |
31 ProtobufMessageParser(const MessageReceivedCallback& callback, | 30 ProtobufMessageParser(const MessageReceivedCallback& callback, |
32 MessageReader* message_reader) | 31 MessageReader* message_reader) |
33 : message_reader_(message_reader), | 32 : message_reader_(message_reader), |
34 message_received_callback_(callback) { | 33 message_received_callback_(callback) { |
35 message_reader->SetMessageReceivedCallback(base::Bind( | 34 message_reader->SetMessageReceivedCallback(base::Bind( |
36 &ProtobufMessageParser<T>::OnNewData, base::Unretained(this))); | 35 &ProtobufMessageParser<T>::OnNewData, base::Unretained(this))); |
37 } | 36 } |
38 ~ProtobufMessageParser() { | 37 ~ProtobufMessageParser() { |
39 message_reader_->SetMessageReceivedCallback( | 38 message_reader_->SetMessageReceivedCallback( |
40 MessageReader::MessageReceivedCallback()); | 39 MessageReader::MessageReceivedCallback()); |
41 } | 40 } |
42 | 41 |
43 private: | 42 private: |
44 void OnNewData(scoped_ptr<CompoundBuffer> buffer, | 43 void OnNewData(scoped_ptr<CompoundBuffer> buffer) { |
45 const base::Closure& done_task) { | |
46 scoped_ptr<T> message(new T()); | 44 scoped_ptr<T> message(new T()); |
47 CompoundBufferInputStream stream(buffer.get()); | 45 CompoundBufferInputStream stream(buffer.get()); |
48 bool ret = message->ParseFromZeroCopyStream(&stream); | 46 bool ret = message->ParseFromZeroCopyStream(&stream); |
49 if (!ret) { | 47 if (!ret) { |
50 LOG(WARNING) << "Received message that is not a valid protocol buffer."; | 48 LOG(WARNING) << "Received message that is not a valid protocol buffer."; |
51 } else { | 49 } else { |
52 DCHECK_EQ(stream.position(), buffer->total_bytes()); | 50 DCHECK_EQ(stream.position(), buffer->total_bytes()); |
53 message_received_callback_.Run(std::move(message), done_task); | 51 message_received_callback_.Run(std::move(message)); |
54 } | 52 } |
55 } | 53 } |
56 | 54 |
57 MessageReader* message_reader_; | 55 MessageReader* message_reader_; |
58 MessageReceivedCallback message_received_callback_; | 56 MessageReceivedCallback message_received_callback_; |
59 }; | 57 }; |
60 | 58 |
61 } // namespace protocol | 59 } // namespace protocol |
62 } // namespace remoting | 60 } // namespace remoting |
63 | 61 |
64 #endif // REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_ | 62 #endif // REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_ |
OLD | NEW |