rippled
Loading...
Searching...
No Matches
Message.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_OVERLAY_MESSAGE_H_INCLUDED
21#define RIPPLE_OVERLAY_MESSAGE_H_INCLUDED
22
23#include <xrpld/overlay/Compression.h>
24#include <xrpl/basics/ByteUtilities.h>
25#include <xrpl/protocol/PublicKey.h>
26#include <xrpl/protocol/messages.h>
27#include <boost/asio/buffer.hpp>
28#include <boost/asio/buffers_iterator.hpp>
29#include <algorithm>
30#include <array>
31#include <cstdint>
32#include <iterator>
33#include <memory>
34#include <type_traits>
35
36namespace ripple {
37
39
40// VFALCO NOTE If we forward declare Message and write out shared_ptr
41// instead of using the in-class type alias, we can remove the
42// entire ripple.pb.h from the main headers.
43//
44
45// packaging of messages into length/type-prepended buffers
46// ready for transmission.
47//
48// Message implements simple "packing" of protocol buffers Messages into
49// a string prepended by a header specifying the message length.
50// MessageType should be a Message class generated by the protobuf compiler.
51//
52
54{
57
58public:
65 Message(
66 ::google::protobuf::Message const& message,
67 int type,
68 std::optional<PublicKey> const& validator = {});
69
73
74 static std::size_t
75 messageSize(::google::protobuf::Message const& message);
76
77 static std::size_t
78 totalSize(::google::protobuf::Message const& message);
79
87 getBuffer(Compressed tryCompressed);
88
92 {
93 return category_;
94 }
95
99 {
100 return validatorKey_;
101 }
102
103private:
109
118 void
119 setHeader(
121 std::uint32_t payloadBytes,
122 int type,
123 Algorithm compression,
124 std::uint32_t uncompressedBytes);
125
130 void
131 compress();
132
139 int
140 getType(std::uint8_t const* in) const;
141};
142
143} // namespace ripple
144
145#endif
std::size_t category_
Definition: Message.h:106
std::once_flag once_flag_
Definition: Message.h:107
std::size_t getCategory() const
Get the traffic category.
Definition: Message.h:91
compression::Compressed Compressed
Definition: Message.h:55
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:173
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition: Message.cpp:209
std::size_t getBufferSize()
Retrieve the size of the packed but uncompressed message data.
Definition: Message.cpp:203
std::optional< PublicKey > const & getValidatorKey() const
Get the validator's key.
Definition: Message.h:98
static std::size_t totalSize(::google::protobuf::Message const &message)
Definition: Message.cpp:65
std::optional< PublicKey > validatorKey_
Definition: Message.h:108
std::vector< uint8_t > buffer_
Definition: Message.h:104
std::vector< uint8_t > bufferCompressed_
Definition: Message.h:105
int getType(std::uint8_t const *in) const
Get the message type from the payload header.
Definition: Message.cpp:223
void compress()
Try to compress the payload.
Definition: Message.cpp:71
static std::size_t messageSize(::google::protobuf::Message const &message)
Definition: Message.cpp:54
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
constexpr auto megabytes(T value) noexcept
Definition: ByteUtilities.h:34
constexpr std::size_t maximiumMessageSize
Definition: Message.h:38