From 2242577661ee7650fe0bbf0d88d4956d5235c203 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Fri, 27 Aug 2021 11:05:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=A3=E6=9E=90=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=E6=97=B6=E7=9A=84=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/config.h | 56 ++++++++++++++++++++++++------------ src/Http/HttpFileManager.cpp | 30 ++++++++++--------- src/Rtsp/RtpMultiCaster.cpp | 10 ++++--- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/Common/config.h b/src/Common/config.h index 917c76c6..b8def890 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -106,28 +106,46 @@ extern const string kBroadcastReloadConfig; #define BroadcastReloadConfigArgs void #define ReloadConfigTag ((void *)(0xFF)) -#define RELOAD_KEY(arg,key) \ - do{ \ - decltype(arg) arg##tmp = mINI::Instance()[key]; \ - if(arg != arg##tmp ) { \ - arg = arg##tmp; \ - InfoL << "reload config:" << key << "=" << arg; \ - } \ - }while(0); +#define RELOAD_KEY(arg,key) \ + do { \ + decltype(arg) arg##_tmp = mINI::Instance()[key]; \ + if (arg == arg##_tmp) { \ + return; \ + } \ + arg = arg##_tmp; \ + InfoL << "reload config:" << key << "=" << arg; \ + } while(0) //监听某个配置发送变更 -#define LISTEN_RELOAD_KEY(arg,key) \ - do{ \ - static onceToken s_token([](){ \ - NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[](BroadcastReloadConfigArgs){ \ - RELOAD_KEY(arg,key); \ - }); \ - }); \ - }while(0); +#define LISTEN_RELOAD_KEY(arg, key, ...) \ + do { \ + static onceToken s_token_listen([](){ \ + NoticeCenter::Instance().addListener(ReloadConfigTag, \ + Broadcast::kBroadcastReloadConfig,[](BroadcastReloadConfigArgs) { \ + __VA_ARGS__; \ + }); \ + }); \ + } while(0) -#define GET_CONFIG(type,arg,key) \ - static type arg = mINI::Instance()[key]; \ - LISTEN_RELOAD_KEY(arg,key); +#define GET_CONFIG(type, 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 diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 75251f27..bb13376e 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -135,9 +135,10 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st } //如果是root目录,添加虚拟目录 if (httpPath == "/") { - GET_CONFIG(string, virtualPath, Http::kVirtualPath); - StrCaseMap args = Parser::parseArgs(virtualPath, ";", ","); - for (auto &pr : args) { + GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) { + return Parser::parseArgs(str, ";", ","); + }); + for (auto &pr : virtualPathMap) { 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 static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){ GET_CONFIG(bool, enableVhost, General::kEnableVhost); - GET_CONFIG(string, virtualPath, Http::kVirtualPath); - StrCaseMap args = Parser::parseArgs(virtualPath, ";", ","); - auto path = args[mediaInfo._app]; - string url; - if (path.empty()) { - //访问的是根路径 - GET_CONFIG(string, rootPath, Http::kRootPath); + GET_CONFIG(string, rootPath, Http::kRootPath); + GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) { + return Parser::parseArgs(str, ";", ","); + }); + + string url, path; + 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; url = parser.Url(); - } else { - //访问的是虚拟路径 - url = parser.Url().substr(1 + mediaInfo._app.size()); } auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + url : url, path); NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast(sender)); diff --git a/src/Rtsp/RtpMultiCaster.cpp b/src/Rtsp/RtpMultiCaster.cpp index 36cef4fd..3ff6d35e 100644 --- a/src/Rtsp/RtpMultiCaster.cpp +++ b/src/Rtsp/RtpMultiCaster.cpp @@ -45,10 +45,12 @@ static uint32_t addressToInt(const string &ip){ std::shared_ptr MultiCastAddressMaker::obtain(uint32_t max_try) { lock_guard lck(_mtx); - GET_CONFIG(string, addrMinStr, MultiCast::kAddrMin); - GET_CONFIG(string, addrMaxStr, MultiCast::kAddrMax); - uint32_t addrMin = addressToInt(addrMinStr); - uint32_t addrMax = addressToInt(addrMaxStr); + GET_CONFIG_FUNC(uint32_t, addrMin, MultiCast::kAddrMin, [](const string &str) { + return addressToInt(str); + }); + GET_CONFIG_FUNC(uint32_t, addrMax, MultiCast::kAddrMax, [](const string &str) { + return addressToInt(str); + }); if (_addr > addrMax || _addr == 0) { _addr = addrMin;