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