-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleConfigure_Json.cpp
More file actions
186 lines (179 loc) · 6.36 KB
/
ModuleConfigure_Json.cpp
File metadata and controls
186 lines (179 loc) · 6.36 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
#include "pch.h"
#include "ModuleConfigure_Json.h"
#include <new>
/********************************************************************
// 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->tszIPAddr, st_JsonRoot["tszIPAddr"].asCString());
pSt_ServerConfig->bDaemon = st_JsonRoot["bDaemon"].asInt();
pSt_ServerConfig->nPort = st_JsonRoot["nPort"].asInt();
//最大配置
if (st_JsonRoot["XMax"].empty() || (4 != 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.nThread = st_JsonXMax["nThread"].asInt();
//时间配置
if (st_JsonRoot["XTime"].empty() || (2 != 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.nTimeOut = st_JsonXTime["nTimeOut"].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["LogFile"].asCString());
return true;
}
/********************************************************************
函数名称:Config_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;
}
JSONCPP_STRING st_JsonError;
Json::Value st_JsonRoot;
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_PARAMENT;
return false;
}
if (st_JsonRoot["XVersionList"].empty())
{
Config_IsErrorOccur = true;
Config_dwErrorCode = ERROR_MODULE_CONFIGURE_JSON_XVER;
return false;
}
Json::Value st_JsonXVer = st_JsonRoot["XVersionList"];
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;
}