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/base/protocol_decoder.h" | 5 #include "remoting/base/protocol_decoder.h" |
6 | 6 |
7 #include "remoting/base/multiple_array_input_stream.h" | 7 #include "remoting/base/multiple_array_input_stream.h" |
8 #include "talk/base/byteorder.h" | 8 #include "talk/base/byteorder.h" |
9 | 9 |
10 namespace remoting { | 10 namespace remoting { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 if (!next_payload_known_ || available_bytes_ < next_payload_) | 62 if (!next_payload_known_ || available_bytes_ < next_payload_) |
63 return false; | 63 return false; |
64 next_payload_known_ = false; | 64 next_payload_known_ = false; |
65 | 65 |
66 // Extract data from |data_list_| used to form a full protocol buffer. | 66 // Extract data from |data_list_| used to form a full protocol buffer. |
67 DataList buffers; | 67 DataList buffers; |
68 std::deque<const uint8*> buffer_pointers; | 68 std::deque<const uint8*> buffer_pointers; |
69 std::deque<int> buffer_sizes; | 69 std::deque<int> buffer_sizes; |
70 while (next_payload_ > 0 && !data_list_.empty()) { | 70 while (next_payload_ > 0 && !data_list_.empty()) { |
71 scoped_refptr<media::DataBuffer> buffer = data_list_.front(); | 71 scoped_refptr<media::DataBuffer> buffer = data_list_.front(); |
72 int read_bytes = std::min( | 72 size_t read_bytes = std::min(buffer->GetDataSize() - last_read_position_, |
73 static_cast<int>(buffer->GetDataSize()) - last_read_position_, | 73 next_payload_); |
74 next_payload_); | |
75 | 74 |
76 buffers.push_back(buffer); | 75 buffers.push_back(buffer); |
77 buffer_pointers.push_back(buffer->GetData() + last_read_position_); | 76 buffer_pointers.push_back(buffer->GetData() + last_read_position_); |
78 buffer_sizes.push_back(read_bytes); | 77 buffer_sizes.push_back(read_bytes); |
79 | 78 |
80 // Adjust counters. | 79 // Adjust counters. |
81 last_read_position_ += read_bytes; | 80 last_read_position_ += read_bytes; |
82 next_payload_ -= read_bytes; | 81 next_payload_ -= read_bytes; |
83 available_bytes_ -= read_bytes; | 82 available_bytes_ -= read_bytes; |
84 | 83 |
85 // If the front buffer is fully read, remove it from the queue. | 84 // If the front buffer is fully read, remove it from the queue. |
86 if (buffer->GetDataSize() == last_read_position_) { | 85 if (buffer->GetDataSize() == last_read_position_) { |
87 data_list_.pop_front(); | 86 data_list_.pop_front(); |
88 last_read_position_ = 0; | 87 last_read_position_ = 0; |
89 } | 88 } |
90 } | 89 } |
91 DCHECK_EQ(0, next_payload_); | 90 DCHECK_EQ(0UL, next_payload_); |
92 DCHECK_EQ(buffers.size(), buffer_pointers.size()); | 91 DCHECK_EQ(buffers.size(), buffer_pointers.size()); |
93 DCHECK_EQ(buffers.size(), buffer_sizes.size()); | 92 DCHECK_EQ(buffers.size(), buffer_sizes.size()); |
94 | 93 |
95 // Create a MultipleArrayInputStream for parsing. | 94 // Create a MultipleArrayInputStream for parsing. |
96 MultipleArrayInputStream stream(buffers.size()); | 95 MultipleArrayInputStream stream(buffers.size()); |
97 for (size_t i = 0; i < buffers.size(); ++i) { | 96 for (size_t i = 0; i < buffers.size(); ++i) { |
98 stream.SetBuffer(i, buffer_pointers[i], buffer_sizes[i]); | 97 stream.SetBuffer(i, buffer_pointers[i], buffer_sizes[i]); |
99 } | 98 } |
100 | 99 |
101 // And finally it is parsing. | 100 // And finally it is parsing. |
102 *message = new T(); | 101 *message = new T(); |
103 bool ret = (*message)->ParseFromZeroCopyStream(&stream); | 102 bool ret = (*message)->ParseFromZeroCopyStream(&stream); |
104 if (!ret) | 103 if (!ret) |
105 delete *message; | 104 delete *message; |
106 return ret; | 105 return ret; |
107 } | 106 } |
108 | 107 |
109 bool ProtocolDecoder::GetPayloadSize(int* size) { | 108 bool ProtocolDecoder::GetPayloadSize(int* size) { |
110 // The header has a size of 4 bytes. | 109 // The header has a size of 4 bytes. |
111 const int kHeaderSize = sizeof(int32); | 110 const size_t kHeaderSize = sizeof(int32); |
112 | 111 |
113 if (available_bytes_ < kHeaderSize) | 112 if (available_bytes_ < kHeaderSize) |
114 return false; | 113 return false; |
115 | 114 |
116 std::string header; | 115 std::string header; |
117 while (header.length() < kHeaderSize && !data_list_.empty()) { | 116 while (header.length() < kHeaderSize && !data_list_.empty()) { |
118 scoped_refptr<media::DataBuffer> buffer = data_list_.front(); | 117 scoped_refptr<media::DataBuffer> buffer = data_list_.front(); |
119 | 118 |
120 // Find out how many bytes we need and how many bytes are available in this | 119 // Find out how many bytes we need and how many bytes are available in this |
121 // buffer. | 120 // buffer. |
(...skipping 18 matching lines...) Expand all Loading... |
140 | 139 |
141 if (header.length() == kHeaderSize) { | 140 if (header.length() == kHeaderSize) { |
142 *size = talk_base::GetBE32(header.c_str()); | 141 *size = talk_base::GetBE32(header.c_str()); |
143 return true; | 142 return true; |
144 } | 143 } |
145 NOTREACHED() << "Unable to extract payload size"; | 144 NOTREACHED() << "Unable to extract payload size"; |
146 return false; | 145 return false; |
147 } | 146 } |
148 | 147 |
149 } // namespace remoting | 148 } // namespace remoting |
OLD | NEW |