cf_void Set() { WritePid(); }
QTSS_ServerState StartServer(XMLPrefsParser* inPrefsSource, PrefsSource* inMessagesSource, UInt16 inPortOverride, int statsUpdateInterval, QTSS_ServerState inInitialState, Bool16 inDontFork, UInt32 debugLevel, UInt32 debugOptions) { //Mark when we are done starting up. If auto-restart is enabled, we want to make sure //to always exit with a status of 0 if we encountered a problem WHILE STARTING UP. This //will prevent infinite-auto-restart-loop type problems Bool16 doneStartingUp = false; QTSS_ServerState theServerState = qtssStartingUpState; sStatusUpdateInterval = statsUpdateInterval; //Initialize utility classes OS::Initialize(); OSThread::Initialize(); Socket::Initialize(); SocketUtils::Initialize(!inDontFork); #if !MACOSXEVENTQUEUE ::select_startevents();//initialize the select() implementation of the event queue #endif //start the server QTSSDictionaryMap::Initialize(); QTSServerInterface::Initialize();// this must be called before constructing the server object sServer = NEW QTSServer(); sServer->SetDebugLevel(debugLevel); sServer->SetDebugOptions(debugOptions); // re-parse config file inPrefsSource->Parse(); Bool16 createListeners = true; if (qtssShuttingDownState == inInitialState) createListeners = false; sServer->Initialize(inPrefsSource, inMessagesSource, inPortOverride,createListeners); if (inInitialState == qtssShuttingDownState) { sServer->InitModules(inInitialState); return inInitialState; } OSCharArrayDeleter runGroupName(sServer->GetPrefs()->GetRunGroupName()); OSCharArrayDeleter runUserName(sServer->GetPrefs()->GetRunUserName()); OSThread::SetPersonality(runUserName.GetObject(), runGroupName.GetObject()); if (sServer->GetServerState() != qtssFatalErrorState) { UInt32 numShortTaskThreads = 0; UInt32 numBlockingThreads = 0; UInt32 numThreads = 0; UInt32 numProcessors = 0; if (OS::ThreadSafe()) { numShortTaskThreads = sServer->GetPrefs()->GetNumThreads(); // whatever the prefs say if (numShortTaskThreads == 0) { numProcessors = OS::GetNumProcessors(); // 1 worker thread per processor, up to 2 threads. // Note: Limiting the number of worker threads to 2 on a MacOS X system with > 2 cores // results in better performance on those systems, as of MacOS X 10.5. Future // improvements should make this limit unnecessary. if (numProcessors > 2) numShortTaskThreads = 2; else numShortTaskThreads = numProcessors; } numBlockingThreads = sServer->GetPrefs()->GetNumBlockingThreads(); // whatever the prefs say if (numBlockingThreads == 0) numBlockingThreads = 1; } if (numShortTaskThreads == 0) numShortTaskThreads = 1; if (numBlockingThreads == 0) numBlockingThreads = 1; numThreads = numShortTaskThreads + numBlockingThreads; //qtss_printf("Add threads shortask=%lu blocking=%lu\n",numShortTaskThreads, numBlockingThreads); TaskThreadPool::SetNumShortTaskThreads(numShortTaskThreads); TaskThreadPool::SetNumBlockingTaskThreads(numBlockingThreads); TaskThreadPool::AddThreads(numThreads); sServer->InitNumThreads(numThreads); #if DEBUG qtss_printf("Number of task threads: %"_U32BITARG_"\n",numThreads); #endif // Start up the server's global tasks, and start listening TimeoutTask::Initialize(); // The TimeoutTask mechanism is task based, // we therefore must do this after adding task threads // this be done before starting the sockets and server tasks } //Make sure to do this stuff last. Because these are all the threads that //do work in the server, this ensures that no work can go on while the server //is in the process of staring up if (sServer->GetServerState() != qtssFatalErrorState) { IdleTask::Initialize(); Socket::StartThread(); OSThread::Sleep(1000); // // On Win32, in order to call modwatch the Socket EventQueue thread must be // created first. Modules call modwatch from their initializer, and we don't // want to prevent them from doing that, so module initialization is separated // out from other initialization, and we start the Socket EventQueue thread first. // The server is still prevented from doing anything as of yet, because there // aren't any TaskThreads yet. sServer->InitModules(inInitialState); sServer->StartTasks(); sServer->SetupUDPSockets(); // udp sockets are set up after the rtcp task is instantiated theServerState = sServer->GetServerState(); } if (theServerState != qtssFatalErrorState) { CleanPid(true); WritePid(!inDontFork); doneStartingUp = true; qtss_printf("Streaming Server done starting up\n"); OSMemory::SetMemoryError(ENOMEM); } // SWITCH TO RUN USER AND GROUP ID //if (!sServer->SwitchPersonality()) // theServerState = qtssFatalErrorState; // // Tell the caller whether the server started up or not return theServerState; }
//启动服务 QTSS_ServerState StartServer(XMLPrefsParser* inPrefsSource, PrefsSource* inMessagesSource, UInt16 inPortOverride, int statsUpdateInterval, QTSS_ServerState inInitialState, Bool16 inDontFork, UInt32 debugLevel, UInt32 debugOptions) { //Mark when we are done starting up. If auto-restart is enabled, we want to make sure //to always exit with a status of 0 if we encountered a problem WHILE STARTING UP. This //will prevent infinite-auto-restart-loop type problems Bool16 doneStartingUp = false; QTSS_ServerState theServerState = qtssStartingUpState; //服务单元状态更新间隔 sStatusUpdateInterval = statsUpdateInterval; //Initialize utility classes OS::Initialize(); OSThread::Initialize(); //建立网络事件(R/W/E事件)线程,仅建立,But未启动 Socket::Initialize(); //获取系统网卡数量sNumIPAddrs及对应的具体ip,存储在sIPAddrInfoArray结构体数组中 SocketUtils::Initialize(!inDontFork); /* #if !MACOSXEVENTQUEUE ::select_startevents();//initialize the select() implementation of the event queue #endif */ #if !MACOSXEVENTQUEUE #ifndef __Win32__ ::epollInit(); #else ::select_startevents();//initialize the select() implementation of the event queue #endif #endif //初始化系统属性字典 QTSSDictionaryMap::Initialize(); //初始化Server对象属性字典,包括其他具有属性字典的类,都要先进行Initialize QTSServerInterface::Initialize();//此部分必须在QTSServer对象构造前调用 sServer = NEW QTSServer(); sServer->SetDebugLevel(debugLevel); sServer->SetDebugOptions(debugOptions); //重新解析xml配置 inPrefsSource->Parse(); //准备开启监听,接收并处理相关事务 Bool16 createListeners = true; if (qtssShuttingDownState == inInitialState) createListeners = false; //初始化服务实例 sServer->Initialize(inPrefsSource, inMessagesSource, inPortOverride, createListeners); if (inInitialState == qtssShuttingDownState) { sServer->InitModules(inInitialState); return inInitialState; } if (sServer->GetServerState() != qtssFatalErrorState) { UInt32 numShortTaskThreads = 0; UInt32 numBlockingThreads = 0; UInt32 numThreads = 0; UInt32 numProcessors = 0; if (OS::ThreadSafe()) { //工作线程 numShortTaskThreads = sServer->GetPrefs()->GetNumThreads(); // whatever the prefs say if (numShortTaskThreads == 0) { numProcessors = OS::GetNumProcessors(); // 1 worker thread per processor, up to 2 threads. // Note:Limiting the number of worker threads to 2 on a MacOS X system with > 2 cores // results in better performance on those systems, as of MacOS X 10.5. Future // improvements should make this limit unnecessary. if (numProcessors > 2) numShortTaskThreads = 2; else numShortTaskThreads = numProcessors; } //协议处理线程 numBlockingThreads = sServer->GetPrefs()->GetNumBlockingThreads(); // whatever the prefs say if (numBlockingThreads == 0) numBlockingThreads = 1; } if (numShortTaskThreads == 0) numShortTaskThreads = 1; if (numBlockingThreads == 0) numBlockingThreads = 1; numThreads = numShortTaskThreads + numBlockingThreads; //qtss_printf("Add threads shortask=%lu blocking=%lu\n",numShortTaskThreads, numBlockingThreads); TaskThreadPool::SetNumShortTaskThreads(numShortTaskThreads); TaskThreadPool::SetNumBlockingTaskThreads(numBlockingThreads); TaskThreadPool::AddThreads(numThreads); sServer->InitNumThreads(numThreads); #if DEBUG qtss_printf("Number of task threads: %" _U32BITARG_ "\n",numThreads); #endif // Start up the server's global tasks, and start listening //超时事件处理线程 TimeoutTask::Initialize(); // The TimeoutTask mechanism is task based, // we therefore must do this after adding task threads // this be done before starting the sockets and server tasks } //Make sure to do this stuff last. Because these are all the threads that //do work in the server, this ensures that no work can go on while the server //is in the process of staring up if (sServer->GetServerState() != qtssFatalErrorState) { //Idle事务处理线程 IdleTask::Initialize(); Socket::StartThread(); OSThread::Sleep(1000); // // On Win32, in order to call modwatch the Socket EventQueue thread must be // created first. Modules call modwatch from their initializer, and we don't // want to prevent them from doing that, so module initialization is separated // out from other initialization, and we start the Socket EventQueue thread first. // The server is still prevented from doing anything as of yet, because there // aren't any TaskThreads yet. sServer->InitModules(inInitialState); sServer->StartTasks(); theServerState = sServer->GetServerState(); } if (theServerState != qtssFatalErrorState) { CleanPid(true); WritePid(!inDontFork); doneStartingUp = true; //Log char msgStr[128]; qtss_snprintf(msgStr, sizeof(msgStr), "EasyCMS Service done starting up"); QTSServerInterface::LogError(qtssMessageVerbosity, msgStr); OSMemory::SetMemoryError(ENOMEM); } //// SWITCH TO RUN USER AND GROUP ID //if (!sServer->SwitchPersonality()) // theServerState = qtssFatalErrorState; // Tell the caller whether the server started up or not return theServerState; }