Index: remoting/protocol/message_decoder.h |
diff --git a/remoting/protocol/message_decoder.h b/remoting/protocol/message_decoder.h |
index 8b521fe6a1144f0785f20cbacf98b6547b438f80..3e0745f73f9a70a4d017f7797c65d5d7be248601 100644 |
--- a/remoting/protocol/message_decoder.h |
+++ b/remoting/protocol/message_decoder.h |
@@ -17,63 +17,30 @@ |
namespace remoting { |
namespace protocol { |
-// MessageDecoder uses CompoundBuffer to decode bytes into protocol |
-// buffer messages. This can be used to decode bytes received from the |
-// network. |
+// MessageDecoder uses CompoundBuffer to split the data received from |
+// the network into separate messages. Each message is expected to be |
+// decoded in the stream as follows: |
+// +--------------+--------------+ |
+// | message_size | message_data | |
+// +--------------+--------------+ |
// |
-// It provides ParseMessages() which accepts an IOBuffer. If enough bytes |
-// are collected to produce protocol buffer messages then the bytes will be |
-// consumed and the generated protocol buffer messages are added to the output |
-// list. |
-// |
-// It retains ownership of IOBuffer given to this object and keeps it alive |
-// until it is full consumed. |
+// Here, message_size is 4-byte integer that represents size of |
+// message_data in bytes. message_data - content of the message. |
class MessageDecoder { |
public: |
MessageDecoder(); |
virtual ~MessageDecoder(); |
- // Parses the bytes in |data| into a protobuf of type MessageType. The bytes |
- // in |data| are conceptually a stream of bytes to be parsed into a series of |
- // protobufs. Each parsed protouf is appended into the |messages|. All calls |
- // to ParseMessages should use same MessageType for any single instace of |
- // MessageDecoder. |
- |
- // This function retains |data| until all its bytes are consumed. |
- // Ownership of the produced protobufs is passed to the caller via the |
- // |messages| list. |
- template <class MessageType> |
- void ParseMessages(scoped_refptr<net::IOBuffer> data, |
- int data_size, std::list<MessageType*>* messages) { |
- AddBuffer(data, data_size); |
- |
- // Then try to parse the next message until we can't parse anymore. |
- MessageType* message; |
- while (ParseOneMessage<MessageType>(&message)) { |
- messages->push_back(message); |
- } |
- } |
- |
- private: |
- // Parse one message from |buffer_list_|. Return true if sucessful. |
- template <class MessageType> |
- bool ParseOneMessage(MessageType** message) { |
- CompoundBuffer buffer; |
- if (!GetNextMessageData(&buffer)) |
- return false; |
- |
- CompoundBufferInputStream stream(&buffer); |
- *message = new MessageType(); |
- bool ret = (*message)->ParseFromZeroCopyStream(&stream); |
- if (!ret) |
- delete *message; |
- return ret; |
- } |
- |
- void AddBuffer(scoped_refptr<net::IOBuffer> data, int data_size); |
+ // Add next chunk of data. MessageDecoder retains |data| until all |
+ // its bytes are consumed. |
+ void AddData(scoped_refptr<net::IOBuffer> data, int data_size); |
- bool GetNextMessageData(CompoundBuffer* message_buffer); |
+ // Get next message from the stream and puts it in |
+ // |message_buffer|. Returns false if there are no complete messages |
+ // yet. |
+ bool GetNextMessage(CompoundBuffer* message_buffer); |
+ private: |
// Retrieves the read payload size of the current protocol buffer via |size|. |
// Returns false and leaves |size| unmodified, if we do not have enough data |
// to retrieve the current size. |