forked from frmeier/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.cpp
164 lines (137 loc) · 3.5 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
// rpc.cpp
#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 & 0xfe) != RPC_TYPE_DTB) throw CRpcError(CRpcError::WRONG_MSG_TYPE);
if (m_type & 0x01)
{ // remove unexpected 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);
}
rpc_io.Read(&m_cmd, 2);
rpc_io.Read(&m_size, 1);
if (m_size) rpc_io.Read(m_par, m_size);
}
// === data =================================================================
void CDataHeader::RecvHeader(CRpcIo &rpc_io)
{
rpc_io.Read(&m_type, 1);
if ((m_type & 0xfe) != RPC_TYPE_DTB) throw CRpcError(CRpcError::WRONG_MSG_TYPE);
if ((m_type & 0x01) == 0)
{ // 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);
}
rpc_io.Read(&m_chn, 1);
rpc_io.Read(&m_size, 2);
}
void rpc_SendRaw(CRpcIo &rpc_io, uint8_t channel, const void *x, uint16_t size)
{
uint8_t value = RPC_TYPE_DTB_DATA;
rpc_io.Write(&value, 1);
rpc_io.Write(&channel, 1);
rpc_io.Write(&size, 2);
if (size) rpc_io.Write(x, size);
// printf("Send Data [%i]\n", int(size));
}
void rpc_DataSink(CRpcIo &rpc_io, uint16_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);
char ch;
for (uint16_t i=0; i<msg.m_size; i++)
{
rpc_io.Read(&ch, 1);
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] <= '4')
{
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 + '&'; 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;
default: out += type;
}
if (i == 0)
{
out += ' ';
out += in.substr(0, size);
out += '(';
}
pos++; i++;
}
out += ");";
}
catch (int) { out = in; }
}