【Darwin学习笔记】之RTSP连接监听初始化

网上有很多大侠已经对Darwin做了详细的剖析,在下仅本着积累经验的目的,将Darwin的学习过程记录下来,同时与网友们交流学习,今天先来分析下Darwin如果根据配置文件监听RTSP连接的流程。

1. Darwin系统初始化,Bool16 QTSServer::Initialize(.....)

2. QTSServer::CreateListeners(...)根据本地xml配置文件中的地址和端口进行监听的建立,主要有两个配置项:

<PREF NAME="bind_ip_addr" >0</PREF>
<LIST-PREF NAME="rtsp_port" TYPE="UInt16" >

3.  void QTSServer::StartTasks() 开始监听上一步建立起来的Socket 列表


[cpp]  view plain  copy
 print ?
  1. Bool16 QTSServer::CreateListeners(Bool16 startListeningNow, QTSServerPrefs* inPrefs, UInt16 inPortOverride)  
  2. {  
  3.     struct PortTracking  
  4.     {  
  5.         PortTracking() : fPort(0), fIPAddr(0), fNeedsCreating(true) {}  
  6.           
  7.         UInt16 fPort;  //端口号  
  8.         UInt32 fIPAddr;//地址  
  9.         Bool16 fNeedsCreating;//是否需要创建RTSPListenerSocket,默认创建  
  10.     };  
  11.       
  12.     PortTracking* thePortTrackers = NULL;     
  13.     UInt32 theTotalPortTrackers = 0;  
  14.       
  15.     // Get the IP addresses from the pref 获取配置文件中用于监听RTSP连接的IP地址和端口号  
  16.     UInt32 theNumAddrs = 0;  
  17.     UInt32* theIPAddrs = this->GetRTSPIPAddrs(inPrefs, &theNumAddrs);     
  18.     UInt32 index = 0;  
  19.       
  20.     //inPortOverride该参数为命令行参数传入,默认为0. -p XXX: Specify the default RTSP listening port of the server  
  21.     if ( inPortOverride != 0)  
  22.     {  
  23.         theTotalPortTrackers = theNumAddrs; // one port tracking struct for each IP addr  
  24.         thePortTrackers = NEW PortTracking[theTotalPortTrackers];  
  25.         for (index = 0; index < theNumAddrs; index++)  
  26.         {  
  27.             thePortTrackers[index].fPort = inPortOverride;    
  28.             thePortTrackers[index].fIPAddr = theIPAddrs[index];  
  29.         }  
  30.     }  
  31.     else  
  32.     {  
  33.         UInt32 theNumPorts = 0;  
  34.         UInt16* thePorts = GetRTSPPorts(inPrefs, &theNumPorts);  //根据xml配置文件中的<LIST-PREF NAME="rtsp_port" TYPE="UInt16" >获取端口号  
  35.         theTotalPortTrackers = theNumAddrs * theNumPorts;  
  36.         thePortTrackers = NEW PortTracking[theTotalPortTrackers];//一共需要监听的数量为:端口数 * IP地址数  
  37.           
  38.         UInt32 currentIndex  = 0;  
  39.           
  40.           
  41.         for (index = 0; index < theNumAddrs; index++)  
  42.         {  
  43.             for (UInt32 portIndex = 0; portIndex < theNumPorts; portIndex++)  
  44.             {  
  45.                 currentIndex = (theNumPorts * index) + portIndex;  
  46.                   
  47.                 thePortTrackers[currentIndex].fPort = thePorts[portIndex];  
  48.                 thePortTrackers[currentIndex].fIPAddr = theIPAddrs[index];  
  49.             }  
  50.         }  
  51.                   
  52.                 delete [] thePorts;  
  53.     }  
  54.       
  55.         delete [] theIPAddrs;  
  56.     //  
  57.     // Now figure out which of these ports we are *already* listening on.  
  58.     // If we already are listening on that port, just move the pointer to the  
  59.     // listener over to the new array  
  60.     TCPListenerSocket** newListenerArray = NEW TCPListenerSocket*[theTotalPortTrackers];  
  61.     UInt32 curPortIndex = 0;  
  62.       
  63.     for (UInt32 count = 0; count < theTotalPortTrackers; count++)  
  64.     {  
  65.         for (UInt32 count2 = 0; count2 < fNumListeners; count2++)  
  66.         {  
  67.             if ((fListeners[count2]->GetLocalPort() == thePortTrackers[count].fPort) &&  
  68.                 (fListeners[count2]->GetLocalAddr() == thePortTrackers[count].fIPAddr))  
  69.             {  
  70.                 //如果fListeners监听列表中已经对该ip+port进行了监听,那么就不再监听;  
  71.                 thePortTrackers[count].fNeedsCreating = false;  
  72.                 newListenerArray[curPortIndex++] = fListeners[count2];  
  73.                 Assert(curPortIndex <= theTotalPortTrackers);  
  74.                 break;  
  75.             }  
  76.         }  
  77.     }  
  78.       
  79.     //  
  80.     // 创建为需要监听的IP+PORT创建RTSPListenerSocket  
  81.     for (UInt32 count3 = 0; count3 < theTotalPortTrackers; count3++)  
  82.     {  
  83.         if (thePortTrackers[count3].fNeedsCreating) //不需要监听的已经在上一步把该字段置为false了.  
  84.         {  
  85.             //创建RTSP监听任务;  
  86.             newListenerArray[curPortIndex] = NEW RTSPListenerSocket();  
  87.             QTSS_Error err = newListenerArray[curPortIndex]->Initialize(thePortTrackers[count3].fIPAddr, thePortTrackers[count3].fPort);  
  88.   
  89.             char thePortStr[20];  
  90.             qtss_sprintf(thePortStr, "%hu", thePortTrackers[count3].fPort);  
  91.               
  92.             //  
  93.             // If there was an error creating this listener, destroy it and log an error  
  94.             if ((startListeningNow) && (err != QTSS_NoErr))  
  95.                 delete newListenerArray[curPortIndex];  
  96.   
  97.             if (err == EADDRINUSE)  
  98.             {  
  99.                 QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortInUse, 0, thePortStr);  
  100.             }  
  101.             else if (err == EACCES)  
  102.             {  
  103.                 QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortAccessDenied, 0, thePortStr);  
  104.             }  
  105.             else if (err != QTSS_NoErr)  
  106.             {  
  107.                 QTSSModuleUtils::LogError(qtssWarningVerbosity, qtssListenPortError, 0, thePortStr);  
  108.             }  
  109.             else  
  110.             {  
  111.                 //将第一个可用端口赋值给fOKPort,保存,后续通过GetOKPort()对外提供RTSP监听端口即为此端口;  
  112.                 if(fOKPort == 0)  
  113.                 {  
  114.                     fOKPort = thePortTrackers[count3].fPort;  
  115.                 }  
  116.   
  117.                 //  
  118.                 // This listener was successfully created.如果需要监听,则立即开始监听该socket上面的连接.  
  119.                 if (startListeningNow)  
  120.                     newListenerArray[curPortIndex]->RequestEvent(EV_RE);  
  121.                 curPortIndex++;  
  122.             }  
  123.         }  
  124.     }  
  125.       
  126.     //  
  127.     //将fListeners监听列表中未在配置文件监听范围内的socket杀掉~  
  128.     for (UInt32 count4 = 0; count4 < fNumListeners; count4++)  
  129.     {  
  130.         Bool16 deleteThisOne = true;  
  131.           
  132.         for (UInt32 count5 = 0; count5 < curPortIndex; count5++)  
  133.         {  
  134.             if (newListenerArray[count5] == fListeners[count4])  
  135.                 deleteThisOne = false;  
  136.         }  
  137.           
  138.         if (deleteThisOne)  
  139.             fListeners[count4]->Signal(Task::kKillEvent);  
  140.     }  
  141.       
  142.     //  
  143.     // Finally, make our server attributes and fListener privy to the new...最后将监听列表赋给fListeners,监听数量赋给fNumListeners  
  144.     fListeners = newListenerArray;  
  145.     fNumListeners = curPortIndex;  
  146.     UInt32 portIndex = 0;  
  147.     //将监听的端口号列表保存起来,key值为qtssSvrRTSPPorts  
  148.     for (UInt32 count6 = 0; count6 < fNumListeners; count6++)  
  149.     {  
  150.         if  (fListeners[count6]->GetLocalAddr() != INADDR_LOOPBACK)  
  151.         {  
  152.             UInt16 thePort = fListeners[count6]->GetLocalPort();  
  153.             (void)this->SetValue(qtssSvrRTSPPorts, portIndex, &thePort, sizeof(thePort), QTSSDictionary::kDontObeyReadOnly);  
  154.             portIndex++;  
  155.         }  
  156.     }  
  157.     //设置端口列表的个数;  
  158.     this->SetNumValues(qtssSvrRTSPPorts, portIndex);  
  159.   
  160.     delete [] thePortTrackers;  
  161.     return (fNumListeners > 0);  
  162. }  

从配置文件中获取RTSP监听地址列表,即<PREF NAME="bind_ip_addr" >0</PREF>配置项,系统默认是为0,该配置项在多网卡的服务器上可能需要用到,一般我们调试学习的时候可以不考虑。但是还是介绍下,也比较简单。
[cpp]  view plain  copy
 print ?
  1. UInt32* QTSServer::GetRTSPIPAddrs(QTSServerPrefs* inPrefs, UInt32* outNumAddrsPtr)  
  2. {  
  3.     UInt32 numAddrs = inPrefs->GetNumValues(qtssPrefsRTSPIPAddr);//获取配置文件中rtsp监听地址的数量;  
  4.     UInt32* theIPAddrArray = NULL;  
  5.       
  6.     if (numAddrs == 0)//如果配置的监听地址数量为0,则设置为INADDR_ANY  
  7.     {  
  8.         *outNumAddrsPtr = 1;  
  9.         theIPAddrArray = NEW UInt32[1];  
  10.         theIPAddrArray[0] = INADDR_ANY;//INADDR_ANY就是指定地址为0.0.0.0的地址,这个地址事实上表示不确定地址,或“所有地址”、“任意地址”。   
  11.     }  
  12.     else  
  13.     {  
  14.         theIPAddrArray = NEW UInt32[numAddrs + 1];  
  15.         UInt32 arrIndex = 0;  
  16.           
  17.         for (UInt32 theIndex = 0; theIndex < numAddrs; theIndex++)  
  18.         {  
  19.             // Get the ip addr out of the prefs dictionary  
  20.             QTSS_Error theErr = QTSS_NoErr;  
  21.               
  22.             char* theIPAddrStr = NULL;  
  23.             theErr = inPrefs->GetValueAsString(qtssPrefsRTSPIPAddr, theIndex, &theIPAddrStr);  
  24.             if (theErr != QTSS_NoErr)  
  25.             {  
  26.                 delete [] theIPAddrStr;  
  27.                 break;  
  28.             }  
  29.   
  30.               
  31.             UInt32 theIPAddr = 0;  
  32.             if (theIPAddrStr != NULL)  
  33.             {  
  34.                 //IP地址从字符串转换成整型,并从网络字节顺序转换为主机字节顺序  
  35.                 theIPAddr = SocketUtils::ConvertStringToAddr(theIPAddrStr);  
  36.                 delete [] theIPAddrStr;  
  37.                   
  38.                 if (theIPAddr != 0)  
  39.                     theIPAddrArray[arrIndex++] = theIPAddr;  
  40.             }     
  41.         }  
  42.           
  43.         if ((numAddrs == 1) && (arrIndex == 0))  //如果配置文件的IP地址数量为1,但是解析不到,则同样按照INADDR_ANY方式监听  
  44.             theIPAddrArray[arrIndex++] = INADDR_ANY;  
  45.         else  
  46.             theIPAddrArray[arrIndex++] = INADDR_LOOPBACK; //INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127.0.0.1上面的连接请求  
  47.       
  48.         *outNumAddrsPtr = arrIndex;  
  49.     }  
  50.       
  51.     return theIPAddrArray;  
  52. }  

获取RTSP监听端口列表的方法比较简单,如下:
[cpp]  view plain  copy
 print ?
  1. UInt16* QTSServer::GetRTSPPorts(QTSServerPrefs* inPrefs, UInt32* outNumPortsPtr)  
  2. {  
  3.     *outNumPortsPtr = inPrefs->GetNumValues(qtssPrefsRTSPPorts); //获取qtssPrefsRTSPPorts对应的数组长度;  
  4.       
  5.     if (*outNumPortsPtr == 0)  
  6.         return NULL;  
  7.           
  8.     UInt16* thePortArray = NEW UInt16[*outNumPortsPtr];  
  9.     //获取监听端口号列表;  
  10.     for (UInt32 theIndex = 0; theIndex < *outNumPortsPtr; theIndex++)  
  11.     {  
  12.         // Get the ip addr out of the prefs dictionary  
  13.         UInt32 theLen = sizeof(UInt16);  
  14.         QTSS_Error theErr = QTSS_NoErr;  
  15.         theErr = inPrefs->GetValue(qtssPrefsRTSPPorts, theIndex, &thePortArray[theIndex], &theLen);  
  16.         Assert(theErr == QTSS_NoErr);     
  17.     }  
  18.       
  19.     return thePortArray;  
  20. }  

如此根据配置文件的IP、port,即已将对应的Socket建立完成,且startListeningNow默认是false,需要在Runserver中调用sServer->StartTasks();方能开始监听
[cpp]  view plain  copy
 print ?
  1. void QTSServer::StartTasks()  
  2. {  
  3.     fRTCPTask = new RTCPTask();  
  4.     fStatsTask = new RTPStatsUpdaterTask();  
  5.   
  6.     //  
  7.     // Start listening  
  8.     for (UInt32 x = 0; x < fNumListeners; x++)  
  9.         fListeners[x]->RequestEvent(EV_RE);  
  10. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值