xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
overlay
Message.h
1
#pragma once
2
3
#include <xrpld/overlay/Compression.h>
4
5
#include <xrpl/basics/ByteUtilities.h>
6
#include <xrpl/protocol/PublicKey.h>
7
#include <xrpl/server/Manifest.h>
8
9
#include <google/protobuf/message.h>
10
11
#include <xrpl.pb.h>
12
13
#include <
cstddef
>
14
#include <
cstdint
>
15
#include <
memory
>
16
#include <
mutex
>
17
#include <
optional
>
18
#include <
vector
>
19
20
namespace
xrpl
{
21
22
constexpr
std::size_t
kMaximumMessageSize
=
megabytes
(64);
23
// Ping messages should be much smaller than the maximum message size,
24
// so we define a separate limit for them.
25
constexpr
std::size_t
kMaximumPingMessageSize
=
kilobytes
(1);
26
27
// Allowance for protobuf framing around each manifest in a TMManifests message.
28
constexpr
std::size_t
kManifestFramingBytes
= 8;
29
47
constexpr
std::size_t
48
maximumManifestsMessageSize
(
std::size_t
const
trustedCount,
std::size_t
const
untrustedCount)
49
{
50
return
(trustedCount + untrustedCount) * (
kMaxManifestBytes
+
kManifestFramingBytes
);
51
}
52
53
// The message size the defaults imply must stay within the overall protocol
54
// message limit. The same check for the largest configurable counts lives in
55
// OverlayImpl.h, where the configured bound is visible.
56
static_assert
(
57
maximumManifestsMessageSize
(
kMaxTrustedCount
,
kMaxUntrustedCount
) <
kMaximumMessageSize
);
58
59
// VFALCO NOTE If we forward declare Message and write out shared_ptr
60
// instead of using the in-class type alias, we can remove the
61
// entire ripple.pb.h from the main headers.
62
//
63
64
// packaging of messages into length/type-prepended buffers
65
// ready for transmission.
66
//
67
// Message implements simple "packing" of protocol buffers Messages into
68
// a string prepended by a header specifying the message length.
69
// MessageType should be a Message class generated by the protobuf compiler.
70
//
71
72
class
Message
:
public
std::enable_shared_from_this
<Message>
73
{
74
using
Compressed
=
compression::Compressed
;
75
using
Algorithm
=
compression::Algorithm
;
76
77
public
:
85
Message
(
86
::google::protobuf::Message
const
& message,
87
protocol::MessageType type,
88
std::optional<PublicKey>
const
& validator = {});
89
93
std::size_t
94
getBufferSize
();
95
96
static
std::size_t
97
messageSize
(::google::protobuf::Message
const
& message);
98
99
static
std::size_t
100
totalSize
(::google::protobuf::Message
const
& message);
101
109
std::vector<uint8_t>
const
&
110
getBuffer
(
Compressed
tryCompressed);
111
115
std::size_t
116
getCategory
()
const
117
{
118
return
category_
;
119
}
120
124
std::optional<PublicKey>
const
&
125
getValidatorKey
()
const
126
{
127
return
validatorKey_
;
128
}
129
130
private
:
131
std::vector<uint8_t>
buffer_
;
132
std::vector<uint8_t>
bufferCompressed_
;
133
std::size_t
category_
;
134
std::once_flag
onceFlag_
;
135
std::optional<PublicKey>
validatorKey_
;
136
146
static
void
147
setHeader
(
148
std::uint8_t
* in,
149
std::uint32_t
payloadBytes,
150
int
type,
151
Algorithm
compression
,
152
std::uint32_t
uncompressedBytes);
153
159
void
160
compress
();
161
169
static
int
170
getType
(
std::uint8_t
const
* in);
171
};
172
173
}
// namespace xrpl
xrpl::Message::getCategory
std::size_t getCategory() const
Get the traffic category.
Definition
Message.h:116
xrpl::Message::Compressed
compression::Compressed Compressed
Definition
Message.h:74
xrpl::Message::onceFlag_
std::once_flag onceFlag_
Definition
Message.h:134
xrpl::Message::validatorKey_
std::optional< PublicKey > validatorKey_
Definition
Message.h:135
xrpl::Message::Message
Message(::google::protobuf::Message const &message, protocol::MessageType type, std::optional< PublicKey > const &validator={})
Constructor.
Definition
Message.cpp:21
xrpl::Message::category_
std::size_t category_
Definition
Message.h:133
xrpl::Message::Algorithm
compression::Algorithm Algorithm
Definition
Message.h:75
xrpl::Message::getType
static int getType(std::uint8_t const *in)
Get the message type from the payload header.
Definition
Message.cpp:217
xrpl::Message::getValidatorKey
std::optional< PublicKey > const & getValidatorKey() const
Get the validator's key.
Definition
Message.h:125
xrpl::Message::totalSize
static std::size_t totalSize(::google::protobuf::Message const &message)
Definition
Message.cpp:59
xrpl::Message::getBuffer
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition
Message.cpp:201
xrpl::Message::buffer_
std::vector< uint8_t > buffer_
Definition
Message.h:131
xrpl::Message::setHeader
static void setHeader(std::uint8_t *in, std::uint32_t payloadBytes, int type, Algorithm compression, std::uint32_t uncompressedBytes)
Set the payload header.
Definition
Message.cpp:166
xrpl::Message::compress
void compress()
Try to compress the payload.
Definition
Message.cpp:65
xrpl::Message::messageSize
static std::size_t messageSize(::google::protobuf::Message const &message)
Definition
Message.cpp:48
xrpl::Message::getBufferSize
std::size_t getBufferSize()
Retrieve the size of the packed but uncompressed message data.
Definition
Message.cpp:195
xrpl::Message::bufferCompressed_
std::vector< uint8_t > bufferCompressed_
Definition
Message.h:132
cstddef
cstdint
std::enable_shared_from_this
std::uint8_t
memory
mutex
xrpl::compression
Definition
Compression.h:10
xrpl::compression::Algorithm
Algorithm
Definition
Compression.h:17
xrpl::compression::Compressed
Compressed
Definition
Compression.h:19
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::kMaximumMessageSize
constexpr std::size_t kMaximumMessageSize
Definition
Message.h:22
xrpl::kMaximumPingMessageSize
constexpr std::size_t kMaximumPingMessageSize
Definition
Message.h:25
xrpl::kMaxTrustedCount
constexpr std::size_t kMaxTrustedCount
Default number of trusted manifests allowed in a Manifest message.
Definition
Manifest.h:232
xrpl::maximumManifestsMessageSize
constexpr std::size_t maximumManifestsMessageSize(std::size_t const trustedCount, std::size_t const untrustedCount)
Upper bound on the wire size of a TMManifests message.
Definition
Message.h:48
xrpl::kMaxManifestBytes
constexpr std::size_t kMaxManifestBytes
Largest a valid manifest can be, in decoded bytes.
Definition
Manifest.h:191
xrpl::kMaxUntrustedCount
constexpr std::size_t kMaxUntrustedCount
Default number of untrusted manifests to store in cache and allowed in one Manifest message.
Definition
Manifest.h:216
xrpl::megabytes
constexpr auto megabytes(T value) noexcept
Definition
ByteUtilities.h:14
xrpl::kilobytes
constexpr auto kilobytes(T value) noexcept
Definition
ByteUtilities.h:7
xrpl::kManifestFramingBytes
constexpr std::size_t kManifestFramingBytes
Definition
Message.h:28
std::once_flag
optional
std::size_t
vector
Generated by
1.17.0