rippled
overlay/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 <ripple/basics/ByteUtilities.h>
24 #include <ripple/overlay/Compression.h>
25 #include <ripple/protocol/PublicKey.h>
26 #include <ripple/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 
36 namespace 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 
53 class Message : public std::enable_shared_from_this<Message>
54 {
57 
58 public:
65  Message(
66  ::google::protobuf::Message const& message,
67  int type,
68  std::optional<PublicKey> const& validator = {});
69 
72  getBufferSize();
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 
91  getCategory() const
92  {
93  return category_;
94  }
95 
99  {
100  return validatorKey_;
101  }
102 
103 private:
109 
118  void
119  setHeader(
120  std::uint8_t* in,
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
ripple::Message::validatorKey_
std::optional< PublicKey > validatorKey_
Definition: overlay/Message.h:108
ripple::Message::getBuffer
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition: Message.cpp:210
ripple::Message::compress
void compress()
Try to compress the payload.
Definition: Message.cpp:68
ripple::maximiumMessageSize
constexpr std::size_t maximiumMessageSize
Definition: overlay/Message.h:38
ripple::Message::totalSize
static std::size_t totalSize(::google::protobuf::Message const &message)
Definition: Message.cpp:62
ripple::Message::messageSize
static std::size_t messageSize(::google::protobuf::Message const &message)
Definition: Message.cpp:51
std::vector< uint8_t >
ripple::Message::getType
int getType(std::uint8_t const *in) const
Get the message type from the payload header.
Definition: Message.cpp:224
ripple::Message::setHeader
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:174
iterator
ripple::Message::once_flag_
std::once_flag once_flag_
Definition: overlay/Message.h:107
ripple::Message::getCategory
std::size_t getCategory() const
Get the traffic category.
Definition: overlay/Message.h:91
algorithm
std::once_flag
ripple::megabytes
constexpr auto megabytes(T value) noexcept
Definition: ByteUtilities.h:34
array
ripple::Message::Message
Message(::google::protobuf::Message const &message, int type, std::optional< PublicKey > const &validator={})
Constructor.
Definition: Message.cpp:26
std::enable_shared_from_this
ripple::compression::Compressed
Compressed
Definition: Compression.h:38
cstdint
ripple::Message::Compressed
compression::Compressed Compressed
Definition: overlay/Message.h:55
std::uint8_t
ripple::compression::Algorithm
Algorithm
Definition: Compression.h:36
memory
ripple::Message::buffer_
std::vector< uint8_t > buffer_
Definition: overlay/Message.h:104
ripple::Message
Definition: overlay/Message.h:53
ripple::Message::getBufferSize
std::size_t getBufferSize()
Retrieve the size of the packed but uncompressed message data.
Definition: Message.cpp:204
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Message::bufferCompressed_
std::vector< uint8_t > bufferCompressed_
Definition: overlay/Message.h:105
std::optional
std::size_t
ripple::Message::getValidatorKey
std::optional< PublicKey > const & getValidatorKey() const
Get the validator's key.
Definition: overlay/Message.h:98
type_traits
ripple::Message::category_
std::size_t category_
Definition: overlay/Message.h:106