EasyDarwin實現說明之添加模塊

在Darwin的架構中一個很重要的概念就是模塊(Module),我們在原生Darwin系統上加入了兩個模塊(Module),是MyBCInteractModule和MyFileUploadModule,每個模塊都要提供兩個函數,Main和dispatch函數,服務器啟動時(QTSServer)會調用Main函數完成對該模塊的初始化;Dispatch是註冊給服務器的一個回調函數,服務器通過該函數分發該模塊訂閱的各種角色,這些角色定義如下:/********************************************************************/// QTSS API ROLES//// Each role represents a unique situation in which a module may be// invoked. Modules must specify which roles they want to be invoked for. enum{ //Global QTSS_Register_Role = FOUR_CHARS_TO_INT('r', 'e', 'g', ' '), //reg //All modules get this once at startup QTSS_Initialize_Role = FOUR_CHARS_TO_INT('i', 'n', 'i', 't'), //init //Gets called once, later on in the startup process QTSS_Shutdown_Role = FOUR_CHARS_TO_INT('s', 'h', 'u', 't'), //shut //Gets called once at shutdown QTSS_ErrorLog_Role = FOUR_CHARS_TO_INT('e', 'l', 'o', 'g'), //elog //This gets called when the server wants to log an error. QTSS_RereadPrefs_Role = FOUR_CHARS_TO_INT('p', 'r', 'e', 'f'), //pref //This gets called when the server rereads preferences. QTSS_StateChange_Role = FOUR_CHARS_TO_INT('s', 't', 'a', 't'), //stat //This gets called whenever the server changes state. QTSS_Interval_Role = FOUR_CHARS_TO_INT('t', 'i', 'm', 'r'), //timr //This gets called whenever the module's interval timer times out calls. //RTSP-specific QTSS_RTSPFilter_Role = FOUR_CHARS_TO_INT('f', 'i', 'l', 't'), //filt //Filter all RTSP requests before the server parses them QTSS_RTSPRoute_Role = FOUR_CHARS_TO_INT('r', 'o', 'u', 't'), //rout //Route all RTSP requests to the correct root folder. QTSS_RTSPAuthenticate_Role = FOUR_CHARS_TO_INT('a', 't', 'h', 'n'), //athn //Authenticate the RTSP request username. QTSS_RTSPAuthorize_Role = FOUR_CHARS_TO_INT('a', 'u', 't', 'h'), //auth //Authorize RTSP requests to proceed QTSS_RTSPPreProcessor_Role = FOUR_CHARS_TO_INT('p', 'r', 'e', 'p'), //prep //Pre-process all RTSP requests before the server responds. //Modules may opt to "steal" the request and return a client response. QTSS_RTSPRequest_Role = FOUR_CHARS_TO_INT('r', 'e', 'q', 'u'), //requ //Process an RTSP request & send client response QTSS_RTSPPostProcessor_Role = FOUR_CHARS_TO_INT('p', 'o', 's', 't'), //post //Post-process all RTSP requests QTSS_RTSPSessionClosing_Role = FOUR_CHARS_TO_INT('s', 'e', 's', 'c'), //sesc //RTSP session is going away QTSS_RTSPIncomingData_Role = FOUR_CHARS_TO_INT('i', 'c', 'm', 'd'), //icmd //Incoming interleaved RTP data on this RTSP connection //RTP-specific QTSS_RTPSendPackets_Role = FOUR_CHARS_TO_INT('s', 'e', 'n', 'd'), //send //Send RTP packets to the client QTSS_ClientSessionClosing_Role = FOUR_CHARS_TO_INT('d', 'e', 's', 's'), //dess //Client session is going away //RTCP-specific QTSS_RTCPProcess_Role = FOUR_CHARS_TO_INT('r', 't', 'c', 'p'), //rtcp //Process all RTCP packets sent to the server //File system roles QTSS_OpenFilePreProcess_Role = FOUR_CHARS_TO_INT('o', 'p', 'p', 'r'), //oppr QTSS_OpenFile_Role = FOUR_CHARS_TO_INT('o', 'p', 'f', 'l'), //opfl QTSS_AdviseFile_Role = FOUR_CHARS_TO_INT('a', 'd', 'f', 'l'), //adfl QTSS_ReadFile_Role = FOUR_CHARS_TO_INT('r', 'd', 'f', 'l'), //rdfl QTSS_CloseFile_Role = FOUR_CHARS_TO_INT('c', 'l', 'f', 'l'), //clfl QTSS_RequestEventFile_Role = FOUR_CHARS_TO_INT('r', 'e', 'f', 'l'), //refl //HLS Session Easy_HLSOpen_Role = FOUR_CHARS_TO_INT('h', 'l', 's', 'o'), //hlso Easy_HLSClose_Role = FOUR_CHARS_TO_INT('h', 'l', 's', 'c'), //hlsc };typedef UInt32 QTSS_Role;每個模塊的main可以如下:QTSS_Error MyBCInteractModule_Main(void* inPrivateArgs){ return _stublibrary_main(inPrivateArgs, MyBCInteractModuleDispatch);}其中MyBCInteractModuleDispatch是Dispatch函數,該函數負責對各種角色的處理,如:QTSS_Error MyBCInteractModuleDispatch(QTSS_Role inRole, QTSS_RoleParamPtr inParams){ switch (inRole) { case QTSS_Register_Role: return Register(&inParams->regParams); break; case QTSS_Initialize_Role: return Initialize(&inParams->initParams); break; case QTSS_RereadPrefs_Role: return RereadPrefs(); break; case QTSS_RTSPAuthenticate_Role: return AuthenticateRTSPRequest(&inParams->rtspAthnParams); break; case QTSS_RTSPAuthorize_Role: return AccessAuthorizeRTSPRequest(&inParams->rtspRequestParams); break; case QTSS_Shutdown_Role: return Shutdown(); break; } return QTSS_NoErr;}那麼,MyBCInteractModule和!MyFileUploadModule是在什麼時候註冊到Darwin系統中的呢?答案是QTSServer::LoadCompiledInModules,在調用!SetupModule時傳人了模塊的Main函數,SetupModule時會調用該main函數,也就獲取到了該模塊的dispatch函數,在AddModule時,調用dispatch函數分發QTSS_Register_Role角色,此時MyBCInteractModule和MyFileUploadModule會去添加自己感興趣的角色,最後在AddModule裡把MyBCInteractModule和MyFileUploadModule添加到隊列裡。到此,添加模塊算是完成了。void QTSServer::LoadCompiledInModules(){#ifndef DSS_DYNAMIC_MODULES_ONLY // // The following modules are all compiled into the server. QTSSModule* theFileModule = new QTSSModule("QTSSFileModule"); (void)theFileModule->SetupModule(&sCallbacks, &QTSSFileModule_Main); (void)AddModule(theFileModule); //與業務服務器通信 QTSSModule* theBCInteractModule = new QTSSModule("MyBCInteractModule"); (void)theBCInteractModule->SetupModule(&sCallbacks, &MyBCInteractModule_Main); (void)AddModule(theBCInteractModule); //文件上傳模塊 QTSSModule* theFileUploadModule = new QTSSModule("MyFileUploadModule"); (void)theFileUploadModule->SetupModule(&sCallbacks, &MyFileUploadModule_Main); (void)AddModule(theFileUploadModule); xxxxxx} 

EasyDarwin實現說明之添加模塊

每天會更新論文和視頻,還有如果想學習c++知識推薦在晚上8.30免費觀看這個直播:https://ke.qq.com/course/131973#tuin=b52b9a80


分享到:


相關文章: