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 #include "remoting/protocol/message_decoder.h" | 5 #include "remoting/protocol/message_decoder.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 #include "remoting/base/multiple_array_input_stream.h" | 9 #include "remoting/base/compound_buffer.h" |
10 #include "remoting/proto/internal.pb.h" | 10 #include "remoting/proto/internal.pb.h" |
11 #include "third_party/libjingle/source/talk/base/byteorder.h" | 11 #include "third_party/libjingle/source/talk/base/byteorder.h" |
12 | 12 |
13 namespace remoting { | 13 namespace remoting { |
14 | 14 |
15 MessageDecoder::MessageDecoder() | 15 MessageDecoder::MessageDecoder() |
16 : available_bytes_(0), | 16 : available_bytes_(0), |
17 next_payload_(0), | 17 next_payload_(0), |
18 next_payload_known_(false) { | 18 next_payload_known_(false) { |
19 } | 19 } |
20 | 20 |
21 MessageDecoder::~MessageDecoder() {} | 21 MessageDecoder::~MessageDecoder() {} |
22 | 22 |
23 void MessageDecoder::AddBuffer(scoped_refptr<net::IOBuffer> data, | 23 void MessageDecoder::AddBuffer(scoped_refptr<net::IOBuffer> data, |
24 int data_size) { | 24 int data_size) { |
25 buffer_list_.push_back(make_scoped_refptr( | 25 buffer_list_.push_back(make_scoped_refptr( |
26 new net::DrainableIOBuffer(data, data_size))); | 26 new net::DrainableIOBuffer(data, data_size))); |
27 available_bytes_ += data_size; | 27 available_bytes_ += data_size; |
28 } | 28 } |
29 | 29 |
30 MultipleArrayInputStream* MessageDecoder::CreateInputStreamFromData() { | 30 CompoundBuffer* MessageDecoder::CreateCompoundBufferFromData() { |
31 // Determine the payload size. If we already know it then skip this part. | 31 // Determine the payload size. If we already know it then skip this part. |
32 // We may not have enough data to determine the payload size so use a | 32 // We may not have enough data to determine the payload size so use a |
33 // utility function to find out. | 33 // utility function to find out. |
34 int next_payload = -1; | 34 int next_payload = -1; |
35 if (!next_payload_known_ && GetPayloadSize(&next_payload)) { | 35 if (!next_payload_known_ && GetPayloadSize(&next_payload)) { |
36 DCHECK_NE(-1, next_payload); | 36 DCHECK_NE(-1, next_payload); |
37 next_payload_ = next_payload; | 37 next_payload_ = next_payload; |
38 next_payload_known_ = true; | 38 next_payload_known_ = true; |
39 } | 39 } |
40 | 40 |
41 // If the next payload size is still not known or we don't have enough | 41 // If the next payload size is still not known or we don't have enough |
42 // data for parsing then exit. | 42 // data for parsing then exit. |
43 if (!next_payload_known_ || available_bytes_ < next_payload_) | 43 if (!next_payload_known_ || available_bytes_ < next_payload_) |
44 return NULL; | 44 return NULL; |
45 next_payload_known_ = false; | 45 next_payload_known_ = false; |
46 | 46 |
47 // The following loop gather buffers in |buffer_list_| that sum up to | 47 // The following loop gather buffers in |buffer_list_| that sum up to |
48 // |next_payload_| bytes. These buffers are added to |stream|. | 48 // |next_payload_| bytes. These buffers are added to |stream|. |
49 | 49 |
50 // Create a MultipleArrayInputStream for parsing. | 50 // Create a CompoundBuffer for parsing. |
51 // TODO(hclam): Avoid creating this object everytime. | 51 CompoundBuffer* result = new CompoundBuffer(); |
52 MultipleArrayInputStream* stream = new MultipleArrayInputStream(); | |
53 while (next_payload_ > 0 && !buffer_list_.empty()) { | 52 while (next_payload_ > 0 && !buffer_list_.empty()) { |
54 scoped_refptr<net::DrainableIOBuffer> buffer = buffer_list_.front(); | 53 scoped_refptr<net::DrainableIOBuffer> buffer = buffer_list_.front(); |
55 int read_bytes = std::min(buffer->BytesRemaining(), next_payload_); | 54 int read_bytes = std::min(buffer->BytesRemaining(), next_payload_); |
56 | 55 |
57 // This call creates a new instance of DrainableIOBuffer internally. | |
58 // This will reference the same base pointer but maintain it's own | 56 // This will reference the same base pointer but maintain it's own |
59 // version of data pointer. | 57 // version of data pointer. |
60 stream->AddBuffer(buffer, read_bytes); | 58 result->Append(buffer, read_bytes); |
61 | 59 |
62 // Adjust counters. | 60 // Adjust counters. |
63 buffer->DidConsume(read_bytes); | 61 buffer->DidConsume(read_bytes); |
64 next_payload_ -= read_bytes; | 62 next_payload_ -= read_bytes; |
65 available_bytes_ -= read_bytes; | 63 available_bytes_ -= read_bytes; |
66 | 64 |
67 // If the front buffer is fully read then remove it from the queue. | 65 // If the front buffer is fully read then remove it from the queue. |
68 if (!buffer->BytesRemaining()) | 66 if (!buffer->BytesRemaining()) |
69 buffer_list_.pop_front(); | 67 buffer_list_.pop_front(); |
70 } | 68 } |
71 DCHECK_EQ(0, next_payload_); | 69 DCHECK_EQ(0, next_payload_); |
72 DCHECK_LE(0, available_bytes_); | 70 DCHECK_LE(0, available_bytes_); |
73 return stream; | 71 result->Lock(); |
| 72 return result; |
74 } | 73 } |
75 | 74 |
76 static int GetHeaderSize(const std::string& header) { | 75 static int GetHeaderSize(const std::string& header) { |
77 return header.length(); | 76 return header.length(); |
78 } | 77 } |
79 | 78 |
80 bool MessageDecoder::GetPayloadSize(int* size) { | 79 bool MessageDecoder::GetPayloadSize(int* size) { |
81 // The header has a size of 4 bytes. | 80 // The header has a size of 4 bytes. |
82 const int kHeaderSize = sizeof(int32); | 81 const int kHeaderSize = sizeof(int32); |
83 | 82 |
(...skipping 20 matching lines...) Expand all Loading... |
104 if (!buffer->BytesRemaining()) { | 103 if (!buffer->BytesRemaining()) { |
105 buffer_list_.pop_front(); | 104 buffer_list_.pop_front(); |
106 } | 105 } |
107 } | 106 } |
108 | 107 |
109 *size = talk_base::GetBE32(header.c_str()); | 108 *size = talk_base::GetBE32(header.c_str()); |
110 return true; | 109 return true; |
111 } | 110 } |
112 | 111 |
113 } // namespace remoting | 112 } // namespace remoting |
OLD | NEW |