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/messages.h>
25 #include <boost/asio/buffer.hpp>
26 #include <boost/asio/buffers_iterator.hpp>
27 #include <algorithm>
28 #include <array>
29 #include <cstdint>
30 #include <iterator>
31 #include <memory>
32 #include <type_traits>
33 
34 namespace ripple {
35 
36 // VFALCO NOTE If we forward declare Message and write out shared_ptr
37 // instead of using the in-class type alias, we can remove the entire
38 // ripple.pb.h from the main headers.
39 //
40 
41 // packaging of messages into length/type-prepended buffers
42 // ready for transmission.
43 //
44 // Message implements simple "packing" of protocol buffers Messages into
45 // a string prepended by a header specifying the message length.
46 // MessageType should be a Message class generated by the protobuf compiler.
47 //
48 
49 class Message : public std::enable_shared_from_this <Message>
50 {
53 public:
58  Message (::google::protobuf::Message const& message, int type);
59 
67  getBuffer (Compressed tryCompressed);
68 
71  getCategory () const
72  {
73  return category_;
74  }
75 
76 private:
81 
90  void setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type,
91  Algorithm comprAlgorithm, std::uint32_t uncompressedBytes);
92 
97  void compress();
98 
105  int getType(std::uint8_t const* in) const;
106 };
107 
108 }
109 
110 #endif
ripple::Message::getBuffer
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition: Message.cpp:144
ripple::Message::compress
void compress()
Try to compress the payload.
Definition: Message.cpp:48
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:158
iterator
ripple::Message::once_flag_
std::once_flag once_flag_
Definition: overlay/Message.h:80
ripple::Message::getCategory
std::size_t getCategory() const
Get the traffic category.
Definition: overlay/Message.h:71
algorithm
ripple::Message::setHeader
void setHeader(std::uint8_t *in, std::uint32_t payloadBytes, int type, Algorithm comprAlgorithm, std::uint32_t uncompressedBytes)
Set the payload header.
Definition: Message.cpp:119
std::once_flag
ripple::Message::Message
Message(::google::protobuf::Message const &message, int type)
Constructor.
Definition: Message.cpp:26
array
std::enable_shared_from_this
ripple::compression::Compressed
Compressed
Definition: Compression.h:39
cstdint
std::uint8_t
ripple::compression::Algorithm
Algorithm
Definition: Compression.h:34
memory
ripple::Message::buffer_
std::vector< uint8_t > buffer_
Definition: overlay/Message.h:77
ripple::Message
Definition: overlay/Message.h:49
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:78
std::size_t
type_traits
ripple::Message::category_
std::size_t category_
Definition: overlay/Message.h:79