mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-19 18:45:52 +00:00
55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
#include "PackedMessage.h"
|
|
|
|
|
|
void PackedMessage::encodeHeader(unsigned size, int type)
|
|
{
|
|
assert(mBuffer.size() >= HEADER_SIZE);
|
|
mBuffer[0] = static_cast<boost::uint8_t>((size >> 24) & 0xFF);
|
|
mBuffer[1] = static_cast<boost::uint8_t>((size >> 16) & 0xFF);
|
|
mBuffer[2] = static_cast<boost::uint8_t>((size >> 8) & 0xFF);
|
|
mBuffer[3] = static_cast<boost::uint8_t>(size & 0xFF);
|
|
mBuffer[4] = static_cast<boost::uint8_t>((type >> 8) & 0xFF);
|
|
mBuffer[5] = static_cast<boost::uint8_t>(type & 0xFF);
|
|
}
|
|
|
|
|
|
PackedMessage::PackedMessage(MessagePointer msg, int type)
|
|
: mMsg(msg)
|
|
{
|
|
unsigned msg_size = mMsg->ByteSize();
|
|
assert(msg_size);
|
|
mBuffer.resize(HEADER_SIZE + msg_size);
|
|
encodeHeader(msg_size,type);
|
|
if(msg_size) mMsg->SerializeToArray(&mBuffer[HEADER_SIZE], msg_size);
|
|
}
|
|
|
|
bool PackedMessage::operator == (const PackedMessage& other)
|
|
{
|
|
return(mBuffer==other.mBuffer);
|
|
}
|
|
|
|
unsigned PackedMessage::getLength(std::vector<uint8_t>& buf)
|
|
{
|
|
if(buf.size() < HEADER_SIZE) return 0;
|
|
|
|
int ret=buf[0];
|
|
ret<<=8;
|
|
ret|=buf[1];
|
|
ret<<=8;
|
|
ret|=buf[2];
|
|
ret<<=8;
|
|
ret|=buf[3];
|
|
|
|
return(ret);
|
|
}
|
|
|
|
int PackedMessage::getType(std::vector<uint8_t>& buf)
|
|
{
|
|
if(buf.size() < HEADER_SIZE) return 0;
|
|
|
|
int ret=buf[4];
|
|
ret<<=8;
|
|
ret|=buf[5];
|
|
return(ret);
|
|
}
|