BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
message.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <boost/endian/buffers.hpp>
26 
28 
29 #include <fc/io/varint.hpp>
30 #include <fc/network/ip.hpp>
31 #include <fc/crypto/ripemd160.hpp>
32 
33 namespace graphene { namespace net {
34 
42  {
43  boost::endian::little_uint32_buf_t size; // number of bytes in message, capped at MAX_MESSAGE_SIZE
44  boost::endian::little_uint32_buf_t msg_type; // every channel gets a 16 bit message type specifier
46  {
47  size = 0;
48  msg_type = 0;
49  }
50  };
51 
53 
58  struct message : public message_header
59  {
60  std::vector<char> data;
61 
62  message(){}
63 
65  :message_header(m),data( std::move(m.data) ){}
66 
67  message( const message& m )
68  :message_header(m),data( m.data ){}
69 
73  template<typename T>
74  message( const T& m )
75  {
76  msg_type = T::type;
77  data = fc::raw::pack(m);
78  size = (uint32_t)data.size();
79  }
80 
82  {
83  return fc::ripemd160::hash( data.data(), (uint32_t)data.size() );
84  }
85 
90  template<typename T>
91  T as()const
92  {
93  try {
94  FC_ASSERT( msg_type.value() == T::type );
95  T tmp;
96  if( data.size() )
97  {
98  fc::datastream<const char*> ds( data.data(), data.size() );
99  fc::raw::unpack( ds, tmp );
100  }
101  else
102  {
103  // just to make sure that tmp shouldn't have any data
104  fc::datastream<const char*> ds( nullptr, 0 );
105  fc::raw::unpack( ds, tmp );
106  }
107  return tmp;
108  } FC_RETHROW_EXCEPTIONS( warn,
109  "error unpacking network message as a '${type}' ${x} !=? ${msg_type}",
110  ("type", fc::get_typename<T>::name() )
111  ("x", T::type)
112  ("msg_type", msg_type.value())
113  );
114  }
115  };
116 
117 } } // graphene::net
118 
121 
graphene::net::message::message
message()
Definition: message.hpp:62
graphene::net::message::message
message(const T &m)
Definition: message.hpp:74
ip.hpp
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
graphene::net::message
Definition: message.hpp:58
fc::datastream
Definition: datastream.hpp:20
graphene::net::message::data
std::vector< char > data
Definition: message.hpp:60
graphene::net::message::id
message_hash_type id() const
Definition: message.hpp:81
graphene::net::message_header::msg_type
boost::endian::little_uint32_buf_t msg_type
Definition: message.hpp:44
types.hpp
graphene::net::message_header::message_header
message_header()
Definition: message.hpp:45
graphene::net::message::as
T as() const
Definition: message.hpp:91
fc::get_typename
Definition: typename.hpp:20
fc::ripemd160
Definition: ripemd160.hpp:11
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
ripemd160.hpp
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::net::message::message
message(const message &m)
Definition: message.hpp:67
graphene::net::message_header::size
boost::endian::little_uint32_buf_t size
Definition: message.hpp:43
std
Definition: zeroed_array.hpp:76
fc::ripemd160::hash
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
FC_RETHROW_EXCEPTIONS
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:464
graphene::net::message::message
message(message &&m)
Definition: message.hpp:64
varint.hpp
graphene
Definition: api.cpp:48
graphene::net::message_header
Definition: message.hpp:41
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11