BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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/io/raw_fwd.hpp>
32 #include <fc/crypto/ripemd160.hpp>
33 #include <fc/reflect/typename.hpp>
34 
35 namespace graphene { namespace net {
36 
44  {
45  boost::endian::little_uint32_buf_t size; // number of bytes in message, capped at MAX_MESSAGE_SIZE
46  boost::endian::little_uint32_buf_t msg_type; // every channel gets a 16 bit message type specifier
48  {
49  size = 0;
50  msg_type = 0;
51  }
52  };
53 
55 
60  struct message : public message_header
61  {
62  std::vector<char> data;
63 
64  message(){}
65 
67  :message_header(m),data( std::move(m.data) ){}
68 
69  message( const message& m )
70  :message_header(m),data( m.data ){}
71 
75  template<typename T>
76  message( const T& m )
77  {
78  msg_type = T::type;
79  data = fc::raw::pack(m);
80  size = (uint32_t)data.size();
81  }
82 
84  {
85  return fc::ripemd160::hash( data.data(), (uint32_t)data.size() );
86  }
87 
92  template<typename T>
93  T as()const
94  {
95  try {
96  FC_ASSERT( msg_type.value() == T::type );
97  T tmp;
98  if( data.size() )
99  {
100  fc::datastream<const char*> ds( data.data(), data.size() );
101  fc::raw::unpack( ds, tmp );
102  }
103  else
104  {
105  // just to make sure that tmp shouldn't have any data
106  fc::datastream<const char*> ds( nullptr, 0 );
107  fc::raw::unpack( ds, tmp );
108  }
109  return tmp;
110  } FC_RETHROW_EXCEPTIONS( warn,
111  "error unpacking network message as a '${type}' ${x} !=? ${msg_type}",
112  ("type", fc::get_typename<T>::name() )
113  ("x", T::type)
114  ("msg_type", msg_type.value())
115  );
116  }
117  };
118 
119 } } // graphene::net
120 
123 
fc::uint160_t message_hash_type
Definition: message.hpp:54
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
Definition: api.cpp:56
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception&#39;s, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:463
FC_REFLECT_TYPENAME(fc::log_message)
boost::endian::little_uint32_buf_t msg_type
Definition: message.hpp:46
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:73
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::uint160_t id() const
Definition: message.hpp:83
message(message &&m)
Definition: message.hpp:66
message(const message &m)
Definition: message.hpp:69
boost::endian::little_uint32_buf_t size
Definition: message.hpp:45
message(const T &m)
Definition: message.hpp:76
std::vector< char > data
Definition: message.hpp:62