Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1066)

Side by Side Diff: remoting/protocol/protobuf_message_parser.h

Issue 1654513003: Simplify message parsing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no_done
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/protocol/message_serialization.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
6 #define REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
7
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "remoting/base/compound_buffer.h"
14 #include "remoting/protocol/message_reader.h"
15
16 namespace remoting {
17 namespace protocol {
18
19 // Version of MessageReader for protocol buffer messages, that parses
20 // each incoming message.
21 template <class T>
22 class ProtobufMessageParser {
23 public:
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|.
26 typedef typename base::Callback<void(scoped_ptr<T> message)>
27 MessageReceivedCallback;
28
29 // |message_reader| must outlive ProtobufMessageParser.
30 ProtobufMessageParser(const MessageReceivedCallback& callback,
31 MessageReader* message_reader)
32 : message_reader_(message_reader),
33 message_received_callback_(callback) {
34 message_reader->SetMessageReceivedCallback(base::Bind(
35 &ProtobufMessageParser<T>::OnNewData, base::Unretained(this)));
36 }
37 ~ProtobufMessageParser() {
38 message_reader_->SetMessageReceivedCallback(
39 MessageReader::MessageReceivedCallback());
40 }
41
42 private:
43 void OnNewData(scoped_ptr<CompoundBuffer> buffer) {
44 scoped_ptr<T> message(new T());
45 CompoundBufferInputStream stream(buffer.get());
46 bool ret = message->ParseFromZeroCopyStream(&stream);
47 if (!ret) {
48 LOG(WARNING) << "Received message that is not a valid protocol buffer.";
49 } else {
50 DCHECK_EQ(stream.position(), buffer->total_bytes());
51 message_received_callback_.Run(std::move(message));
52 }
53 }
54
55 MessageReader* message_reader_;
56 MessageReceivedCallback message_received_callback_;
57 };
58
59 } // namespace protocol
60 } // namespace remoting
61
62 #endif // REMOTING_PROTOCOL_PROTOBUF_MESSAGE_PARSER_H_
OLDNEW
« no previous file with comments | « remoting/protocol/message_serialization.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698