Electroneum
Loading...
Searching...
No Matches
levin_helper.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26
27
28
29#pragma once
30
31#include "levin_base.h"
32#include "serializeble_struct_helper.h"
33#include "int-util.h"
34
35#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
36#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net"
37
38namespace epee
39{
40namespace levin
41{
42 template<class t_struct>
43 bool pack_struct_to_levin_message(const t_struct& t, std::string& buff, int command_id)
44 {
45 buff.resize(sizeof(levin::bucket_head));
47 head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
48 head.m_cb = 0;
49 head.m_have_to_return_data = true;
50 head.m_command = SWAP32LE(command_id);
51 head.m_return_code = SWAP32LE(1);
52 head.m_reservedA = rand(); //probably some flags in future
53 head.m_reservedB = rand(); //probably some check summ in future
54
55 std::string buff_strg;
56 if(!StorageNamed::save_struct_as_storage_to_buff_t<t_struct, StorageNamed::DefaultStorageType>(t, buff_strg))
57 return false;
58
59 head.m_cb = SWAP64LE(buff_strg.size());
60 buff.append(buff_strg);
61 return true;
62 }
63
64
65 bool pack_data_to_levin_message(const std::string& data, std::string& buff, int command_id)
66 {
67 buff.resize(sizeof(levin::bucket_head));
69 head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
70 head.m_cb = 0;
71 head.m_have_to_return_data = true;
72 head.m_command = SWAP32LE(command_id);
73 head.m_return_code = SWAP32LE(1);
74 head.m_reservedA = rand(); //probably some flags in future
75 head.m_reservedB = rand(); //probably some check summ in future
76
77 head.m_cb = SWAP64LE(data.size());
78 buff.append(data);
79 return true;
80 }
81
82 bool load_levin_data_from_levin_message(std::string& levin_data, const std::string& buff, int& command)
83 {
84 if(buff.size() < sizeof(levin::bucket_head) )
85 {
86 LOG_PRINT_L3("size of buff(" << buff.size() << ") is too small, at load_struct_from_levin_message");
87 return false;
88 }
89
90#if BYTE_ORDER == LITTLE_ENDIAN
92#else
94 head.m_signature = SWAP64LE(head.m_signature);
95 head.m_cb = SWAP64LE(head.m_cb);
96 head.m_command = SWAP32LE(head.m_command);
97 head.m_return_code = SWAP32LE(head.m_return_code);
98 head.m_reservedA = SWAP32LE(head.m_reservedA);
99 head.m_reservedB = SWAP32LE(head.m_reservedB);
100#endif
101 if(head.m_signature != LEVIN_SIGNATURE)
102 {
103 LOG_PRINT_L3("Failed to read signature in levin message, at load_struct_from_levin_message");
104 return false;
105 }
106 if(head.m_cb != buff.size()-sizeof(levin::bucket_head))
107 {
108 LOG_PRINT_L3("sizes mismatch, at load_struct_from_levin_message");
109 return false;
110 }
111
112 //std::string buff_strg;
113 levin_data.assign(&buff[sizeof(levin::bucket_head)], buff.size()-sizeof(levin::bucket_head));
114 command = head.m_command;
115 return true;
116 }
117
118 template<class t_struct>
119 bool load_struct_from_levin_message(t_struct& t, const std::string& buff, int& command)
120 {
121 if(buff.size() < sizeof(levin::bucket_head) )
122 {
123 LOG_ERROR("size of buff(" << buff.size() << ") is too small, at load_struct_from_levin_message");
124 return false;
125 }
126
127#if BYTE_ORDER == LITTLE_ENDIAN
129#else
131 head.m_signature = SWAP64LE(head.m_signature);
132 head.m_cb = SWAP64LE(head.m_cb);
133 head.m_command = SWAP32LE(head.m_command);
134 head.m_return_code = SWAP32LE(head.m_return_code);
135 head.m_reservedA = SWAP32LE(head.m_reservedA);
136 head.m_reservedB = SWAP32LE(head.m_reservedB);
137#endif
138 if(head.m_signature != LEVIN_SIGNATURE)
139 {
140 LOG_ERROR("Failed to read signature in levin message, at load_struct_from_levin_message");
141 return false;
142 }
143 if(head.m_cb != buff.size()-sizeof(levin::bucket_head))
144 {
145 LOG_ERROR("sizes mismatch, at load_struct_from_levin_message");
146 return false;
147 }
148
149 std::string buff_strg;
150 buff_strg.assign(&buff[sizeof(levin::bucket_head)], buff.size()-sizeof(levin::bucket_head));
151
152 if(!StorageNamed::load_struct_from_storage_buff_t<t_struct, StorageNamed::DefaultStorageType>(t, buff_strg))
153 {
154 LOG_ERROR("Failed to read storage, at load_struct_from_levin_message");
155 return false;
156 }
157 command = head.m_command;
158 return true;
159 }
160}
161}
#define SWAP64LE
Definition int-util.h:232
#define SWAP32LE
Definition int-util.h:224
#define LEVIN_SIGNATURE
Definition levin_base.h:34
#define LOG_PRINT_L3(x)
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
bool load_levin_data_from_levin_message(std::string &levin_data, const std::string &buff, int &command)
bool load_struct_from_levin_message(t_struct &t, const std::string &buff, int &command)
bool pack_data_to_levin_message(const std::string &data, std::string &buff, int command_id)
bool pack_struct_to_levin_message(const t_struct &t, std::string &buff, int command_id)
struct rule_list head