diff options
Diffstat (limited to 'remoting/protocol/message_decoder.cc')
-rw-r--r-- | remoting/protocol/message_decoder.cc | 79 |
1 files changed, 17 insertions, 62 deletions
diff --git a/remoting/protocol/message_decoder.cc b/remoting/protocol/message_decoder.cc index 2e31bbc..cf44d45 100644 --- a/remoting/protocol/message_decoder.cc +++ b/remoting/protocol/message_decoder.cc @@ -13,8 +13,7 @@ namespace remoting { MessageDecoder::MessageDecoder() - : available_bytes_(0), - next_payload_(0), + : next_payload_(0), next_payload_known_(false) { } @@ -22,12 +21,10 @@ MessageDecoder::~MessageDecoder() {} void MessageDecoder::AddBuffer(scoped_refptr<net::IOBuffer> data, int data_size) { - buffer_list_.push_back(make_scoped_refptr( - new net::DrainableIOBuffer(data, data_size))); - available_bytes_ += data_size; + buffer_.Append(data, data_size); } -CompoundBuffer* MessageDecoder::CreateCompoundBufferFromData() { +bool MessageDecoder::GetNextMessageData(CompoundBuffer* message_buffer) { // Determine the payload size. If we already know it then skip this part. // We may not have enough data to determine the payload size so use a // utility function to find out. @@ -40,72 +37,30 @@ CompoundBuffer* MessageDecoder::CreateCompoundBufferFromData() { // If the next payload size is still not known or we don't have enough // data for parsing then exit. - if (!next_payload_known_ || available_bytes_ < next_payload_) - return NULL; - next_payload_known_ = false; - - // The following loop gather buffers in |buffer_list_| that sum up to - // |next_payload_| bytes. These buffers are added to |stream|. - - // Create a CompoundBuffer for parsing. - CompoundBuffer* result = new CompoundBuffer(); - while (next_payload_ > 0 && !buffer_list_.empty()) { - scoped_refptr<net::DrainableIOBuffer> buffer = buffer_list_.front(); - int read_bytes = std::min(buffer->BytesRemaining(), next_payload_); - - // This will reference the same base pointer but maintain it's own - // version of data pointer. - result->Append(buffer, read_bytes); - - // Adjust counters. - buffer->DidConsume(read_bytes); - next_payload_ -= read_bytes; - available_bytes_ -= read_bytes; + if (!next_payload_known_ || buffer_.total_bytes() < next_payload_) + return false; - // If the front buffer is fully read then remove it from the queue. - if (!buffer->BytesRemaining()) - buffer_list_.pop_front(); - } - DCHECK_EQ(0, next_payload_); - DCHECK_LE(0, available_bytes_); - result->Lock(); - return result; -} + message_buffer->CopyFrom(buffer_, 0, next_payload_); + message_buffer->Lock(); + buffer_.CropFront(next_payload_); + next_payload_known_ = false; -static int GetHeaderSize(const std::string& header) { - return header.length(); + return true; } bool MessageDecoder::GetPayloadSize(int* size) { // The header has a size of 4 bytes. const int kHeaderSize = sizeof(int32); - if (available_bytes_ < kHeaderSize) + if (buffer_.total_bytes() < kHeaderSize) return false; - std::string header; - while (GetHeaderSize(header) < kHeaderSize && !buffer_list_.empty()) { - scoped_refptr<net::DrainableIOBuffer> buffer = buffer_list_.front(); - - // Find out how many bytes we need and how many bytes are available in this - // buffer. - int needed_bytes = kHeaderSize - GetHeaderSize(header); - int available_bytes = buffer->BytesRemaining(); - - // Then append the required bytes into the header and advance the last - // read position. - int read_bytes = std::min(needed_bytes, available_bytes); - header.append(buffer->data(), read_bytes); - buffer->DidConsume(read_bytes); - available_bytes_ -= read_bytes; - - // If the buffer is depleted then remove it from the queue. - if (!buffer->BytesRemaining()) { - buffer_list_.pop_front(); - } - } - - *size = talk_base::GetBE32(header.c_str()); + CompoundBuffer header_buffer; + char header[kHeaderSize]; + header_buffer.CopyFrom(buffer_, 0, kHeaderSize); + header_buffer.CopyTo(header, kHeaderSize); + *size = talk_base::GetBE32(header); + buffer_.CropFront(kHeaderSize); return true; } |