live555学习笔记3

三 消息循环

看服端的主体:live555MediaServer.cpp中的main()函数,可见其创建一个RTSPServer类实例后,即进入一个函数env->taskScheduler().doEventLoop()中,看名字很明显是一个消息循坏,执行到里面后不停地转圈,生名不息,转圈不止。那么在这个人生的圈圈中如何实现RTSP服务和RTP传输呢?别想那么远了,还是先看这个圈圈中实现了什么功能吧。

  1. void BasicTaskScheduler0::doEventLoop(char* watchVariable) {  
  2.     // Repeatedly loop, handling readble sockets and timed events:  
  3.     while (1) {  
  4.         if (watchVariable != NULL && *watchVariable != 0)  
  5.             break;  
  6.         SingleStep();  
  7.     }  

void BasicTaskScheduler0::doEventLoop(char* watchVariable) { // Repeatedly loop, handling readble sockets and timed events: while (1) { if (watchVariable != NULL && *watchVariable != 0) break; SingleStep(); } }


BasicTaskScheduler0从TaskScheduler派生,所以还是一个任务调度对象,所以依然说明任务调度对象是整个程序的发动机。

循环中每次走一步:SingleStep()。这走一步中都做些什么呢?

总结为以下四步:

1为所有需要操作的socket执行select。

2找出第一个应执行的socket任务(handler)并执行之。

3找到第一个应响应的事件,并执行之。

4找到第一个应执行的延迟任务并执行之。

可见,每一步中只执行三个任务队列中的一项。下面详细分析函数SingleStep():

  1. //循坏中主要执行的函数  
  2. void BasicTaskScheduler::SingleStep(unsigned maxDelayTime) {  
  3.     fd_set readSet = fReadSet; // make a copy for this select() call  
  4.     fd_set writeSet = fWriteSet; // ditto  
  5.     fd_set exceptionSet = fExceptionSet; // ditto  
  6.   
  7.     //计算select socket们时的超时时间。  
  8.     DelayInterval const& timeToDelay = fDelayQueue.timeToNextAlarm();  
  9.     struct timeval tv_timeToDelay;  
  10.     tv_timeToDelay.tv_sec = timeToDelay.seconds();  
  11.     tv_timeToDelay.tv_usec = timeToDelay.useconds();  
  12.     // Very large "tv_sec" values cause select() to fail.  
  13.     // Don't make it any larger than 1 million seconds (11.5 days)  
  14.     const long MAX_TV_SEC = MILLION;  
  15.     if (tv_timeToDelay.tv_sec > MAX_TV_SEC) {  
  16.         tv_timeToDelay.tv_sec = MAX_TV_SEC;  
  17.     }  
  18.     // Also check our "maxDelayTime" parameter (if it's > 0):  
  19.     if (maxDelayTime > 0  
  20.             && (tv_timeToDelay.tv_sec > (long) maxDelayTime / MILLION  
  21.                     || (tv_timeToDelay.tv_sec == (long) maxDelayTime / MILLION  
  22.                             && tv_timeToDelay.tv_usec  
  23.                                     > (long) maxDelayTime % MILLION))) {  
  24.         tv_timeToDelay.tv_sec = maxDelayTime / MILLION;  
  25.         tv_timeToDelay.tv_usec = maxDelayTime % MILLION;  
  26.     }  
  27.   
  28.     //先执行socket的select操作,以确定哪些socket任务(handler)需要执行。  
  29.     int selectResult = select(fMaxNumSockets,  
  30.             &readSet, &writeSet,&exceptionSet,  
  31.             &tv_timeToDelay);  
  32.   
  33.     if (selectResult < 0) {  
  34. //#if defined(__WIN32__) || defined(_WIN32)  
  35.         int err = WSAGetLastError();  
  36.         // For some unknown reason, select() in Windoze sometimes fails with WSAEINVAL if  
  37.         // it was called with no entries set in "readSet".  If this happens, ignore it:  
  38.         if (err == WSAEINVAL && readSet.fd_count == 0) {  
  39.             err = EINTR;  
  40.             // To stop this from happening again, create a dummy socket:  
  41.             int dummySocketNum = socket(AF_INET, SOCK_DGRAM, 0);  
  42.             FD_SET((unsigned) dummySocketNum, &fReadSet);  
  43.         }  
  44.         if (err != EINTR) {  
  45. //#else  
  46. //      if (errno != EINTR && errno != EAGAIN) {  
  47. //#endif  
  48.             // Unexpected error - treat this as fatal:  
  49. //#if !defined(_WIN32_WCE)  
  50. //          perror("BasicTaskScheduler::SingleStep(): select() fails");  
  51. //#endif  
  52.             internalError();  
  53.         }  
  54.     }  
  55.   
  56.     // Call the handler function for one readable socket:  
  57.     HandlerIterator iter(*fHandlers);  
  58.     HandlerDescriptor* handler;  
  59.     // To ensure forward progress through the handlers, begin past the last  
  60.     // socket number that we handled:  
  61.     if (fLastHandledSocketNum >= 0) {  
  62.         //找到上次执行的socket handler的下一个  
  63.         while ((handler = iter.next()) != NULL) {  
  64.             if (handler->socketNum == fLastHandledSocketNum)  
  65.                 break;  
  66.         }  
  67.         if (handler == NULL) {  
  68.             fLastHandledSocketNum = -1;  
  69.             iter.reset(); // start from the beginning instead  
  70.         }  
  71.     }  
  72.   
  73.     //从找到的handler开始,找一个可以执行的handler,不论其状态是可读,可写,还是出错,执行之。  
  74.     while ((handler = iter.next()) != NULL) {  
  75.         int sock = handler->socketNum; // alias  
  76.         int resultConditionSet = 0;  
  77.         if (FD_ISSET(sock, &readSet)  
  78.                 && FD_ISSET(sock, &fReadSet)/*sanity check*/)  
  79.             resultConditionSet |= SOCKET_READABLE;  
  80.         if (FD_ISSET(sock, &writeSet)  
  81.                 && FD_ISSET(sock, &fWriteSet)/*sanity check*/)  
  82.             resultConditionSet |= SOCKET_WRITABLE;  
  83.         if (FD_ISSET(sock, &exceptionSet)  
  84.                 && FD_ISSET(sock, &fExceptionSet)/*sanity check*/)  
  85.             resultConditionSet |= SOCKET_EXCEPTION;  
  86.         if ((resultConditionSet & handler->conditionSet)  
  87.                 != 0 && handler->handlerProc != NULL) {  
  88.             fLastHandledSocketNum = sock;  
  89.             // Note: we set "fLastHandledSocketNum" before calling the handler,  
  90.             // in case the handler calls "doEventLoop()" reentrantly.  
  91.             (*handler->handlerProc)(handler->clientData, resultConditionSet);  
  92.             break;  
  93.         }  
  94.     }  
  95.   
  96.     //如果寻找完了依然没有执行任何handle,则从头再找。  
  97.     if (handler == NULL && fLastHandledSocketNum >= 0) {  
  98.         // We didn't call a handler, but we didn't get to check all of them,  
  99.         // so try again from the beginning:  
  100.         iter.reset();  
  101.         while ((handler = iter.next()) != NULL) {  
  102.             int sock = handler->socketNum; // alias  
  103.             int resultConditionSet = 0;  
  104.             if (FD_ISSET(sock, &readSet)&& FD_ISSET(sock, &fReadSet)/*sanity check*/)  
  105.                 resultConditionSet |= SOCKET_READABLE;  
  106.             if (FD_ISSET(sock, &writeSet)&& FD_ISSET(sock, &fWriteSet)/*sanity check*/)  
  107.                 resultConditionSet |= SOCKET_WRITABLE;  
  108.             if (FD_ISSET(sock, &exceptionSet)   && FD_ISSET(sock, &fExceptionSet)/*sanity check*/)  
  109.                 resultConditionSet |= SOCKET_EXCEPTION;  
  110.             if ((resultConditionSet & handler->conditionSet)  
  111.                     != 0 && handler->handlerProc != NULL) {  
  112.                 fLastHandledSocketNum = sock;  
  113.                 // Note: we set "fLastHandledSocketNum" before calling the handler,  
  114.                 // in case the handler calls "doEventLoop()" reentrantly.  
  115.                 (*handler->handlerProc)(handler->clientData, resultConditionSet);  
  116.                 break;  
  117.             }  
  118.         }  
  119.   
  120.         //依然没有找到可执行的handler。  
  121.         if (handler == NULL)  
  122.             fLastHandledSocketNum = -1; //because we didn't call a handler  
  123.     }  
  124.   
  125.     //响应事件  
  126.     // Also handle any newly-triggered event  
  127.     // (Note that we do this *after* calling a socket handler,  
  128.     // in case the triggered event handler modifies The set of readable sockets.)  
  129.     if (fTriggersAwaitingHandling != 0) {  
  130.         if (fTriggersAwaitingHandling == fLastUsedTriggerMask) {  
  131.             // Common-case optimization for a single event trigger:  
  132.             fTriggersAwaitingHandling = 0;  
  133.             if (fTriggeredEventHandlers[fLastUsedTriggerNum] != NULL) {  
  134.                 //执行一个事件处理函数  
  135.                 (*fTriggeredEventHandlers[fLastUsedTriggerNum])(fTriggeredEventClientDatas[fLastUsedTriggerNum]);  
  136.             }  
  137.         } else {  
  138.             // Look for an event trigger that needs handling  
  139.             // (making sure that we make forward progress through all possible triggers):  
  140.             unsigned i = fLastUsedTriggerNum;  
  141.             EventTriggerId mask = fLastUsedTriggerMask;  
  142.   
  143.             do {  
  144.                 i = (i + 1) % MAX_NUM_EVENT_TRIGGERS;  
  145.                 mask >>= 1;  
  146.                 if (mask == 0)  
  147.                     mask = 0x80000000;  
  148.   
  149.                 if ((fTriggersAwaitingHandling & mask) != 0) {  
  150.                     //执行一个事件响应  
  151.                     fTriggersAwaitingHandling &= ~mask;  
  152.                     if (fTriggeredEventHandlers[i] != NULL) {  
  153.                         (*fTriggeredEventHandlers[i])(fTriggeredEventClientDatas[i]);  
  154.                     }  
  155.   
  156.                     fLastUsedTriggerMask = mask;  
  157.                     fLastUsedTriggerNum = i;  
  158.                     break;  
  159.                 }  
  160.             } while (i != fLastUsedTriggerNum);  
  161.         }  
  162.     }  
  163.   
  164.     //执行一个最迫切的延迟任务。  
  165.     // Also handle any delayed event that may have come due.  
  166.     fDelayQueue.handleAlarm();  
  167. }  
//循坏中主要执行的函数 void BasicTaskScheduler::SingleStep(unsigned maxDelayTime) { fd_set readSet = fReadSet; // make a copy for this select() call fd_set writeSet = fWriteSet; // ditto fd_set exceptionSet = fExceptionSet; // ditto //计算select socket们时的超时时间。 DelayInterval const& timeToDelay = fDelayQueue.timeToNextAlarm(); struct timeval tv_timeToDelay; tv_timeToDelay.tv_sec = timeToDelay.seconds(); tv_timeToDelay.tv_usec = timeToDelay.useconds(); // Very large "tv_sec" values cause select() to fail. // Don't make it any larger than 1 million seconds (11.5 days) const long MAX_TV_SEC = MILLION; if (tv_timeToDelay.tv_sec > MAX_TV_SEC) { tv_timeToDelay.tv_sec = MAX_TV_SEC; } // Also check our "maxDelayTime" parameter (if it's > 0): if (maxDelayTime > 0 && (tv_timeToDelay.tv_sec > (long) maxDelayTime / MILLION || (tv_timeToDelay.tv_sec == (long) maxDelayTime / MILLION && tv_timeToDelay.tv_usec > (long) maxDelayTime % MILLION))) { tv_timeToDelay.tv_sec = maxDelayTime / MILLION; tv_timeToDelay.tv_usec = maxDelayTime % MILLION; } //先执行socket的select操作,以确定哪些socket任务(handler)需要执行。 int selectResult = select(fMaxNumSockets, &readSet, &writeSet,&exceptionSet, &tv_timeToDelay); if (selectResult < 0) { //#if defined(__WIN32__) || defined(_WIN32) int err = WSAGetLastError(); // For some unknown reason, select() in Windoze sometimes fails with WSAEINVAL if // it was called with no entries set in "readSet". If this happens, ignore it: if (err == WSAEINVAL && readSet.fd_count == 0) { err = EINTR; // To stop this from happening again, create a dummy socket: int dummySocketNum = socket(AF_INET, SOCK_DGRAM, 0); FD_SET((unsigned) dummySocketNum, &fReadSet); } if (err != EINTR) { //#else // if (errno != EINTR && errno != EAGAIN) { //#endif // Unexpected error - treat this as fatal: //#if !defined(_WIN32_WCE) // perror("BasicTaskScheduler::SingleStep(): select() fails"); //#endif internalError(); } } // Call the handler function for one readable socket: HandlerIterator iter(*fHandlers); HandlerDescriptor* handler; // To ensure forward progress through the handlers, begin past the last // socket number that we handled: if (fLastHandledSocketNum >= 0) { //找到上次执行的socket handler的下一个 while ((handler = iter.next()) != NULL) { if (handler->socketNum == fLastHandledSocketNum) break; } if (handler == NULL) { fLastHandledSocketNum = -1; iter.reset(); // start from the beginning instead } } //从找到的handler开始,找一个可以执行的handler,不论其状态是可读,可写,还是出错,执行之。 while ((handler = iter.next()) != NULL) { int sock = handler->socketNum; // alias int resultConditionSet = 0; if (FD_ISSET(sock, &readSet) && FD_ISSET(sock, &fReadSet)/*sanity check*/) resultConditionSet |= SOCKET_READABLE; if (FD_ISSET(sock, &writeSet) && FD_ISSET(sock, &fWriteSet)/*sanity check*/) resultConditionSet |= SOCKET_WRITABLE; if (FD_ISSET(sock, &exceptionSet) && FD_ISSET(sock, &fExceptionSet)/*sanity check*/) resultConditionSet |= SOCKET_EXCEPTION; if ((resultConditionSet & handler->conditionSet) != 0 && handler->handlerProc != NULL) { fLastHandledSocketNum = sock; // Note: we set "fLastHandledSocketNum" before calling the handler, // in case the handler calls "doEventLoop()" reentrantly. (*handler->handlerProc)(handler->clientData, resultConditionSet); break; } } //如果寻找完了依然没有执行任何handle,则从头再找。 if (handler == NULL && fLastHandledSocketNum >= 0) { // We didn't call a handler, but we didn't get to check all of them, // so try again from the beginning: iter.reset(); while ((handler = iter.next()) != NULL) { int sock = handler->socketNum; // alias int resultConditionSet = 0; if (FD_ISSET(sock, &readSet)&& FD_ISSET(sock, &fReadSet)/*sanity check*/) resultConditionSet |= SOCKET_READABLE; if (FD_ISSET(sock, &writeSet)&& FD_ISSET(sock, &fWriteSet)/*sanity check*/) resultConditionSet |= SOCKET_WRITABLE; if (FD_ISSET(sock, &exceptionSet) && FD_ISSET(sock, &fExceptionSet)/*sanity check*/) resultConditionSet |= SOCKET_EXCEPTION; if ((resultConditionSet & handler->conditionSet) != 0 && handler->handlerProc != NULL) { fLastHandledSocketNum = sock; // Note: we set "fLastHandledSocketNum" before calling the handler, // in case the handler calls "doEventLoop()" reentrantly. (*handler->handlerProc)(handler->clientData, resultConditionSet); break; } } //依然没有找到可执行的handler。 if (handler == NULL) fLastHandledSocketNum = -1; //because we didn't call a handler } //响应事件 // Also handle any newly-triggered event // (Note that we do this *after* calling a socket handler, // in case the triggered event handler modifies The set of readable sockets.) if (fTriggersAwaitingHandling != 0) { if (fTriggersAwaitingHandling == fLastUsedTriggerMask) { // Common-case optimization for a single event trigger: fTriggersAwaitingHandling = 0; if (fTriggeredEventHandlers[fLastUsedTriggerNum] != NULL) { //执行一个事件处理函数 (*fTriggeredEventHandlers[fLastUsedTriggerNum])(fTriggeredEventClientDatas[fLastUsedTriggerNum]); } } else { // Look for an event trigger that needs handling // (making sure that we make forward progress through all possible triggers): unsigned i = fLastUsedTriggerNum; EventTriggerId mask = fLastUsedTriggerMask; do { i = (i + 1) % MAX_NUM_EVENT_TRIGGERS; mask >>= 1; if (mask == 0) mask = 0x80000000; if ((fTriggersAwaitingHandling & mask) != 0) { //执行一个事件响应 fTriggersAwaitingHandling &= ~mask; if (fTriggeredEventHandlers[i] != NULL) { (*fTriggeredEventHandlers[i])(fTriggeredEventClientDatas[i]); } fLastUsedTriggerMask = mask; fLastUsedTriggerNum = i; break; } } while (i != fLastUsedTriggerNum); } } //执行一个最迫切的延迟任务。 // Also handle any delayed event that may have come due. fDelayQueue.handleAlarm(); }

你可能感兴趣的:(socket,null,任务调度,任务,optimization,triggers,网络)