-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpc.cpp
235 lines (200 loc) · 4.68 KB
/
rpc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream> // cout
#include "rpc.h"
#include "profiler.h"
CRpcIoNull RpcIoNull;
void rpcMessage::Create( uint16_t cmd )
{
m_type = RPC_TYPE_DTB;
m_cmd = cmd;
m_size = 0;
m_pos = 0;
}
void rpcMessage::Send( CRpcIo & rpc_io )
{
rpc_io.Write( &m_type, 1 );
rpc_io.Write( &m_cmd, 2 );
rpc_io.Write( &m_size, 1 );
if( m_size )
rpc_io.Write( m_par, m_size );
}
void rpcMessage::Receive( CRpcIo & rpc_io )
{
m_pos = 0;
rpc_io.Read( &m_type, 1 );
if( m_type == RPC_TYPE_DTB ) {
}
else if( m_type == RPC_TYPE_DTB_DATA ) { // remove unexpected data message from queue
uint16_t size = 0;
rpc_io.Read( &size, 3 );
rpc_DataSink( rpc_io, size );
throw CRpcError( CRpcError::NO_CMD_MSG );
}
else if( m_type == RPC_TYPE_DTB_DATA_OLD ) { // remove unexpected old data message from queue
uint8_t chn;
uint16_t size;
rpc_io.Read( &chn, 1 );
rpc_io.Read( &size, 2 );
rpc_DataSink( rpc_io, size );
throw CRpcError( CRpcError::NO_CMD_MSG );
}
else
throw CRpcError( CRpcError::WRONG_MSG_TYPE );
rpc_io.Read( &m_cmd, 2 );
rpc_io.Read( &m_size, 1 );
//std::cout << "[rpcMessage::Receive] size " << int(m_size) << std::endl;
if( m_size )
rpc_io.Read( m_par, m_size ); // usb Read
}
// === data =================================================================
void CDataHeader::RecvHeader( CRpcIo & rpc_io )
{
rpc_io.Read( &m_type, 1 );
if( m_type == RPC_TYPE_DTB_DATA ) {
}
else if( m_type == RPC_TYPE_DTB ) { // remove unexpected command message from queue
uint16_t cmd;
uint8_t size;
rpc_io.Read( &cmd, 2 );
rpc_io.Read( &size, 1 );
rpc_DataSink( rpc_io, size );
throw CRpcError( CRpcError::NO_DATA_MSG );
}
else if( m_type == RPC_TYPE_DTB_DATA_OLD ) { // remove unexpected old data message from queue
uint8_t chn;
uint16_t size;
rpc_io.Read( &chn, 1 );
rpc_io.Read( &size, 2 );
rpc_DataSink( rpc_io, size );
throw CRpcError( CRpcError::NO_CMD_MSG );
}
else
throw CRpcError( CRpcError::WRONG_MSG_TYPE );
m_size = 0;
rpc_io.Read( &m_size, 3 );
}
void rpc_SendRaw( CRpcIo & rpc_io, const void *x, uint32_t size )
{
uint8_t value = RPC_TYPE_DTB_DATA;
rpc_io.Write( &value, 1 );
rpc_io.Write( &size, 3 );
if( size )
rpc_io.Write( x, size );
// printf("Send Data [%i]\n", int(size));
}
void rpc_DataSink( CRpcIo & rpc_io, uint32_t size )
{
if( size == 0 )
return;
CBuffer buffer( size );
rpc_io.Read( &buffer, size );
}
void rpc_Receive( CRpcIo & rpc_io, string & x )
{
CDataHeader msg;
msg.RecvHeader( rpc_io );
x.clear( );
x.reserve( msg.m_size );
//std::cout << "rpc_Receive " << msg.m_size << " bytes" << std::endl;
char ch;
for( uint32_t i = 0; i < msg.m_size; i++ ) {
rpc_io.Read( &ch, 1 ); // usb.cpp Read
x.push_back( ch );
}
}
// === tools ================================================================
void rpc_TranslateCallName( const string & in, string & out )
{
out.clear( );
unsigned int size = in.rfind( '$' );
unsigned int pos = size;
try {
if( pos == string::npos )
throw int ( 1 );
pos++;
int i = 0;
while( pos < in.size( ) ) {
int comp = -1;
if( in[pos] >= '0' && in[pos] <= '5' ) {
comp = in[pos] - '0';
pos++;
if( pos >= in.size( ) )
throw int ( 2 );
}
const char *type;
switch ( in[pos] ) {
case 'v':
type = "void";
break;
case 'b':
type = "bool";
break;
case 'c':
type = "int8_t";
break;
case 'C':
type = "uint8_t";
break;
case 's':
type = "int16_t";
break;
case 'S':
type = "uint16_t";
break;
case 'i':
type = "int32_t";
break;
case 'I':
type = "uint32_t";
break;
case 'l':
type = "int64_t";
break;
case 'L':
type = "uint64_t";
break;
default:
type = "?";
}
if( i > 1 )
out += ", ";
switch ( comp ) {
case 0:
out += type;
out += "&";
break;
case 1:
out += "vector<";
out += type;
out += ">&";
break;
case 2:
out += "vectorR<";
out += type;
out += ">&";
break;
case 3:
out += "string&";
break;
case 4:
out += "stringR&";
break;
case 5:
out += "HWvectorR&";
break;
default:
out += type;
}
if( i == 0 ) {
out += ' ';
out += in.substr( 0, size );
out += '(';
}
pos++;
i++;
}
out += ");";
}
catch( int ) {
out = in;
}
}