From b6946a596897b9a8315fe153bb08a9162df281e8 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 23 Apr 2020 21:38:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=B9=BF=E6=92=AD=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E8=80=85=E6=94=B9=E6=88=90SockInfo=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- api/include/mk_events.h | 18 ++++---- api/include/mk_tcp.h | 40 ++++++++++++---- api/source/mk_tcp.cpp | 70 +++++++++++++++++----------- api/source/mk_tcp_private.h | 6 +-- api/tests/server.c | 90 ++++++++++++++++++------------------ server/WebApi.cpp | 2 +- src/Common/MediaSource.cpp | 2 +- src/Common/config.h | 18 ++++---- src/Http/HttpFileManager.cpp | 6 +-- src/Http/HttpSession.cpp | 5 +- src/Rtmp/RtmpSession.cpp | 8 ++-- src/Rtsp/RtspSession.cpp | 13 ++---- src/Shell/ShellSession.cpp | 2 +- 14 files changed, 158 insertions(+), 124 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index fac78b63..07d21ac6 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit fac78b63701ce6febd9ac6acefb637886c651873 +Subproject commit 07d21ac61be6c7a4eba90a5d2d26b15daa882cf7 diff --git a/api/include/mk_events.h b/api/include/mk_events.h index 79f435a8..47bbb4a2 100644 --- a/api/include/mk_events.h +++ b/api/include/mk_events.h @@ -36,7 +36,7 @@ typedef struct { */ void (API_CALL *on_mk_media_publish)(const mk_media_info url_info, const mk_publish_auth_invoker invoker, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 播放rtsp/rtmp/http-flv/hls事件广播,通过该事件控制播放鉴权 @@ -47,7 +47,7 @@ typedef struct { */ void (API_CALL *on_mk_media_play)(const mk_media_info url_info, const mk_auth_invoker invoker, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了 @@ -55,7 +55,7 @@ typedef struct { * @param sender 播放客户端相关信息 */ void (API_CALL *on_mk_media_not_found)(const mk_media_info url_info, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 @@ -73,7 +73,7 @@ typedef struct { void (API_CALL *on_mk_http_request)(const mk_parser parser, const mk_http_response_invoker invoker, int *consumed, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 @@ -87,7 +87,7 @@ typedef struct { const char *path, int is_dir, const mk_http_access_path_invoker invoker, - mk_tcp_session sender); + const mk_sock_info sender); /** * 在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 @@ -98,7 +98,7 @@ typedef struct { */ void (API_CALL *on_mk_http_before_access)(const mk_parser parser, char *path, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 该rtsp流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm @@ -108,7 +108,7 @@ typedef struct { */ void (API_CALL *on_mk_rtsp_get_realm)(const mk_media_info url_info, const mk_rtsp_get_realm_invoker invoker, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 @@ -125,7 +125,7 @@ typedef struct { const char *user_name, int must_no_encrypt, const mk_rtsp_auth_invoker invoker, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 录制mp4分片文件成功后广播 @@ -138,7 +138,7 @@ typedef struct { void (API_CALL *on_mk_shell_login)(const char *user_name, const char *passwd, const mk_auth_invoker invoker, - const mk_tcp_session sender); + const mk_sock_info sender); /** * 停止rtsp/rtmp/http-flv会话后流量汇报事件广播 diff --git a/api/include/mk_tcp.h b/api/include/mk_tcp.h index 1a2dac80..c999b46c 100644 --- a/api/include/mk_tcp.h +++ b/api/include/mk_tcp.h @@ -17,19 +17,41 @@ extern "C" { #endif +///////////////////////////////////////////SockInfo///////////////////////////////////////////// +//SockInfo对象的C映射 +typedef void* mk_sock_info; + +//SockInfo::get_peer_ip() +API_EXPORT const char* API_CALL mk_sock_info_peer_ip(const mk_sock_info ctx); +//SockInfo::get_local_ip() +API_EXPORT const char* API_CALL mk_sock_info_local_ip(const mk_sock_info ctx); +//SockInfo::get_peer_port() +API_EXPORT uint16_t API_CALL mk_sock_info_peer_port(const mk_sock_info ctx); +//SockInfo::get_local_port() +API_EXPORT uint16_t API_CALL mk_sock_info_local_port(const mk_sock_info ctx); + +#ifndef SOCK_INFO_API_RENAME +#define SOCK_INFO_API_RENAME +//mk_tcp_session对象转换成mk_sock_info对象后再获取网络相关信息 +#define mk_tcp_session_peer_ip(x) mk_sock_info_peer_ip(mk_tcp_session_get_sock_info(x)) +#define mk_tcp_session_local_ip(x) mk_sock_info_local_ip(mk_tcp_session_get_sock_info(x)) +#define mk_tcp_session_peer_port(x) mk_sock_info_peer_port(mk_tcp_session_get_sock_info(x)) +#define mk_tcp_session_local_port(x) mk_sock_info_local_port(mk_tcp_session_get_sock_info(x)) + +//mk_tcp_client对象转换成mk_sock_info对象后再获取网络相关信息 +#define mk_tcp_client_peer_ip(x) mk_sock_info_peer_ip(mk_tcp_client_get_sock_info(x)) +#define mk_tcp_client_local_ip(x) mk_sock_info_local_ip(mk_tcp_client_get_sock_info(x)) +#define mk_tcp_client_peer_port(x) mk_sock_info_peer_port(mk_tcp_client_get_sock_info(x)) +#define mk_tcp_client_local_port(x) mk_sock_info_local_port(mk_tcp_client_get_sock_info(x)) +#endif ///////////////////////////////////////////TcpSession///////////////////////////////////////////// //TcpSession对象的C映射 typedef void* mk_tcp_session; +//获取基类指针以便获取其网络相关信息 +API_EXPORT mk_sock_info API_CALL mk_tcp_session_get_sock_info(const mk_tcp_session ctx); + //TcpSession::safeShutdown() API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg); -//TcpSession::get_peer_ip() -API_EXPORT const char* API_CALL mk_tcp_session_peer_ip(const mk_tcp_session ctx); -//TcpSession::get_local_ip() -API_EXPORT const char* API_CALL mk_tcp_session_local_ip(const mk_tcp_session ctx); -//TcpSession::get_peer_port() -API_EXPORT uint16_t API_CALL mk_tcp_session_peer_port(const mk_tcp_session ctx); -//TcpSession::get_local_port() -API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx); //TcpSession::send() API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx,const char *data,int len); //切换到该对象所在线程后再TcpSession::send() @@ -115,6 +137,8 @@ API_EXPORT void API_CALL mk_tcp_server_events_listen(const mk_tcp_session_events ///////////////////////////////////////////自定义tcp客户端///////////////////////////////////////////// typedef void* mk_tcp_client; +//获取基类指针以便获取其网络相关信息 +API_EXPORT mk_sock_info API_CALL mk_tcp_client_get_sock_info(const mk_tcp_client ctx); typedef struct { /** diff --git a/api/source/mk_tcp.cpp b/api/source/mk_tcp.cpp index a6347015..ce141d41 100644 --- a/api/source/mk_tcp.cpp +++ b/api/source/mk_tcp.cpp @@ -14,38 +14,46 @@ #include "Http/WebSocketSession.h" using namespace mediakit; +API_EXPORT const char* API_CALL mk_sock_info_peer_ip(const mk_sock_info ctx){ + assert(ctx); + SockInfo *sock = (SockInfo *)ctx; + return sock->get_peer_ip().c_str(); +} +API_EXPORT const char* API_CALL mk_sock_info_local_ip(const mk_sock_info ctx){ + assert(ctx); + SockInfo *sock = (SockInfo *)ctx; + return sock->get_local_ip().c_str(); +} +API_EXPORT uint16_t API_CALL mk_sock_info_peer_port(const mk_sock_info ctx){ + assert(ctx); + SockInfo *sock = (SockInfo *)ctx; + return sock->get_peer_port(); +} +API_EXPORT uint16_t API_CALL mk_sock_info_local_port(const mk_sock_info ctx){ + assert(ctx); + SockInfo *sock = (SockInfo *)ctx; + return sock->get_local_port(); +} + //////////////////////////////////////////////////////////////////////////////////////// +API_EXPORT mk_sock_info API_CALL mk_tcp_session_get_sock_info(const mk_tcp_session ctx){ + assert(ctx); + TcpSessionForC *session = (TcpSessionForC *)ctx; + return (SockInfo *)session; +} + API_EXPORT void API_CALL mk_tcp_session_shutdown(const mk_tcp_session ctx,int err,const char *err_msg){ assert(ctx); - TcpSession *session = (TcpSession *)ctx; + TcpSessionForC *session = (TcpSessionForC *)ctx; session->safeShutdown(SockException((ErrCode)err,err_msg)); } -API_EXPORT const char* API_CALL mk_tcp_session_peer_ip(const mk_tcp_session ctx){ - assert(ctx); - TcpSession *session = (TcpSession *)ctx; - return session->get_peer_ip().c_str(); -} -API_EXPORT const char* API_CALL mk_tcp_session_local_ip(const mk_tcp_session ctx){ - assert(ctx); - TcpSession *session = (TcpSession *)ctx; - return session->get_local_ip().c_str(); -} -API_EXPORT uint16_t API_CALL mk_tcp_session_peer_port(const mk_tcp_session ctx){ - assert(ctx); - TcpSession *session = (TcpSession *)ctx; - return session->get_peer_port(); -} -API_EXPORT uint16_t API_CALL mk_tcp_session_local_port(const mk_tcp_session ctx){ - assert(ctx); - TcpSession *session = (TcpSession *)ctx; - return session->get_local_port(); -} + API_EXPORT void API_CALL mk_tcp_session_send(const mk_tcp_session ctx,const char *data,int len){ assert(ctx && data); if(!len){ len = strlen(data); } - TcpSession *session = (TcpSession *)ctx; + TcpSessionForC *session = (TcpSessionForC *)ctx; session->SockSender::send(data,len); } @@ -55,9 +63,9 @@ API_EXPORT void API_CALL mk_tcp_session_send_safe(const mk_tcp_session ctx,const len = strlen(data); } try { - weak_ptr weak_session = ((TcpSession *)ctx)->shared_from_this(); + weak_ptr weak_session = ((TcpSessionForC *)ctx)->shared_from_this(); string str = string(data,len); - ((TcpSession *)ctx)->async([weak_session,str](){ + ((TcpSessionForC *)ctx)->async([weak_session,str](){ auto session_session = weak_session.lock(); if(session_session){ session_session->SockSender::send(str); @@ -205,6 +213,12 @@ TcpClientForC::Ptr *mk_tcp_client_create_l(mk_tcp_client_events *events, mk_tcp_ } } +API_EXPORT mk_sock_info API_CALL mk_tcp_client_get_sock_info(const mk_tcp_client ctx){ + assert(ctx); + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; + return (SockInfo *)client->get(); +} + API_EXPORT mk_tcp_client API_CALL mk_tcp_client_create(mk_tcp_client_events *events, mk_tcp_type type){ auto ret = mk_tcp_client_create_l(events,type); (*ret)->setClient(ret); @@ -213,25 +227,25 @@ API_EXPORT mk_tcp_client API_CALL mk_tcp_client_create(mk_tcp_client_events *eve API_EXPORT void API_CALL mk_tcp_client_release(mk_tcp_client ctx){ assert(ctx); - TcpClient::Ptr *client = (TcpClient::Ptr *)ctx; + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; delete client; } API_EXPORT void API_CALL mk_tcp_client_connect(mk_tcp_client ctx, const char *host, uint16_t port, float time_out_sec){ assert(ctx); - TcpClient::Ptr *client = (TcpClient::Ptr *)ctx; + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; (*client)->startConnect(host,port); } API_EXPORT void API_CALL mk_tcp_client_send(mk_tcp_client ctx, const char *data, int len){ assert(ctx && data); - TcpClient::Ptr *client = (TcpClient::Ptr *)ctx; + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; (*client)->SockSender::send(data,len); } API_EXPORT void API_CALL mk_tcp_client_send_safe(mk_tcp_client ctx, const char *data, int len){ assert(ctx && data); - TcpClient::Ptr *client = (TcpClient::Ptr *)ctx; + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; weak_ptr weakClient = *client; Buffer::Ptr buf = (*client)->obtainBuffer(data,len); (*client)->async([weakClient,buf](){ diff --git a/api/source/mk_tcp_private.h b/api/source/mk_tcp_private.h index eba7401e..fd48fc3b 100644 --- a/api/source/mk_tcp_private.h +++ b/api/source/mk_tcp_private.h @@ -17,7 +17,7 @@ using namespace toolkit; class TcpClientForC : public TcpClient { - public: +public: typedef std::shared_ptr Ptr; TcpClientForC(mk_tcp_client_events *events) ; ~TcpClientForC() override ; @@ -27,13 +27,13 @@ class TcpClientForC : public TcpClient { void onConnect(const SockException &ex) override; void setClient(mk_tcp_client client); void *_user_data; - private: +private: mk_tcp_client_events _events; mk_tcp_client _client; }; class TcpSessionForC : public TcpSession { - public: +public: TcpSessionForC(const Socket::Ptr &pSock) ; ~TcpSessionForC() override = default; void onRecv(const Buffer::Ptr &buffer) override ; diff --git a/api/tests/server.c b/api/tests/server.c index e346ca29..e6da1ef3 100644 --- a/api/tests/server.c +++ b/api/tests/server.c @@ -46,14 +46,14 @@ void API_CALL on_mk_media_changed(int regist, */ void API_CALL on_mk_media_publish(const mk_media_info url_info, const mk_publish_auth_invoker invoker, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s/%s/%s/%s, url params: %s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_media_info_get_schema(url_info), mk_media_info_get_vhost(url_info), mk_media_info_get_app(url_info), @@ -73,15 +73,15 @@ void API_CALL on_mk_media_publish(const mk_media_info url_info, */ void API_CALL on_mk_media_play(const mk_media_info url_info, const mk_auth_invoker invoker, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s/%s/%s/%s, url params: %s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_media_info_get_schema(url_info), mk_media_info_get_vhost(url_info), mk_media_info_get_app(url_info), @@ -98,14 +98,14 @@ void API_CALL on_mk_media_play(const mk_media_info url_info, * @param sender 播放客户端相关信息 */ void API_CALL on_mk_media_not_found(const mk_media_info url_info, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s/%s/%s/%s, url params: %s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_media_info_get_schema(url_info), mk_media_info_get_vhost(url_info), mk_media_info_get_app(url_info), @@ -137,17 +137,17 @@ void API_CALL on_mk_media_no_reader(const mk_media_source sender) { void API_CALL on_mk_http_request(const mk_parser parser, const mk_http_response_invoker invoker, int *consumed, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s %s?%s %s\n" "User-Agent: %s\n" "%s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_parser_get_method(parser), mk_parser_get_url(parser), mk_parser_get_url_params(parser), @@ -191,17 +191,17 @@ void API_CALL on_mk_http_access(const mk_parser parser, const char *path, int is_dir, const mk_http_access_path_invoker invoker, - mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d, path: %s ,is_dir: %d\n" "%s %s?%s %s\n" "User-Agent: %s\n" "%s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), path,(int)is_dir, mk_parser_get_method(parser), mk_parser_get_url(parser), @@ -223,16 +223,16 @@ void API_CALL on_mk_http_access(const mk_parser parser, */ void API_CALL on_mk_http_before_access(const mk_parser parser, char *path, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d, path: %s\n" "%s %s?%s %s\n" "User-Agent: %s\n" "%s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), path, mk_parser_get_method(parser), mk_parser_get_url(parser), @@ -251,14 +251,14 @@ void API_CALL on_mk_http_before_access(const mk_parser parser, */ void API_CALL on_mk_rtsp_get_realm(const mk_media_info url_info, const mk_rtsp_get_realm_invoker invoker, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s/%s/%s/%s, url params: %s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_media_info_get_schema(url_info), mk_media_info_get_vhost(url_info), mk_media_info_get_app(url_info), @@ -284,16 +284,16 @@ void API_CALL on_mk_rtsp_auth(const mk_media_info url_info, const char *user_name, int must_no_encrypt, const mk_rtsp_auth_invoker invoker, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV, "client info, local: %s:%d, peer: %s:%d\n" "%s/%s/%s/%s, url params: %s\n" "realm: %s, user_name: %s, must_no_encrypt: %d", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), mk_media_info_get_schema(url_info), mk_media_info_get_vhost(url_info), mk_media_info_get_app(url_info), @@ -338,13 +338,13 @@ void API_CALL on_mk_record_mp4(const mk_mp4_info mp4) { void API_CALL on_mk_shell_login(const char *user_name, const char *passwd, const mk_auth_invoker invoker, - const mk_tcp_session sender) { + const mk_sock_info sender) { log_printf(LOG_LEV,"client info, local: %s:%d, peer: %s:%d\n" "user_name: %s, passwd: %s", - mk_tcp_session_local_ip(sender), - mk_tcp_session_local_port(sender), - mk_tcp_session_peer_ip(sender), - mk_tcp_session_peer_port(sender), + mk_sock_info_local_ip(sender), + mk_sock_info_local_port(sender), + mk_sock_info_peer_ip(sender), + mk_sock_info_peer_port(sender), user_name, passwd); //允许登录shell mk_auth_invoker_do(invoker, NULL); diff --git a/server/WebApi.cpp b/server/WebApi.cpp index c22be830..775c80f2 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -86,7 +86,7 @@ public: ~SuccessException() = default; }; -#define API_ARGS1 TcpSession &sender,HttpSession::KeyValue &headerIn, HttpSession::KeyValue &headerOut, ApiArgsType &allArgs, Json::Value &val +#define API_ARGS1 SockInfo &sender,HttpSession::KeyValue &headerIn, HttpSession::KeyValue &headerOut, ApiArgsType &allArgs, Json::Value &val #define API_ARGS2 API_ARGS1, const HttpSession::HttpResponseInvoker &invoker #define API_ARGS_VALUE1 sender,headerIn,headerOut,allArgs,val #define API_ARGS_VALUE2 API_ARGS_VALUE1, invoker diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 1947900a..a350704f 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -190,7 +190,7 @@ void findAsync_l(const MediaInfo &info, const std::shared_ptr &sessi void *listener_tag = session.get(); weak_ptr weakSession = session; //广播未找到流,此时可以立即去拉流,这样还来得及 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastNotFoundStream,info,*session); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastNotFoundStream,info, static_cast(*session)); //最多等待一定时间,如果这个时间内,流未注册上,那么返回未找到流 GET_CONFIG(int,maxWaitMS,General::kMaxStreamWaitTimeMS); diff --git a/src/Common/config.h b/src/Common/config.h index 2c681e05..84b5024a 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -62,25 +62,25 @@ extern const string kBroadcastRecordMP4; //收到http api请求广播 extern const string kBroadcastHttpRequest; -#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,TcpSession &sender +#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,SockInfo &sender //在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限 extern const string kBroadcastHttpAccess; -#define BroadcastHttpAccessArgs const Parser &parser,const string &path,const bool &is_dir,const HttpSession::HttpAccessPathInvoker &invoker,TcpSession &sender +#define BroadcastHttpAccessArgs const Parser &parser,const string &path,const bool &is_dir,const HttpSession::HttpAccessPathInvoker &invoker,SockInfo &sender //在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射 //在该事件中通过自行覆盖path参数,可以做到譬如根据虚拟主机或者app选择不同http根目录的目的 extern const string kBroadcastHttpBeforeAccess; -#define BroadcastHttpBeforeAccessArgs const Parser &parser,string &path,TcpSession &sender +#define BroadcastHttpBeforeAccessArgs const Parser &parser,string &path,SockInfo &sender //该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证 extern const string kBroadcastOnGetRtspRealm; -#define BroadcastOnGetRtspRealmArgs const MediaInfo &args,const RtspSession::onGetRealm &invoker,TcpSession &sender +#define BroadcastOnGetRtspRealmArgs const MediaInfo &args,const RtspSession::onGetRealm &invoker,SockInfo &sender //请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败 //获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码 extern const string kBroadcastOnRtspAuth; -#define BroadcastOnRtspAuthArgs const MediaInfo &args,const string &realm,const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,TcpSession &sender +#define BroadcastOnRtspAuthArgs const MediaInfo &args,const string &realm,const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,SockInfo &sender //推流鉴权结果回调对象 //如果errMessage为空则代表鉴权成功 @@ -91,7 +91,7 @@ typedef std::function AuthInvoker; //播放rtsp/rtmp/http-flv事件广播,通过该事件控制播放鉴权 extern const string kBroadcastMediaPlayed; -#define BroadcastMediaPlayedArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,TcpSession &sender +#define BroadcastMediaPlayedArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,SockInfo &sender //shell登录鉴权 extern const string kBroadcastShellLogin; -#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker,TcpSession &sender +#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker,SockInfo &sender //停止rtsp/rtmp/http-flv会话后流量汇报事件广播 extern const string kBroadcastFlowReport; @@ -111,7 +111,7 @@ extern const string kBroadcastFlowReport; //未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了 extern const string kBroadcastNotFoundStream; -#define BroadcastNotFoundStreamArgs const MediaInfo &args,TcpSession &sender +#define BroadcastNotFoundStreamArgs const MediaInfo &args,SockInfo &sender //某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑 extern const string kBroadcastStreamNoneReader; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 1dec693f..f505abbd 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -306,7 +306,7 @@ static bool emitHlsPlayed(const Parser &parser, const MediaInfo &mediaInfo, cons //cookie有效期为kHlsCookieSecond invoker(err,"",kHlsCookieSecond); }; - return NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,mediaInfo,mediaAuthInvoker,sender); + return NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,mediaInfo,mediaAuthInvoker,static_cast(sender)); } @@ -407,7 +407,7 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI } //事件未被拦截,则认为是http下载请求 - bool flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpAccess, parser, path, is_dir, accessPathInvoker, sender); + bool flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpAccess, parser, path, is_dir, accessPathInvoker, static_cast(sender)); if (!flag) { //此事件无人监听,我们默认都有权限访问 callback("", nullptr); @@ -521,7 +521,7 @@ static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSe GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(string, rootPath, Http::kRootPath); auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + parser.Url() : parser.Url(), rootPath); - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, sender); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast(sender)); return std::move(ret); } diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 441894e8..9e443e31 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -241,7 +241,7 @@ bool HttpSession::checkLiveFlvStream(const function &cb){ onRes(err); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,*this); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,static_cast(*this)); if(!flag){ //该事件无人监听,默认不鉴权 onRes(""); @@ -520,7 +520,8 @@ bool HttpSession::emitHttpEvent(bool doInvoke){ }; ///////////////////广播HTTP事件/////////////////////////// bool consumed = false;//该事件是否被消费 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpRequest,_parser,invoker,consumed,*this); + TcpSession &session = static_cast(*this); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpRequest,_parser,invoker,consumed,static_cast(*this)); if(!consumed && doInvoke){ //该事件无人消费,所以返回404 invoker("404 Not Found",KeyValue(), HttpBody::Ptr()); diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 8e8ca0ed..632f016a 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -171,10 +171,7 @@ void RtmpSession::onCmd_publish(AMFDecoder &dec) { onRes(err,enableRtxp,enableHls,enableMP4); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPublish, - _mediaInfo, - invoker, - *this); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPublish,_mediaInfo,invoker,static_cast(*this)); if(!flag){ //该事件无人监听,默认鉴权成功 GET_CONFIG(bool,toRtxp,General::kPublishToRtxp); @@ -346,7 +343,8 @@ void RtmpSession::doPlay(AMFDecoder &dec){ strongSelf->doPlayResponse(err,[pToken](bool){}); }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,*this); + + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,static_cast(*this)); if(!flag){ //该事件无人监听,默认不鉴权 doPlayResponse("",[pToken](bool){}); diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index c6ca0038..247e5893 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -302,7 +302,7 @@ void RtspSession::handleReq_RECORD(const Parser &parser){ }; //rtsp推流需要鉴权 - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPublish,_mediaInfo,invoker,*this); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPublish,_mediaInfo,invoker,static_cast(*this)); if(!flag){ //该事件无人监听,默认不鉴权 GET_CONFIG(bool,toRtxp,General::kPublishToRtxp); @@ -341,10 +341,7 @@ void RtspSession::handleReq_Describe(const Parser &parser) { }; //广播是否需要认证事件 - if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnGetRtspRealm, - _mediaInfo, - invoker, - *this)){ + if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnGetRtspRealm,_mediaInfo,invoker,static_cast(*this))){ //无人监听此事件,说明无需认证 invoker(""); } @@ -446,7 +443,7 @@ void RtspSession::onAuthBasic(const string &realm,const string &strBase64){ }; //此时必须提供明文密码 - if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,_mediaInfo,realm,user, true,invoker,*this)){ + if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,_mediaInfo,realm,user, true,invoker,static_cast(*this))){ //表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之 WarnP(this) << "请监听kBroadcastOnRtspAuth事件!"; //但是我们还是忽略认证以便完成播放 @@ -530,7 +527,7 @@ void RtspSession::onAuthDigest(const string &realm,const string &strMd5){ }; //此时可以提供明文或md5加密的密码 - if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,_mediaInfo,realm,username, false,invoker,*this)){ + if(!NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastOnRtspAuth,_mediaInfo,realm,username, false,invoker,static_cast(*this))){ //表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之 WarnP(this) << "请监听kBroadcastOnRtspAuth事件!"; //但是我们还是忽略认证以便完成播放 @@ -824,7 +821,7 @@ void RtspSession::handleReq_Play(const Parser &parser) { }; if(_bFirstPlay){ //第一次收到play命令,需要鉴权 - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,*this); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,_mediaInfo,invoker,static_cast(*this)); if(!flag){ //该事件无人监听,默认不鉴权 onRes(""); diff --git a/src/Shell/ShellSession.cpp b/src/Shell/ShellSession.cpp index 358eac06..0345d879 100644 --- a/src/Shell/ShellSession.cpp +++ b/src/Shell/ShellSession.cpp @@ -136,7 +136,7 @@ inline void ShellSession::pleaseInputPasswd() { }); }; - auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,_strUserName,passwd,invoker,*this); + auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,_strUserName,passwd,invoker,static_cast(*this)); if(!flag){ //如果无人监听shell登录事件,那么默认shell无法登录 onAuth("please listen kBroadcastShellLogin event");