diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 18:43:37 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 18:43:37 +0000 |
commit | 4d10edeb5f3db4366b4521c2346bfb4741c21e6f (patch) | |
tree | 98a0a1d7d295cc31c0840977dc15134c1271de31 /remoting/base | |
parent | 45b10f0917d6449aa53f66a6ea7fa6e8a4aea079 (diff) | |
download | chromium_src-4d10edeb5f3db4366b4521c2346bfb4741c21e6f.zip chromium_src-4d10edeb5f3db4366b4521c2346bfb4741c21e6f.tar.gz chromium_src-4d10edeb5f3db4366b4521c2346bfb4741c21e6f.tar.bz2 |
HostMessageDispatcher to parse control messages
Changed MessageReader and MessageDecoder to support parsing in
HostMessageDispatcher.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/4017002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64283 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/base')
-rw-r--r-- | remoting/base/multiple_array_input_stream.cc | 37 | ||||
-rw-r--r-- | remoting/base/multiple_array_input_stream.h | 28 | ||||
-rw-r--r-- | remoting/base/multiple_array_input_stream_unittest.cc | 3 |
3 files changed, 35 insertions, 33 deletions
diff --git a/remoting/base/multiple_array_input_stream.cc b/remoting/base/multiple_array_input_stream.cc index ab0ba45..d2127c4 100644 --- a/remoting/base/multiple_array_input_stream.cc +++ b/remoting/base/multiple_array_input_stream.cc @@ -5,13 +5,13 @@ #include <functional> #include "base/logging.h" +#include "net/base/io_buffer.h" #include "remoting/base/multiple_array_input_stream.h" namespace remoting { MultipleArrayInputStream::MultipleArrayInputStream() : current_buffer_(0), - current_buffer_offset_(0), position_(0), last_returned_size_(0) { } @@ -19,24 +19,21 @@ MultipleArrayInputStream::MultipleArrayInputStream() MultipleArrayInputStream::~MultipleArrayInputStream() { } -void MultipleArrayInputStream::AddBuffer( - const char* buffer, int size) { +void MultipleArrayInputStream::AddBuffer(net::IOBuffer* buffer, int size) { DCHECK_EQ(position_, 0); // Haven't started reading. - buffers_.push_back(buffer); - buffer_sizes_.push_back(size); - DCHECK_EQ(buffers_.size(), buffer_sizes_.size()); + buffers_.push_back(new net::DrainableIOBuffer(buffer, size)); } bool MultipleArrayInputStream::Next(const void** data, int* size) { if (current_buffer_ < buffers_.size()) { - // Also reply with that is remaining in the current buffer. - last_returned_size_ = - buffer_sizes_[current_buffer_] - current_buffer_offset_; - *data = buffers_[current_buffer_] + current_buffer_offset_; + // Reply with the number of bytes remaining in the current buffer. + scoped_refptr<net::DrainableIOBuffer> buffer = buffers_[current_buffer_]; + last_returned_size_ = buffer->BytesRemaining(); + *data = buffer->data(); *size = last_returned_size_; // After reading the current buffer then advance to the next buffer. - current_buffer_offset_ = 0; + buffer->DidConsume(last_returned_size_); ++current_buffer_; position_ += last_returned_size_; return true; @@ -52,14 +49,13 @@ bool MultipleArrayInputStream::Next(const void** data, int* size) { void MultipleArrayInputStream::BackUp(int count) { DCHECK_LE(count, last_returned_size_); - DCHECK_EQ(0, current_buffer_offset_); DCHECK_GT(current_buffer_, 0u); - // Rewind one buffer. + // Rewind one buffer and rewind data offset by |count| bytes. --current_buffer_; - current_buffer_offset_ = buffer_sizes_[current_buffer_] - count; + scoped_refptr<net::DrainableIOBuffer> buffer = buffers_[current_buffer_]; + buffer->SetOffset(buffer->size() - count); position_ -= count; - DCHECK_GE(current_buffer_offset_, 0); DCHECK_GE(position_, 0); } @@ -68,20 +64,17 @@ bool MultipleArrayInputStream::Skip(int count) { last_returned_size_ = 0; while (count && current_buffer_ < buffers_.size()) { - int read = std::min( - count, - buffer_sizes_[current_buffer_] - current_buffer_offset_); + scoped_refptr<net::DrainableIOBuffer> buffer = buffers_[current_buffer_]; + int read = std::min(count, buffer->BytesRemaining()); // Advance the current buffer offset and position. - current_buffer_offset_ += read; + buffer->DidConsume(read); position_ += read; count -= read; // If the current buffer is fully read, then advance to the next buffer. - if (current_buffer_offset_ == buffer_sizes_[current_buffer_]) { + if (!buffer->BytesRemaining()) ++current_buffer_; - current_buffer_offset_ = 0; - } } return count == 0; } diff --git a/remoting/base/multiple_array_input_stream.h b/remoting/base/multiple_array_input_stream.h index 7747da5..a848248 100644 --- a/remoting/base/multiple_array_input_stream.h +++ b/remoting/base/multiple_array_input_stream.h @@ -2,29 +2,39 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// MultipleArrayInputStream implements ZeroCopyInputStream to be used by +// protobuf to decode bytes into a protocol buffer message. +// +// This input stream is made of multiple IOBuffers received from the network. +// This object retains the IOBuffers added to it. +// +// Internally, we wrap each added IOBuffer in a DrainableIOBuffer. This allows +// us to track how much data has been consumed from each IOBuffer. + #ifndef REMOTING_BASE_MULTIPLE_ARRAY_INPUT_STREAM_H_ #define REMOTING_BASE_MULTIPLE_ARRAY_INPUT_STREAM_H_ #include <vector> #include "base/basictypes.h" +#include "base/ref_counted.h" #include "google/protobuf/io/zero_copy_stream.h" +namespace net { +class DrainableIOBuffer; +class IOBuffer; +} // namespace net + namespace remoting { -// A MultipleArrayInputStream provides a ZeroCopyInputStream with multiple -// backing arrays. class MultipleArrayInputStream : public google::protobuf::io::ZeroCopyInputStream { public: - // Construct a MultipleArrayInputStream with |count| backing arrays. - // TODO(hclam): Consider adding block size to see if it has a performance - // gain. MultipleArrayInputStream(); virtual ~MultipleArrayInputStream(); - // Add a new buffer to the list. - void AddBuffer(const char* buffer, int size); + // Add a buffer to the list. |buffer| is retained by this object. + void AddBuffer(net::IOBuffer* buffer, int size); // google::protobuf::io::ZeroCopyInputStream interface. virtual bool Next(const void** data, int* size); @@ -33,11 +43,9 @@ class MultipleArrayInputStream : virtual int64 ByteCount() const; private: - std::vector<const char*> buffers_; - std::vector<int> buffer_sizes_; + std::vector<scoped_refptr<net::DrainableIOBuffer> > buffers_; size_t current_buffer_; - int current_buffer_offset_; int position_; int last_returned_size_; diff --git a/remoting/base/multiple_array_input_stream_unittest.cc b/remoting/base/multiple_array_input_stream_unittest.cc index 1d705fe..4a840e4 100644 --- a/remoting/base/multiple_array_input_stream_unittest.cc +++ b/remoting/base/multiple_array_input_stream_unittest.cc @@ -5,6 +5,7 @@ #include <string> #include "base/scoped_ptr.h" +#include "net/base/io_buffer.h" #include "remoting/base/multiple_array_input_stream.h" #include "testing/gtest/include/gtest/gtest.h" @@ -71,7 +72,7 @@ static void PrepareData(scoped_ptr<MultipleArrayInputStream>* stream) { const char* data = kTestData.c_str(); for (int i = 0; i < segments; ++i) { int size = i % 2 == 0 ? 1 : 2; - mstream->AddBuffer(data, size); + mstream->AddBuffer(new net::StringIOBuffer(std::string(data, size)), size); data += size; } stream->reset(mstream); |