OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef REMOTING_PROTOCOL_MESSAGES_DECODER_H_ | |
6 #define REMOTING_PROTOCOL_MESSAGES_DECODER_H_ | |
7 | |
8 #include <deque> | |
9 #include <list> | |
10 | |
11 #include "base/ref_counted.h" | |
12 #include "google/protobuf/message_lite.h" | |
13 #include "remoting/proto/internal.pb.h" | |
14 | |
15 namespace net { | |
16 class IOBuffer; | |
17 } | |
18 | |
19 namespace remoting { | |
20 | |
21 typedef std::list<ChromotingHostMessage*> HostMessageList; | |
22 typedef std::list<ChromotingClientMessage*> ClientMessageList; | |
23 | |
24 // A protocol decoder is used to decode data transmitted in the chromoting | |
25 // network. | |
26 // TODO(hclam): Defines the interface and implement methods. | |
27 class MessagesDecoder { | |
28 public: | |
29 MessagesDecoder(); | |
30 virtual ~MessagesDecoder(); | |
31 | |
32 // Parse data received from network into ClientMessages. Output is written | |
33 // to |messages|. | |
34 virtual void ParseClientMessages(scoped_refptr<net::IOBuffer> data, | |
35 int data_size, | |
36 ClientMessageList* messages); | |
37 | |
38 // Parse data received from network into HostMessages. Output is | |
39 // written to |messages|. | |
40 virtual void ParseHostMessages(scoped_refptr<net::IOBuffer> data, | |
41 int data_size, | |
42 HostMessageList* messages); | |
43 | |
44 private: | |
45 // DataChunk stores reference to a net::IOBuffer and size of the data | |
46 // stored in that buffer. | |
47 struct DataChunk { | |
48 DataChunk(net::IOBuffer* data, size_t data_size); | |
49 ~DataChunk(); | |
50 | |
51 scoped_refptr<net::IOBuffer> data; | |
52 size_t data_size; | |
53 }; | |
54 | |
55 // TODO(sergeyu): It might be more efficient to memcopy data to one big buffer | |
56 // instead of storing chunks in dqueue. | |
57 typedef std::deque<DataChunk> DataList; | |
58 | |
59 // A private method used to parse data received from network into protocol | |
60 // buffers. | |
61 template <typename T> | |
62 void ParseMessages(scoped_refptr<net::IOBuffer> data, | |
63 int data_size, | |
64 std::list<T*>* messages); | |
65 | |
66 // Parse one message from |data_list_|. Return true if sucessful. | |
67 template <typename T> | |
68 bool ParseOneMessage(T** messages); | |
69 | |
70 // A utility method to read payload size of the protocol buffer from the | |
71 // data list. Return false if we don't have enough data. | |
72 bool GetPayloadSize(int* size); | |
73 | |
74 DataList data_list_; | |
75 size_t last_read_position_; | |
76 | |
77 // Count the number of bytes in |data_list_| not read. | |
78 size_t available_bytes_; | |
79 | |
80 // Stores the size of the next payload if known. | |
81 size_t next_payload_; | |
82 | |
83 // True if the size of the next payload is known. After one payload is read, | |
84 // this is reset to false. | |
85 bool next_payload_known_; | |
86 }; | |
87 | |
88 } // namespace remoting | |
89 | |
90 #endif // REMOTING_PROTOCOL_MESSAGES_DECODER_H_ | |
OLD | NEW |