OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_MESSAGES_DECODER_H_ | 5 #ifndef REMOTING_PROTOCOL_MESSAGES_DECODER_H_ |
6 #define REMOTING_PROTOCOL_MESSAGES_DECODER_H_ | 6 #define REMOTING_PROTOCOL_MESSAGES_DECODER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "net/base/io_buffer.h" | |
awong
2010/11/16 00:04:58
ordering
Sergey Ulanov
2010/11/16 01:25:09
Done.
| |
13 #include "google/protobuf/message_lite.h" | 14 #include "google/protobuf/message_lite.h" |
awong
2010/11/16 00:04:58
Should this be from third_party/protobuf/src?
Sergey Ulanov
2010/11/16 01:25:09
Done. Also changed util.h
| |
14 #include "remoting/base/multiple_array_input_stream.h" | 15 #include "remoting/base/compound_buffer.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 class DrainableIOBuffer; | 18 class DrainableIOBuffer; |
18 class IOBuffer; | 19 class IOBuffer; |
19 } // namespace net | 20 } // namespace net |
20 | 21 |
21 namespace remoting { | 22 namespace remoting { |
22 | 23 |
23 class ChromotingClientMessage; | 24 class ChromotingClientMessage; |
24 class ChromotingHostMessage; | 25 class ChromotingHostMessage; |
25 class ClientControlMessage; | 26 class ClientControlMessage; |
26 class ClientEventMessage; | 27 class ClientEventMessage; |
27 class HostControlMessage; | 28 class HostControlMessage; |
28 class HostEventMessage; | 29 class HostEventMessage; |
29 | 30 |
30 // MessageDecoder uses MultipleArrayInputStream to decode bytes into | 31 // MessageDecoder uses CompoundBuffer to decode bytes into protocol |
31 // protocol buffer messages. This can be used to decode bytes received from | 32 // buffer messages. This can be used to decode bytes received from the |
32 // the network. | 33 // network. |
33 // | 34 // |
34 // It provides ParseMessages() which accepts an IOBuffer. If enough bytes | 35 // It provides ParseMessages() which accepts an IOBuffer. If enough bytes |
35 // are collected to produce protocol buffer messages then the bytes will be | 36 // are collected to produce protocol buffer messages then the bytes will be |
36 // consumed and the generated protocol buffer messages are added to the output | 37 // consumed and the generated protocol buffer messages are added to the output |
37 // list. | 38 // list. |
38 // | 39 // |
39 // It retains ownership of IOBuffer given to this object and keeps it alive | 40 // It retains ownership of IOBuffer given to this object and keeps it alive |
40 // until it is full consumed. | 41 // until it is full consumed. |
41 class MessageDecoder { | 42 class MessageDecoder { |
42 public: | 43 public: |
(...skipping 22 matching lines...) Expand all Loading... | |
65 } | 66 } |
66 | 67 |
67 private: | 68 private: |
68 // TODO(sergeyu): It might be more efficient to memcopy data to one big buffer | 69 // TODO(sergeyu): It might be more efficient to memcopy data to one big buffer |
69 // instead of storing chunks in dqueue. | 70 // instead of storing chunks in dqueue. |
70 typedef std::deque<scoped_refptr<net::DrainableIOBuffer> > BufferList; | 71 typedef std::deque<scoped_refptr<net::DrainableIOBuffer> > BufferList; |
71 | 72 |
72 // Parse one message from |buffer_list_|. Return true if sucessful. | 73 // Parse one message from |buffer_list_|. Return true if sucessful. |
73 template <class MessageType> | 74 template <class MessageType> |
74 bool ParseOneMessage(MessageType** message) { | 75 bool ParseOneMessage(MessageType** message) { |
75 scoped_ptr<MultipleArrayInputStream> stream(CreateInputStreamFromData()); | 76 scoped_ptr<CompoundBuffer> buffer(CreateCompoundBufferFromData()); |
76 if (!stream.get()) | 77 if (!buffer.get()) |
77 return false; | 78 return false; |
78 | 79 |
80 CompoundBufferInputStream stream(buffer.get()); | |
79 *message = new MessageType(); | 81 *message = new MessageType(); |
80 bool ret = (*message)->ParseFromZeroCopyStream(stream.get()); | 82 bool ret = (*message)->ParseFromZeroCopyStream(&stream); |
81 if (!ret) { | 83 if (!ret) |
82 delete *message; | 84 delete *message; |
83 } | |
84 return ret; | 85 return ret; |
85 } | 86 } |
86 | 87 |
87 void AddBuffer(scoped_refptr<net::IOBuffer> data, int data_size); | 88 void AddBuffer(scoped_refptr<net::IOBuffer> data, int data_size); |
88 | 89 |
89 MultipleArrayInputStream* CreateInputStreamFromData(); | 90 CompoundBuffer* CreateCompoundBufferFromData(); |
90 | 91 |
91 // Retrieves the read payload size of the current protocol buffer via |size|. | 92 // Retrieves the read payload size of the current protocol buffer via |size|. |
92 // Returns false and leaves |size| unmodified, if we do not have enough data | 93 // Returns false and leaves |size| unmodified, if we do not have enough data |
93 // to retrieve the current size. | 94 // to retrieve the current size. |
94 bool GetPayloadSize(int* size); | 95 bool GetPayloadSize(int* size); |
95 | 96 |
96 BufferList buffer_list_; | 97 BufferList buffer_list_; |
97 | 98 |
98 // The number of bytes in |buffer_list_| not consumed. | 99 // The number of bytes in |buffer_list_| not consumed. |
99 int available_bytes_; | 100 int available_bytes_; |
100 | 101 |
101 // |next_payload_| stores the size of the next payload if known. | 102 // |next_payload_| stores the size of the next payload if known. |
102 // |next_payload_known_| is true if the size of the next payload is known. | 103 // |next_payload_known_| is true if the size of the next payload is known. |
103 // After one payload is read this is reset to false. | 104 // After one payload is read this is reset to false. |
104 int next_payload_; | 105 int next_payload_; |
105 bool next_payload_known_; | 106 bool next_payload_known_; |
106 }; | 107 }; |
107 | 108 |
108 } // namespace remoting | 109 } // namespace remoting |
109 | 110 |
110 #endif // REMOTING_PROTOCOL_MESSAGES_DECODER_H_ | 111 #endif // REMOTING_PROTOCOL_MESSAGES_DECODER_H_ |
OLD | NEW |