Commit 22425776 by ziyue

优化解析复杂数据结构配置项时的性能

parent e044d5ce
...@@ -106,28 +106,46 @@ extern const string kBroadcastReloadConfig; ...@@ -106,28 +106,46 @@ extern const string kBroadcastReloadConfig;
#define BroadcastReloadConfigArgs void #define BroadcastReloadConfigArgs void
#define ReloadConfigTag ((void *)(0xFF)) #define ReloadConfigTag ((void *)(0xFF))
#define RELOAD_KEY(arg,key) \ #define RELOAD_KEY(arg,key) \
do{ \ do { \
decltype(arg) arg##tmp = mINI::Instance()[key]; \ decltype(arg) arg##_tmp = mINI::Instance()[key]; \
if(arg != arg##tmp ) { \ if (arg == arg##_tmp) { \
arg = arg##tmp; \ return; \
InfoL << "reload config:" << key << "=" << arg; \ } \
} \ arg = arg##_tmp; \
}while(0); InfoL << "reload config:" << key << "=" << arg; \
} while(0)
//监听某个配置发送变更 //监听某个配置发送变更
#define LISTEN_RELOAD_KEY(arg,key) \ #define LISTEN_RELOAD_KEY(arg, key, ...) \
do{ \ do { \
static onceToken s_token([](){ \ static onceToken s_token_listen([](){ \
NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[](BroadcastReloadConfigArgs){ \ NoticeCenter::Instance().addListener(ReloadConfigTag, \
RELOAD_KEY(arg,key); \ Broadcast::kBroadcastReloadConfig,[](BroadcastReloadConfigArgs) { \
}); \ __VA_ARGS__; \
}); \ }); \
}while(0); }); \
} while(0)
#define GET_CONFIG(type,arg,key) \
static type arg = mINI::Instance()[key]; \ #define GET_CONFIG(type, arg, key) \
LISTEN_RELOAD_KEY(arg,key); static type arg = mINI::Instance()[key]; \
LISTEN_RELOAD_KEY(arg, key, { \
RELOAD_KEY(arg, key); \
});
#define GET_CONFIG_FUNC(type, arg, key, ...) \
static type arg; \
do { \
static onceToken s_token_set([](){ \
static auto lam = __VA_ARGS__ ; \
static auto arg##_str = mINI::Instance()[key]; \
arg = lam(arg##_str); \
LISTEN_RELOAD_KEY(arg, key, { \
RELOAD_KEY(arg##_str, key); \
arg = lam(arg##_str); \
}); \
}); \
} while(0)
} //namespace Broadcast } //namespace Broadcast
......
...@@ -135,9 +135,10 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st ...@@ -135,9 +135,10 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
} }
//如果是root目录,添加虚拟目录 //如果是root目录,添加虚拟目录
if (httpPath == "/") { if (httpPath == "/") {
GET_CONFIG(string, virtualPath, Http::kVirtualPath); GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) {
StrCaseMap args = Parser::parseArgs(virtualPath, ";", ","); return Parser::parseArgs(str, ";", ",");
for (auto &pr : args) { });
for (auto &pr : virtualPathMap) {
file_map.emplace(pr.first, std::make_pair(string("虚拟目录:") + pr.first, File::absolutePath("", pr.second))); file_map.emplace(pr.first, std::make_pair(string("虚拟目录:") + pr.first, File::absolutePath("", pr.second)));
} }
} }
...@@ -472,18 +473,21 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo ...@@ -472,18 +473,21 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){ static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){
GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(bool, enableVhost, General::kEnableVhost);
GET_CONFIG(string, virtualPath, Http::kVirtualPath); GET_CONFIG(string, rootPath, Http::kRootPath);
StrCaseMap args = Parser::parseArgs(virtualPath, ";", ","); GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) {
auto path = args[mediaInfo._app]; return Parser::parseArgs(str, ";", ",");
string url; });
if (path.empty()) {
//访问的是根路径 string url, path;
GET_CONFIG(string, rootPath, Http::kRootPath); auto it = virtualPathMap.find(mediaInfo._app);
if (it != virtualPathMap.end()) {
//访问的是virtualPath
path = it->second;
url = parser.Url().substr(1 + mediaInfo._app.size());
} else {
//访问的是rootPath
path = rootPath; path = rootPath;
url = parser.Url(); url = parser.Url();
} else {
//访问的是虚拟路径
url = parser.Url().substr(1 + mediaInfo._app.size());
} }
auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + url : url, path); auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + url : url, path);
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast<SockInfo &>(sender)); NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast<SockInfo &>(sender));
......
...@@ -45,10 +45,12 @@ static uint32_t addressToInt(const string &ip){ ...@@ -45,10 +45,12 @@ static uint32_t addressToInt(const string &ip){
std::shared_ptr<uint32_t> MultiCastAddressMaker::obtain(uint32_t max_try) { std::shared_ptr<uint32_t> MultiCastAddressMaker::obtain(uint32_t max_try) {
lock_guard<recursive_mutex> lck(_mtx); lock_guard<recursive_mutex> lck(_mtx);
GET_CONFIG(string, addrMinStr, MultiCast::kAddrMin); GET_CONFIG_FUNC(uint32_t, addrMin, MultiCast::kAddrMin, [](const string &str) {
GET_CONFIG(string, addrMaxStr, MultiCast::kAddrMax); return addressToInt(str);
uint32_t addrMin = addressToInt(addrMinStr); });
uint32_t addrMax = addressToInt(addrMaxStr); GET_CONFIG_FUNC(uint32_t, addrMax, MultiCast::kAddrMax, [](const string &str) {
return addressToInt(str);
});
if (_addr > addrMax || _addr == 0) { if (_addr > addrMax || _addr == 0) {
_addr = addrMin; _addr = addrMin;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论