Repository navigation
Expand file tree
/
Copy pathRpcCmd.cpp
More file actions
138 lines (119 loc) · 2.57 KB
/
Copy pathRpcCmd.cpp
File metadata and controls
138 lines (119 loc) · 2.57 KB
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
#include "StdAfx.h"
#include "RpcCmd.h"
#include "SynchronousSocket.h"
#include "json\reader.h"
#include "json\value.h"
CRpcCmd::CRpcCmd(void)
{
}
CRpcCmd::~CRpcCmd(void)
{
}
int CRpcCmd::SendRpc(CString strCommand,string &rev)
{
CStringA strSendData;
BuildSendString(strCommand,strSendData);
string teprev;
if(CSynchronousSocket::GetRpcRes(mIp,mPort,strSendData.GetString(),teprev,7000)>0)
{
CStringA message = teprev.c_str();
int pos = message.Find("Server:");
if (pos >=0)
{
pos = message.Find('\n',pos);
if (pos >=0)
{
rev = message.Mid(pos);
}
}
}
return rev.length();
}
static bool IsAllDigtal(const char* pData)
{
for(int i = 0;pData[i] != '\0';i++)
{
if (!isdigit(pData[i]))
return false;
}
return true;
}
void CRpcCmd:: BuildSendString(const CString &cmd,CStringA &sendStr)
{
CStringA sendStrte;
RPCCommandToJson(cmd,sendStrte);
RpcJosnStr = sendStrte;
string tepsend = sendStrte.GetString();
string nSendStr = "";
nSendStr += m_sendPreHeadstr;
CStringA nMidStr = "Content-Length: ";
nMidStr.AppendFormat("%d\r\n",tepsend.length());
nSendStr += nMidStr.GetString();
nSendStr += m_sendendHeadstr.GetString();
nSendStr += sendStrte;
sendStr = nSendStr.c_str();
}
void CRpcCmd::RPCCommandToJson(const CString& strRPCCommand,CStringA& strSendData)
{
USES_CONVERSION;
char* pData = W2A(strRPCCommand);
CStringA rpcCommand(pData);
Json::Value root;
Json::Value arrayObj;
Json::Value item;
int pos = rpcCommand.Find(" ");
CStringA method;
CStringA param;
if (pos >=0)
{
method = rpcCommand.Left(pos);
rpcCommand = rpcCommand.Mid(pos + 1);
}
else if (!rpcCommand.IsEmpty())
{
root["method"] = rpcCommand.GetString();
root["params"]=arrayObj;
}
if (method != "")
{
root["method"] = method.GetString();
pos = rpcCommand.Find(" ");
while(pos >=0)
{
param = rpcCommand.Left(pos);
param.TrimLeft();
param.TrimRight();
rpcCommand = rpcCommand.Mid(pos+1);
rpcCommand = rpcCommand.TrimLeft();
if (param.GetLength()<10&&IsAllDigtal(param))
{
root["params"].append(atoi(param));
}
else
{
root["params"].append(param.GetString());
}
pos = rpcCommand.Find(" ");
}
if (IsAllDigtal(rpcCommand) && rpcCommand.GetLength() != 12)
{
root["params"].append(atoi(rpcCommand));
}
else
{
if (rpcCommand == "true")
{
root["params"].append(true);
}
else if (rpcCommand == "false")
{
root["params"].append(false);
}
else
{
root["params"].append(rpcCommand.GetString());
}
}
}
strSendData = root.toStyledString().c_str();
}