xrpld
Loading...
Searching...
No Matches
xrpl::Message Class Reference

#include <Message.h>

Inheritance diagram for xrpl::Message:
Collaboration diagram for xrpl::Message:

Public Member Functions

 Message (::google::protobuf::Message const &message, protocol::MessageType type, std::optional< PublicKey > const &validator={})
 Constructor.
std::size_t getBufferSize ()
 Retrieve the size of the packed but uncompressed message data.
std::vector< uint8_t > const & getBuffer (Compressed tryCompressed)
 Retrieve the packed message data.
std::size_t getCategory () const
 Get the traffic category.
std::optional< PublicKey > const & getValidatorKey () const
 Get the validator's key.
shared_from_this (T... args)
weak_from_this (T... args)

Static Public Member Functions

static std::size_t messageSize (::google::protobuf::Message const &message)
static std::size_t totalSize (::google::protobuf::Message const &message)

Private Types

using Compressed = compression::Compressed
using Algorithm = compression::Algorithm

Private Member Functions

void compress ()
 Try to compress the payload.

Static Private Member Functions

static void setHeader (std::uint8_t *in, std::uint32_t payloadBytes, int type, Algorithm compression, std::uint32_t uncompressedBytes)
 Set the payload header.
static int getType (std::uint8_t const *in)
 Get the message type from the payload header.

Private Attributes

std::vector< uint8_t > buffer_
std::vector< uint8_t > bufferCompressed_
std::size_t category_
std::once_flag onceFlag_
std::optional< PublicKeyvalidatorKey_

Detailed Description

Definition at line 29 of file Message.h.

Member Typedef Documentation

◆ Compressed

Definition at line 31 of file Message.h.

◆ Algorithm

Definition at line 32 of file Message.h.

Constructor & Destructor Documentation

◆ Message()

xrpl::Message::Message ( ::google::protobuf::Message const & message,
protocol::MessageType type,
std::optional< PublicKey > const & validator = {} )

Constructor.

Parameters
messageProtocol message to serialize
typeProtocol message type
validatorPublic Key of the source validator for Validation or Proposal message. Used to check if the message should be squelched.

Definition at line 21 of file Message.cpp.

Member Function Documentation

◆ getBufferSize()

std::size_t xrpl::Message::getBufferSize ( )

Retrieve the size of the packed but uncompressed message data.

Definition at line 195 of file Message.cpp.

◆ messageSize()

std::size_t xrpl::Message::messageSize ( ::google::protobuf::Message const & message)
static

Definition at line 48 of file Message.cpp.

◆ totalSize()

std::size_t xrpl::Message::totalSize ( ::google::protobuf::Message const & message)
static

Definition at line 59 of file Message.cpp.

◆ getBuffer()

std::vector< uint8_t > const & xrpl::Message::getBuffer ( Compressed tryCompressed)

Retrieve the packed message data.

If compressed message is requested but the message is not compressible then the uncompressed buffer is returned.

Parameters
compressedRequest compressed (Compress::On) or uncompressed (Compress::Off) payload buffer
Returns
Payload buffer

Definition at line 201 of file Message.cpp.

◆ getCategory()

std::size_t xrpl::Message::getCategory ( ) const

Get the traffic category.

Definition at line 67 of file Message.h.

◆ getValidatorKey()

std::optional< PublicKey > const & xrpl::Message::getValidatorKey ( ) const

Get the validator's key.

Definition at line 74 of file Message.h.

◆ setHeader()

void xrpl::Message::setHeader ( std::uint8_t * in,
std::uint32_t payloadBytes,
int type,
Algorithm compression,
std::uint32_t uncompressedBytes )
staticprivate

Set the payload header.

Set payload header.

Parameters
inPointer to the payload
payloadBytesSize of the payload excluding the header size
typeProtocol message type
compressionCompression algorithm used in compression, currently LZ4 only. If None then the message is uncompressed.
uncompressedBytesSize of the uncompressed message

The header is a variable-sized structure that contains information about the type of the message and the length and encoding of the payload.

The first bit determines whether a message is compressed or uncompressed; for compressed messages, the next three bits identify the compression algorithm.

All multi-byte values are represented in big endian.

For uncompressed messages (6 bytes), numbering bits from left to right:

- The first 6 bits are set to 0.
- The next 26 bits represent the payload size.
- The remaining 16 bits represent the message type.

For compressed messages (10 bytes), numbering bits from left to right:

- The first 32 bits, together, represent the compression algorithm
  and payload size:
    - The first bit is set to 1 to indicate the message is compressed.
    - The next 3 bits indicate the compression algorithm.
    - The next 2 bits are reserved at this time and set to 0.
    - The remaining 26 bits represent the payload size.
- The next 16 bits represent the message type.
- The remaining 32 bits are the uncompressed message size.

The maximum size of a message at this time is 64 MB. Messages larger than this will be dropped and the recipient may, at its option, sever the link.

Note
While nominally a part of the wire protocol, the framing is subject to change; future versions of the code may negotiate the use of substantially different framing.

Definition at line 166 of file Message.cpp.

◆ compress()

void xrpl::Message::compress ( )
private

Try to compress the payload.

Can be called concurrently by multiple peers but is compressed once. If the message is not compressible then the serialized buffer_ is used.

Definition at line 65 of file Message.cpp.

◆ getType()

int xrpl::Message::getType ( std::uint8_t const * in)
staticprivate

Get the message type from the payload header.

First four bytes are the compression/algorithm flag and the payload size. Next two bytes are the message type

Parameters
inPayload header pointer
Returns
Message type

Definition at line 217 of file Message.cpp.

Member Data Documentation

◆ buffer_

std::vector<uint8_t> xrpl::Message::buffer_
private

Definition at line 80 of file Message.h.

◆ bufferCompressed_

std::vector<uint8_t> xrpl::Message::bufferCompressed_
private

Definition at line 81 of file Message.h.

◆ category_

std::size_t xrpl::Message::category_
private

Definition at line 82 of file Message.h.

◆ onceFlag_

std::once_flag xrpl::Message::onceFlag_
private

Definition at line 83 of file Message.h.

◆ validatorKey_

std::optional<PublicKey> xrpl::Message::validatorKey_
private

Definition at line 84 of file Message.h.