blob: 8bb879ab58d020365798ef82b953f74db0d0bdfb [file] [log] [blame]
[email protected]09ce8922012-03-03 08:29:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4d10ede2010-10-28 18:43:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "remoting/protocol/message_decoder.h"
6
avi5a080f012015-12-22 23:15:437#include <stdint.h>
8
[email protected]4d10ede2010-10-28 18:43:379#include "base/logging.h"
10#include "net/base/io_buffer.h"
[email protected]b3c03402010-11-16 01:27:4611#include "remoting/base/compound_buffer.h"
[email protected]4d10ede2010-10-28 18:43:3712#include "remoting/proto/internal.pb.h"
Henrik Kjellander5f4278232017-06-30 09:24:4713#include "third_party/webrtc/rtc_base/byteorder.h"
[email protected]4d10ede2010-10-28 18:43:3714
15namespace remoting {
[email protected]2c229682010-12-02 20:23:3516namespace protocol {
[email protected]4d10ede2010-10-28 18:43:3717
18MessageDecoder::MessageDecoder()
[email protected]e609bb92010-11-18 01:54:5519 : next_payload_(0),
[email protected]4d10ede2010-10-28 18:43:3720 next_payload_known_(false) {
21}
22
Chris Watkins6fe52aa2017-11-28 03:24:0523MessageDecoder::~MessageDecoder() = default;
[email protected]4d10ede2010-10-28 18:43:3724
[email protected]051916e62011-01-14 21:58:0125void MessageDecoder::AddData(scoped_refptr<net::IOBuffer> data,
26 int data_size) {
[email protected]f9d8a772013-06-01 04:33:1727 buffer_.Append(data.get(), data_size);
[email protected]4d10ede2010-10-28 18:43:3728}
29
[email protected]6852d7d2011-01-22 02:34:5630CompoundBuffer* MessageDecoder::GetNextMessage() {
[email protected]4d10ede2010-10-28 18:43:3731 // 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
33 // utility function to find out.
34 int next_payload = -1;
35 if (!next_payload_known_ && GetPayloadSize(&next_payload)) {
36 DCHECK_NE(-1, next_payload);
37 next_payload_ = next_payload;
38 next_payload_known_ = true;
39 }
40
41 // If the next payload size is still not known or we don't have enough
42 // data for parsing then exit.
[email protected]e609bb92010-11-18 01:54:5543 if (!next_payload_known_ || buffer_.total_bytes() < next_payload_)
sergeyuc5f104b2015-01-09 19:33:2444 return nullptr;
[email protected]e609bb92010-11-18 01:54:5545
[email protected]6852d7d2011-01-22 02:34:5646 CompoundBuffer* message_buffer = new CompoundBuffer();
[email protected]e609bb92010-11-18 01:54:5547 message_buffer->CopyFrom(buffer_, 0, next_payload_);
48 message_buffer->Lock();
49 buffer_.CropFront(next_payload_);
[email protected]4d10ede2010-10-28 18:43:3750 next_payload_known_ = false;
51
[email protected]6852d7d2011-01-22 02:34:5652 return message_buffer;
[email protected]4d10ede2010-10-28 18:43:3753}
54
55bool MessageDecoder::GetPayloadSize(int* size) {
56 // The header has a size of 4 bytes.
avi5a080f012015-12-22 23:15:4357 const int kHeaderSize = sizeof(int32_t);
[email protected]4d10ede2010-10-28 18:43:3758
[email protected]e609bb92010-11-18 01:54:5559 if (buffer_.total_bytes() < kHeaderSize)
[email protected]4d10ede2010-10-28 18:43:3760 return false;
61
[email protected]e609bb92010-11-18 01:54:5562 CompoundBuffer header_buffer;
63 char header[kHeaderSize];
64 header_buffer.CopyFrom(buffer_, 0, kHeaderSize);
65 header_buffer.CopyTo(header, kHeaderSize);
[email protected]e758d4c2014-08-06 16:48:1666 *size = rtc::GetBE32(header);
[email protected]e609bb92010-11-18 01:54:5567 buffer_.CropFront(kHeaderSize);
[email protected]4d10ede2010-10-28 18:43:3768 return true;
69}
70
[email protected]2c229682010-12-02 20:23:3571} // namespace protocol
[email protected]4d10ede2010-10-28 18:43:3772} // namespace remoting