From f9f487bf618af68eecc1dcc35902b78c4eedb3bc Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 20 May 2019 17:34:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E5=A4=9A=E7=9A=84?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 84 ++++++++++++++++++++++------------------------ server/WebHook.cpp | 84 +++++++++++++++++----------------------------- 2 files changed, 70 insertions(+), 98 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 610f6690..cce6c974 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -128,53 +128,49 @@ static inline void addHttpListener(){ } //该api已被消费 consumed = true; - AsyncHttpApi api = it->second; - //异步执行该api,防止阻塞NoticeCenter - EventPollerPool::Instance().getExecutor()->async([api, parser, invoker]() { - //执行API - Json::Value val; - val["code"] = API::Success; - HttpSession::KeyValue &headerIn = parser.getValues(); - HttpSession::KeyValue headerOut; - auto allArgs = getAllArgs(parser); - headerOut["Content-Type"] = "application/json; charset=utf-8"; - if(api_debug){ - auto newInvoker = [invoker,parser,allArgs](const string &codeOut, - const HttpSession::KeyValue &headerOut, - const string &contentOut){ - stringstream ss; - for(auto &pr : allArgs ){ - ss << pr.first << " : " << pr.second << "\r\n"; - } + //执行API + Json::Value val; + val["code"] = API::Success; + HttpSession::KeyValue headerOut; + auto allArgs = getAllArgs(parser); + HttpSession::KeyValue &headerIn = parser.getValues(); + headerOut["Content-Type"] = "application/json; charset=utf-8"; + if(api_debug){ + auto newInvoker = [invoker,parser,allArgs](const string &codeOut, + const HttpSession::KeyValue &headerOut, + const string &contentOut){ + stringstream ss; + for(auto &pr : allArgs ){ + ss << pr.first << " : " << pr.second << "\r\n"; + } - DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n" - << "# content:\r\n" << parser.Content() << "\r\n" - << "# args:\r\n" << ss.str() - << "# response:\r\n" - << contentOut << "\r\n"; + DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n" + << "# content:\r\n" << parser.Content() << "\r\n" + << "# args:\r\n" << ss.str() + << "# response:\r\n" + << contentOut << "\r\n"; - invoker(codeOut,headerOut,contentOut); - }; - ((HttpSession::HttpResponseInvoker &)invoker) = newInvoker; - } + invoker(codeOut,headerOut,contentOut); + }; + ((HttpSession::HttpResponseInvoker &)invoker) = newInvoker; + } - try { - api(headerIn, headerOut, allArgs, val, invoker); - } catch(ApiRetException &ex){ - val["code"] = ex.code(); - val["msg"] = ex.what(); - invoker("200 OK", headerOut, val.toStyledString()); - } catch(SqlException &ex){ - val["code"] = API::SqlFailed; - val["msg"] = StrPrinter << "操作数据库失败:" << ex.what() << ":" << ex.getSql(); - WarnL << ex.what() << ":" << ex.getSql(); - invoker("200 OK", headerOut, val.toStyledString()); - } catch (std::exception &ex) { - val["code"] = API::OtherFailed; - val["msg"] = ex.what(); - invoker("200 OK", headerOut, val.toStyledString()); - } - }); + try { + it->second(headerIn, headerOut, allArgs, val, invoker); + } catch(ApiRetException &ex){ + val["code"] = ex.code(); + val["msg"] = ex.what(); + invoker("200 OK", headerOut, val.toStyledString()); + } catch(SqlException &ex){ + val["code"] = API::SqlFailed; + val["msg"] = StrPrinter << "操作数据库失败:" << ex.what() << ":" << ex.getSql(); + WarnL << ex.what() << ":" << ex.getSql(); + invoker("200 OK", headerOut, val.toStyledString()); + } catch (std::exception &ex) { + val["code"] = API::OtherFailed; + val["msg"] = ex.what(); + invoker("200 OK", headerOut, val.toStyledString()); + } }); } diff --git a/server/WebHook.cpp b/server/WebHook.cpp index e3e7b5cd..2c91393f 100644 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -168,11 +168,9 @@ void installWebHook(){ body["ip"] = sender.get_peer_ip(); body["port"] = sender.get_peer_port(); body["id"] = sender.getIdentifier(); - EventPollerPool::Instance().getExecutor()->async([body,invoker](){ - //执行hook - do_http_hook(hook_publish,body,[invoker](const Value &obj,const string &err){ - invoker(err); - }); + //执行hook + do_http_hook(hook_publish,body,[invoker](const Value &obj,const string &err){ + invoker(err); }); }); @@ -185,12 +183,9 @@ void installWebHook(){ body["ip"] = sender.get_peer_ip(); body["port"] = sender.get_peer_port(); body["id"] = sender.getIdentifier(); - //异步执行该hook api,防止阻塞NoticeCenter - EventPollerPool::Instance().getExecutor()->async([body,invoker](){ - //执行hook - do_http_hook(hook_play,body,[invoker](const Value &obj,const string &err){ - invoker(err); - }); + //执行hook + do_http_hook(hook_play,body,[invoker](const Value &obj,const string &err){ + invoker(err); }); }); @@ -204,12 +199,8 @@ void installWebHook(){ body["id"] = sender.getIdentifier(); body["totalBytes"] = (Json::UInt64)totalBytes; body["duration"] = (Json::UInt64)totalDuration; - - //流量统计事件 - EventPollerPool::Instance().getExecutor()->async([body,totalBytes](){ - //执行hook - do_http_hook(hook_flowreport,body, nullptr); - }); + //执行hook + do_http_hook(hook_flowreport,body, nullptr); }); @@ -226,17 +217,14 @@ void installWebHook(){ body["ip"] = sender.get_peer_ip(); body["port"] = sender.get_peer_port(); body["id"] = sender.getIdentifier(); - - EventPollerPool::Instance().getExecutor()->async([body,invoker](){ - //执行hook - do_http_hook(hook_rtsp_realm,body, [invoker](const Value &obj,const string &err){ - if(!err.empty()){ - //如果接口访问失败,那么该rtsp流认证失败 - invoker(unAuthedRealm); - return; - } - invoker(obj["realm"].asString()); - }); + //执行hook + do_http_hook(hook_rtsp_realm,body, [invoker](const Value &obj,const string &err){ + if(!err.empty()){ + //如果接口访问失败,那么该rtsp流认证失败 + invoker(unAuthedRealm); + return; + } + invoker(obj["realm"].asString()); }); }); @@ -254,17 +242,14 @@ void installWebHook(){ body["user_name"] = user_name; body["must_no_encrypt"] = must_no_encrypt; body["realm"] = realm; - - EventPollerPool::Instance().getExecutor()->async([body,invoker](){ - //执行hook - do_http_hook(hook_rtsp_auth,body, [invoker](const Value &obj,const string &err){ - if(!err.empty()){ - //认证失败 - invoker(false,makeRandStr(12)); - return; - } - invoker(obj["encrypted"].asBool(),obj["passwd"].asString()); - }); + //执行hook + do_http_hook(hook_rtsp_auth,body, [invoker](const Value &obj,const string &err){ + if(!err.empty()){ + //认证失败 + invoker(false,makeRandStr(12)); + return; + } + invoker(obj["encrypted"].asBool(),obj["passwd"].asString()); }); }); @@ -280,11 +265,8 @@ void installWebHook(){ body["vhost"] = vhost; body["app"] = app; body["stream"] = stream; - - EventPollerPool::Instance().getExecutor()->async([body](){ - //执行hook - do_http_hook(hook_stream_chaned,body, nullptr); - }); + //执行hook + do_http_hook(hook_stream_chaned,body, nullptr); }); //监听播放失败(未找到特定的流)事件 @@ -296,11 +278,8 @@ void installWebHook(){ body["ip"] = sender.get_peer_ip(); body["port"] = sender.get_peer_port(); body["id"] = sender.getIdentifier(); - - EventPollerPool::Instance().getExecutor()->async([body](){ - //执行hook - do_http_hook(hook_stream_not_found,body, nullptr); - }); + //执行hook + do_http_hook(hook_stream_not_found,body, nullptr); }); #ifdef ENABLE_MP4V2 @@ -320,11 +299,8 @@ void installWebHook(){ body["app"] = info.strAppName; body["stream"] = info.strStreamId; body["vhost"] = info.strVhost; - - EventPollerPool::Instance().getExecutor()->async([body](){ - //执行hook - do_http_hook(hook_record_mp4,body, nullptr); - }); + //执行hook + do_http_hook(hook_record_mp4,body, nullptr); }); #endif //ENABLE_MP4V2