-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleConfigure_Json.cpp
More file actions
292 lines (273 loc) · 10.7 KB
/
ModuleConfigure_Json.cpp
File metadata and controls
292 lines (273 loc) · 10.7 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "pch.h"
#include "ModuleConfigure_Json.h"
/********************************************************************
// Created: 2021/12/02 16:14:11
// File Name: D:\XEngine_ServiceApp\XEngine_Source\XEngine_ModuleConfigure\ModuleConfigure_Json\ModuleConfigure_Json.cpp
// File Path: D:\XEngine_ServiceApp\XEngine_Source\XEngine_ModuleConfigure\ModuleConfigure_Json
// File Base: ModuleConfigure_Json
// File Ext: cpp
// Project: XEngine(网络通信引擎)
// Author: qyt
// Purpose: JSON配置读写实现
// History:
*********************************************************************/
CModuleConfigure_Json::CModuleConfigure_Json()
{
}
CModuleConfigure_Json::~CModuleConfigure_Json()
{
}
//////////////////////////////////////////////////////////////////////////
// 公用函数
//////////////////////////////////////////////////////////////////////////
/********************************************************************
函数名称:ModuleConfigure_Json_File
函数功能:读取JSON配置文件
参数.一:lpszConfigFile
In/Out:In
类型:常量字符指针
可空:N
意思:输入要读取的配置文件
参数.二:pSt_ServerConfig
In/Out:Out
类型:数据结构指针
可空:N
意思:输出服务配置信息
返回值
类型:逻辑型
意思:是否成功
备注:
*********************************************************************/
bool CModuleConfigure_Json::ModuleConfigure_Json_File(LPCXSTR lpszConfigFile, XENGINE_SERVICECONFIG* pSt_ServerConfig)
{
// 约定:每次进入函数先清除错误状态,后续任一步失败都设置错误码并返回 false。
Config_IsErrorOccur = false;
// 第一步:校验输入参数,配置文件路径和输出结构体都不能为空。
if ((NULL == lpszConfigFile) || (NULL == pSt_ServerConfig))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARAMENT;
return false;
}
// 第二步:准备 JSON 解析器对象与错误信息缓冲。
Json::Value st_JsonRoot;
JSONCPP_STRING st_JsonError;
Json::CharReaderBuilder st_JsonBuilder;
// 第三步:读取配置文件内容到固定缓冲区(当前实现最多读取 8192 字节)。
FILE* pSt_File = _xtfopen(lpszConfigFile, _X("rb"));
if (NULL == pSt_File)
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_OPENFILE;
return false;
}
XCHAR tszMsgBuffer[8192];
int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_File);
fclose(pSt_File);
// 第四步:解析 JSON 文本。解析失败时返回统一的解析错误码。
std::unique_ptr<Json::CharReader> const pSt_JsonReader(st_JsonBuilder.newCharReader());
if (!pSt_JsonReader->parse(tszMsgBuffer, tszMsgBuffer + nRet, &st_JsonRoot, &st_JsonError))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARSE;
return false;
}
// 第五步:将 JSON 字段映射到服务配置结构体,供调用方后续使用。
_tcsxcpy(pSt_ServerConfig->tszIPAddr, st_JsonRoot["tszIPAddr"].asCString());
pSt_ServerConfig->bDeamon = st_JsonRoot["bDeamon"].asBool();
pSt_ServerConfig->nHttpPort = st_JsonRoot["nHttpPort"].asInt();
pSt_ServerConfig->nSocksPort = st_JsonRoot["nSocksPort"].asInt();
pSt_ServerConfig->nTunnelPort = st_JsonRoot["nTunnelPort"].asInt();
pSt_ServerConfig->nForwardPort = st_JsonRoot["nForwardPort"].asInt();
pSt_ServerConfig->nProxyPort = st_JsonRoot["nProxyPort"].asInt();
if (st_JsonRoot["XMax"].empty() || (5 != st_JsonRoot["XMax"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XMAX;
return false;
}
Json::Value st_JsonXMax = st_JsonRoot["XMax"];
pSt_ServerConfig->st_XMax.nMaxClient = st_JsonXMax["nMaxClient"].asInt();
pSt_ServerConfig->st_XMax.nMaxQueue = st_JsonXMax["nMaxQueue"].asInt();
pSt_ServerConfig->st_XMax.nIOThread = st_JsonXMax["nIOThread"].asInt();
pSt_ServerConfig->st_XMax.nHTTPThread = st_JsonXMax["nHTTPThread"].asInt();
pSt_ServerConfig->st_XMax.nForwardThread = st_JsonXMax["nForwardThread"].asInt();
if (st_JsonRoot["XTime"].empty() || (6 != st_JsonRoot["XTime"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XTIME;
return false;
}
Json::Value st_JsonXTime = st_JsonRoot["XTime"];
pSt_ServerConfig->st_XTime.nTimeCheck = st_JsonXTime["nTimeCheck"].asInt();
pSt_ServerConfig->st_XTime.nHttpTimeout = st_JsonXTime["nHttpTimeout"].asInt();
pSt_ServerConfig->st_XTime.nSocksTimeout = st_JsonXTime["nSocksTimeout"].asInt();
pSt_ServerConfig->st_XTime.nTunnelTimeout = st_JsonXTime["nTunnelTimeout"].asInt();
pSt_ServerConfig->st_XTime.nForwardTimeout = st_JsonXTime["nForwardTimeout"].asInt();
pSt_ServerConfig->st_XTime.nProxyTimeout = st_JsonXTime["nProxyTimeout"].asInt();
if (st_JsonRoot["XLog"].empty() || (5 != st_JsonRoot["XLog"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XLOG;
return false;
}
Json::Value st_JsonXLog = st_JsonRoot["XLog"];
pSt_ServerConfig->st_XLog.nMaxSize = st_JsonXLog["MaxSize"].asInt();
pSt_ServerConfig->st_XLog.nMaxCount = st_JsonXLog["MaxCount"].asInt();
pSt_ServerConfig->st_XLog.nLogLeave = st_JsonXLog["LogLeave"].asInt();
pSt_ServerConfig->st_XLog.nLogType = st_JsonXLog["LogType"].asInt();
_tcsxcpy(pSt_ServerConfig->st_XLog.tszLogFile, st_JsonXLog["tszLogFile"].asCString());
if (st_JsonRoot["XVerification"].empty() || (4 != st_JsonRoot["XVerification"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XVERICATION;
return false;
}
Json::Value st_JsonXVerifcation = st_JsonRoot["XVerification"];
pSt_ServerConfig->st_XVerifcation.bEnable = st_JsonXVerifcation["bEnable"].asBool();
pSt_ServerConfig->st_XVerifcation.nVType = st_JsonXVerifcation["nVType"].asInt();
_tcsxcpy(pSt_ServerConfig->st_XVerifcation.tszUserName, st_JsonXVerifcation["tszUserName"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XVerifcation.tszUserPass, st_JsonXVerifcation["tszUserPass"].asCString());
if (st_JsonRoot["XReport"].empty() || (3 != st_JsonRoot["XReport"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XREPORT;
return false;
}
Json::Value st_JsonXReport = st_JsonRoot["XReport"];
pSt_ServerConfig->st_XReport.bEnable = st_JsonXReport["bEnable"].asBool();
_tcsxcpy(pSt_ServerConfig->st_XReport.tszAPIUrl, st_JsonXReport["tszAPIUrl"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XReport.tszServiceName, st_JsonXReport["tszServiceName"].asCString());
return true;
}
/********************************************************************
函数名称:ModuleConfigure_Json_Version
函数功能:读取版本配置文件
参数.一:lpszConfigFile
In/Out:In
类型:常量字符指针
可空:N
意思:输入要读取的配置文件
参数.二:pSt_ServerConfig
In/Out:Out
类型:数据结构指针
可空:N
意思:输出配置信息
返回值
类型:逻辑型
意思:是否成功
备注:
*********************************************************************/
bool CModuleConfigure_Json::ModuleConfigure_Json_Version(LPCXSTR lpszConfigFile, XENGINE_SERVICECONFIG* pSt_ServerConfig)
{
Config_IsErrorOccur = false;
if ((NULL == lpszConfigFile) || (NULL == pSt_ServerConfig))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARAMENT;
return false;
}
Json::Value st_JsonRoot;
JSONCPP_STRING st_JsonError;
Json::CharReaderBuilder st_JsonBuilder;
//读取配置文件所有内容到缓冲区
FILE* pSt_File = _xtfopen(lpszConfigFile, _X("rb"));
if (NULL == pSt_File)
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_OPENFILE;
return false;
}
XCHAR tszMsgBuffer[8192];
int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_File);
fclose(pSt_File);
//开始解析配置文件
std::unique_ptr<Json::CharReader> const pSt_JsonReader(st_JsonBuilder.newCharReader());
if (!pSt_JsonReader->parse(tszMsgBuffer, tszMsgBuffer + nRet, &st_JsonRoot, &st_JsonError))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARSE;
return false;
}
if (st_JsonRoot["XVer"].empty())
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XVER;
return false;
}
pSt_ServerConfig->st_XVer.pStl_ListVer = new list<string>;
Json::Value st_JsonXVer = st_JsonRoot["XVer"];
for (unsigned int i = 0; i < st_JsonXVer.size(); i++)
{
pSt_ServerConfig->st_XVer.pStl_ListVer->push_back(st_JsonXVer[i].asCString());
}
return true;
}
/********************************************************************
函数名称:ModuleConfigure_Json_ProxyFile
函数功能:读取JSON配置文件
参数.一:lpszConfigFile
In/Out:In
类型:常量字符指针
可空:N
意思:输入要读取的配置文件
参数.二:pSt_ServerConfig
In/Out:Out
类型:数据结构指针
可空:N
意思:输出服务配置信息
返回值
类型:逻辑型
意思:是否成功
备注:
*********************************************************************/
bool CModuleConfigure_Json::ModuleConfigure_Json_ProxyFile(LPCXSTR lpszConfigFile, XENGINE_PROXYCONFIG* pSt_ServerConfig)
{
Config_IsErrorOccur = false;
if ((NULL == lpszConfigFile) || (NULL == pSt_ServerConfig))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARAMENT;
return false;
}
Json::Value st_JsonRoot;
JSONCPP_STRING st_JsonError;
Json::CharReaderBuilder st_JsonBuilder;
//读取配置文件所有内容到缓冲区
FILE* pSt_File = _xtfopen(lpszConfigFile, _X("rb"));
if (NULL == pSt_File)
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_OPENFILE;
return false;
}
XCHAR tszMsgBuffer[8192];
int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_File);
fclose(pSt_File);
//开始解析配置文件
std::unique_ptr<Json::CharReader> const pSt_JsonReader(st_JsonBuilder.newCharReader());
if (!pSt_JsonReader->parse(tszMsgBuffer, tszMsgBuffer + nRet, &st_JsonRoot, &st_JsonError))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_PARSE;
return false;
}
pSt_ServerConfig->pStl_ListRuleAddr = new list<xstring>;
pSt_ServerConfig->pStl_ListDestAddr = new list<xstring>;
pSt_ServerConfig->nRuleMode = st_JsonRoot["nRuleMode"].asInt();
if (!st_JsonRoot["tszDestIPAddr"].isNull())
{
for (unsigned int i = 0; i < st_JsonRoot["tszDestIPAddr"].size(); i++)
{
pSt_ServerConfig->pStl_ListDestAddr->push_back(st_JsonRoot["tszDestIPAddr"][i].asCString());
}
}
if (!st_JsonRoot["tszRuleIPAddr"].isNull())
{
for (unsigned int i = 0; i < st_JsonRoot["tszRuleIPAddr"].size(); i++)
{
pSt_ServerConfig->pStl_ListRuleAddr->push_back(st_JsonRoot["tszRuleIPAddr"][i].asCString());
}
}
return true;
}