68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
#include "Config.h"
|
|
#include "BS_Log.h"
|
|
#include "CCFIleInStream.h"
|
|
#include "CCFileOutStream.h"
|
|
|
|
void Config::Save() {
|
|
const auto Setting = getConfig();
|
|
try {
|
|
const CTL::JSONObject Json = *Setting;
|
|
const auto Path = GetSettingDB();
|
|
CTL::FileOutStream::WriteFile(Path,Json.to_String());
|
|
}
|
|
catch (CCException& e) {
|
|
BS_Log::Error("Config::Save Error: {}",e.what());
|
|
}
|
|
}
|
|
|
|
void Config::Init() {
|
|
const auto Setting = getConfig();
|
|
try {
|
|
const auto Path = GetSettingDB();
|
|
const CCFile file(Path);
|
|
if (file.isExists()) {
|
|
const auto str = CTL::FileInputStream::ReadFileDataAll(Path);
|
|
CTL::JSONObject Json = CTL::JSONObject::parse(str);
|
|
Setting->IP = Json["IP"];
|
|
Setting->OrderPort = Json["OrderPort"];
|
|
Setting->StreamPort = Json["StreamPort"];
|
|
Setting->HttpPort = Json["HttpPort"];
|
|
Setting->Flag = Json["Flag"];
|
|
Setting->ServerID = Json["ServerID"];
|
|
Setting->ServerIP = Json["ServerIP"];
|
|
Setting->ServerPort = Json["ServerPort"];
|
|
Setting->ID = Json["ID"];
|
|
Setting->Name = Json["Name"];
|
|
}
|
|
|
|
}
|
|
catch (CCException& e) {
|
|
BS_Log::Error("Config::Init Error: {}",e.what());
|
|
}
|
|
Setting->IP = GetIP();
|
|
}
|
|
|
|
Config * Config::getConfig() {
|
|
static Config config;
|
|
return &config;
|
|
}
|
|
|
|
bool Config::IsRunning() {
|
|
return getConfig()->Flag;
|
|
}
|
|
|
|
CTL::String Config::GetIP() {
|
|
const auto Setting = Config::getConfig();
|
|
const auto List = CTL::Socket::GetLocalIP(Setting->IP_x);
|
|
CCVector<CTL::String> IPList;
|
|
for (const auto IP : List) {
|
|
if (IP != "127.0.0.1") {
|
|
IPList.push_back(IP);
|
|
}
|
|
if (IP == Setting->IP) {
|
|
return IP;
|
|
}
|
|
}
|
|
return IPList.empty() ? "127.0.0.1" : IPList[0];
|
|
}
|