-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModuleConfigure_Json.cpp
More file actions
276 lines (264 loc) · 11.8 KB
/
ModuleConfigure_Json.cpp
File metadata and controls
276 lines (264 loc) · 11.8 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
#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)
{
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] = {};
size_t 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;
}
_tcsxcpy(pSt_ServerConfig->tszSMSUrl, st_JsonRoot["tszSMSUrl"].asCString());
_tcsxcpy(pSt_ServerConfig->tszIPAddr, st_JsonRoot["tszIPAddr"].asCString());
pSt_ServerConfig->bDeamon = st_JsonRoot["bDeamon"].asInt();
pSt_ServerConfig->nRTMPPort = st_JsonRoot["nRTMPPort"].asInt();
pSt_ServerConfig->nHttpPort = st_JsonRoot["nHttpPort"].asInt();
pSt_ServerConfig->nXStreamPort = st_JsonRoot["nXStreamPort"].asInt();
pSt_ServerConfig->nJT1078Port = st_JsonRoot["nJT1078Port"].asInt();
pSt_ServerConfig->nSrtPort = st_JsonRoot["nSrtPort"].asInt();
pSt_ServerConfig->nRTCWhepPort = st_JsonRoot["nRTCWhepPort"].asInt();
pSt_ServerConfig->nRTCWhipPort = st_JsonRoot["nRTCWhipPort"].asInt();
//最大配置
if (st_JsonRoot["XMax"].empty() || (8 != 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.nXStreamThread = st_JsonXMax["nXStreamThread"].asInt();
pSt_ServerConfig->st_XMax.nRTMPThread = st_JsonXMax["nRTMPThread"].asInt();
pSt_ServerConfig->st_XMax.nJT1078Thread = st_JsonXMax["nJT1078Thread"].asInt();
pSt_ServerConfig->st_XMax.nSRTThread = st_JsonXMax["nSRTThread"].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.nXStreamTimeout = st_JsonXTime["nXStreamTimeout"].asInt();
pSt_ServerConfig->st_XTime.nRTMPTimeout = st_JsonXTime["nRTMPTimeout"].asInt();
pSt_ServerConfig->st_XTime.nJT1078Timeout = st_JsonXTime["nJT1078Timeout"].asInt();
pSt_ServerConfig->st_XTime.nRTCTimeout = st_JsonXTime["nRTCTimeout"].asInt();
//时间配置
if (st_JsonRoot["XPull"].empty() || (8 != st_JsonRoot["XPull"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XPULL;
return false;
}
Json::Value st_Pull = st_JsonRoot["XPull"];
Json::Value st_PullXStream = st_Pull["XStream"];
Json::Value st_PullRtmp = st_Pull["RTMP"];
Json::Value st_PullFlv = st_Pull["FLV"];
Json::Value st_PullRtsp = st_Pull["RTSP"];
Json::Value st_PullHls = st_Pull["HLS"];
Json::Value st_PullWebRtc = st_Pull["RTC"];
Json::Value st_PullSrt = st_Pull["SRT"];
Json::Value st_PullTs = st_Pull["TS"];
pSt_ServerConfig->st_XPull.st_PullXStream.bEnable = st_PullXStream["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullXStream.bPrePull = st_PullXStream["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullRtmp.bEnable = st_PullRtmp["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullRtmp.bPrePull = st_PullRtmp["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullFlv.bEnable = st_PullFlv["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullFlv.bPrePull = st_PullFlv["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullHls.bEnable = st_PullHls["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullHls.bPrePull = st_PullHls["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullSrt.bEnable = st_PullSrt["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullSrt.bPrePull = st_PullSrt["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullTs.bEnable = st_Pull["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullTs.bPrePull = st_Pull["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullRtsp.bEnable = st_PullRtsp["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullRtsp.bPrePull = st_PullRtsp["bPrePull"].asBool();
pSt_ServerConfig->st_XPull.st_PullRtsp.nVRTPPort = st_PullRtsp["nVRTPPort"].asInt();
pSt_ServerConfig->st_XPull.st_PullRtsp.nVRTCPPort = st_PullRtsp["nVRTCPPort"].asInt();
pSt_ServerConfig->st_XPull.st_PullRtsp.nARTPPort = st_PullRtsp["nARTPPort"].asInt();
pSt_ServerConfig->st_XPull.st_PullRtsp.nARTCPPort = st_PullRtsp["nARTCPPort"].asInt();
pSt_ServerConfig->st_XPull.st_PullHls.bClear = st_PullHls["bClear"].asBool();
pSt_ServerConfig->st_XPull.st_PullHls.nTime = st_PullHls["nTime"].asInt();
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullHls.tszHLSPath, st_PullHls["tszHLSPath"].asCString());
pSt_ServerConfig->st_XPull.st_PullWebRtc.bEnable = st_PullWebRtc["bEnable"].asBool();
pSt_ServerConfig->st_XPull.st_PullWebRtc.bPrePull = st_PullWebRtc["bPrePull"].asBool();
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszICEUser, st_PullWebRtc["tszICEUser"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszICEPass, st_PullWebRtc["tszICEPass"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszCertStr, st_PullWebRtc["tszCertStr"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszKeyStr, st_PullWebRtc["tszKeyStr"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XPull.st_PullWebRtc.tszDerStr, st_PullWebRtc["tszDerStr"].asCString());
//日志配置
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["LogFile"].asCString());
//接口验证
if (st_JsonRoot["XVerification"].empty() || (5 != st_JsonRoot["XVerification"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_VERIFICATION;
return false;
}
Json::Value st_JsonXVerification = st_JsonRoot["XVerification"];
pSt_ServerConfig->st_XVerification.bEnable = st_JsonXVerification["bEnable"].asBool();
pSt_ServerConfig->st_XVerification.nVType = st_JsonXVerification["nVerType"].asInt();
_tcsxcpy(pSt_ServerConfig->st_XVerification.tszUserName, st_JsonXVerification["tszUser"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XVerification.tszUserPass, st_JsonXVerification["tszPass"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XVerification.tszAPIUrl, st_JsonXVerification["tszAPIUrl"].asCString());
//信息报告
if (st_JsonRoot["XReport"].empty() || (3 != st_JsonRoot["XReport"].size()))
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_REPORT;
return false;
}
Json::Value st_JsonReport = st_JsonRoot["XReport"];
pSt_ServerConfig->st_XReport.bEnable = st_JsonReport["bEnable"].asBool();
_tcsxcpy(pSt_ServerConfig->st_XReport.tszServiceName, st_JsonReport["tszServiceName"].asCString());
_tcsxcpy(pSt_ServerConfig->st_XReport.tszAPIUrl, st_JsonReport["tszAPIUrl"].asCString());
return true;
}
/********************************************************************
函数名称:ModuleConfigure_Json_Versions
函数功能:读取版本列表配置
参数.一:lpszConfigFile
In/Out:In
类型:常量字符指针
可空:N
意思:输入要读取的配置文件
参数.二:pSt_ServerConfig
In/Out:Out
类型:数据结构指针
可空:N
意思:输出服务配置信息
返回值
类型:逻辑型
意思:是否成功
备注:
*********************************************************************/
bool CModuleConfigure_Json::ModuleConfigure_Json_Versions(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] = {};
size_t 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;
}
Json::Value st_JsonXVer = st_JsonRoot["XVer"];
pSt_ServerConfig->st_XVer.pStl_ListVer = new (std::nothrow) list<string>;
if (NULL == pSt_ServerConfig->st_XVer.pStl_ListVer)
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_MALLOC;
return false;
}
for (unsigned int i = 0; i < st_JsonXVer.size(); i++)
{
pSt_ServerConfig->st_XVer.pStl_ListVer->push_back(st_JsonXVer[i].asCString());
}
return true;
}