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/overlay/Compression.h>
24 #include <ripple/protocol/PublicKey.h>
25 #include <ripple/protocol/messages.h>
26 #include <boost/asio/buffer.hpp>
27 #include <boost/asio/buffers_iterator.hpp>
28 #include <algorithm>
29 #include <array>
30 #include <cstdint>
31 #include <iterator>
32 #include <memory>
33 #include <type_traits>
34 
35 namespace ripple {
36 
37 // VFALCO NOTE If we forward declare Message and write out shared_ptr
38 // instead of using the in-class type alias, we can remove the
39 // entire ripple.pb.h from the main headers.
40 //
41 
42 // packaging of messages into length/type-prepended buffers
43 // ready for transmission.
44 //
45 // Message implements simple "packing" of protocol buffers Messages into
46 // a string prepended by a header specifying the message length.
47 // MessageType should be a Message class generated by the protobuf compiler.
48 //
49 
50 class Message : public std::enable_shared_from_this<Message>
51 {
54 
55 public:
62  Message(
63  ::google::protobuf::Message const& message,
64  int type,
65  boost::optional<PublicKey> const& validator = {});
66 
69  getBufferSize();
70 
78  getBuffer(Compressed tryCompressed);
79 
82  getCategory() const
83  {
84  return category_;
85  }
86 
88  boost::optional<PublicKey> const&
90  {
91  return validatorKey_;
92  }
93 
94 private:
99  boost::optional<PublicKey> validatorKey_;
100 
109  void
110  setHeader(
111  std::uint8_t* in,
112  std::uint32_t payloadBytes,
113  int type,
114  Algorithm compression,
115  std::uint32_t uncompressedBytes);
116 
121  void
122  compress();
123 
130  int
131  getType(std::uint8_t const* in) const;
132 };
133 
134 } // namespace ripple
135 
136 #endif
ripple::Message::getBuffer
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition: Message.cpp:187
ripple::Message::compress
void compress()
Try to compress the payload.
Definition: Message.cpp:52
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:201
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:151
iterator
ripple::Message::once_flag_
std::once_flag once_flag_
Definition: overlay/Message.h:98
ripple::Message::getCategory
std::size_t getCategory() const
Get the traffic category.
Definition: overlay/Message.h:82
ripple::Message::getValidatorKey
boost::optional< PublicKey > const & getValidatorKey() const
Get the validator's key.
Definition: overlay/Message.h:89
ripple::Message::validatorKey_
boost::optional< PublicKey > validatorKey_
Definition: overlay/Message.h:99
algorithm
std::once_flag
array
std::enable_shared_from_this
ripple::compression::Compressed
Compressed
Definition: Compression.h:38
cstdint
ripple::Message::Compressed
compression::Compressed Compressed
Definition: overlay/Message.h:52
std::uint8_t
ripple::compression::Algorithm
Algorithm
Definition: Compression.h:36
memory
ripple::Message::buffer_
std::vector< uint8_t > buffer_
Definition: overlay/Message.h:95
ripple::Message
Definition: overlay/Message.h:50
ripple::Message::getBufferSize
std::size_t getBufferSize()
Retrieve the size of the packed but uncompressed message data.
Definition: Message.cpp:181
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:96
std::size_t
type_traits
ripple::Message::category_
std::size_t category_
Definition: overlay/Message.h:97
ripple::Message::Message
Message(::google::protobuf::Message const &message, int type, boost::optional< PublicKey > const &validator={})
Constructor.
Definition: Message.cpp:26