This source file includes following definitions.
- next_payload_known_
- AddData
- GetNextMessage
- GetPayloadSize
#include "remoting/protocol/message_decoder.h"
#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/proto/internal.pb.h"
#include "third_party/libjingle/source/talk/base/byteorder.h"
namespace remoting {
namespace protocol {
MessageDecoder::MessageDecoder()
: next_payload_(0),
next_payload_known_(false) {
}
MessageDecoder::~MessageDecoder() {}
void MessageDecoder::AddData(scoped_refptr<net::IOBuffer> data,
int data_size) {
buffer_.Append(data.get(), data_size);
}
CompoundBuffer* MessageDecoder::GetNextMessage() {
int next_payload = -1;
if (!next_payload_known_ && GetPayloadSize(&next_payload)) {
DCHECK_NE(-1, next_payload);
next_payload_ = next_payload;
next_payload_known_ = true;
}
if (!next_payload_known_ || buffer_.total_bytes() < next_payload_)
return NULL;
CompoundBuffer* message_buffer = new CompoundBuffer();
message_buffer->CopyFrom(buffer_, 0, next_payload_);
message_buffer->Lock();
buffer_.CropFront(next_payload_);
next_payload_known_ = false;
return message_buffer;
}
bool MessageDecoder::GetPayloadSize(int* size) {
const int kHeaderSize = sizeof(int32);
if (buffer_.total_bytes() < kHeaderSize)
return false;
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;
}
}
}