• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            簡析LIVE555中的延時隊列

            轉載自:http://blog.sina.com.cn/s/blog_77c6324101018j1k.html

            最近在看LIVE555的源碼,對其中的延時隊列感覺有點亂,網上查詢資料,于是就總結一下。

            首先描述一下LIVE555中的延時隊列的設計理念。如下圖,A,B,C分別為時間軸上的三個事件點,而head表示當前時間點。

            要描述一個事件發生的時間,通常可以有兩種方法:一種方法直接描述事件發生的絕對時間;另一種方法則是可以描述和另一事件發生的相對時間。而LIVE555中采用的就是后者。  

            LIVE555中,首先將所有的事件點以發生時間的先后進行排序,然后每個事件對應的時間都是相對于前一事件發生的時間差。比如B事件中存儲的時間就是A事件觸發后,再去觸發B事件所需要的時間。這樣,我們每次去查詢這個隊列中是否有事件被觸發的時候,就只需要查詢整個隊列中的第一個事件就可以了。

             然后就是LIVE555中的實現方法了。整個延時隊列是用DelayQueue這個類實現的,而它的基類DelayQueueEntry就是用來描述每個事件節點的。在DelayQueueEntry中的主要成員有以下幾個:fDelayTimeRemaining表示的就是與前一事件之間的時間差;fNextfPrev就是指向時間軸上的下一個事件和前一個事件的指針;ftoken表示當前節點的標識;handleTimeout就是事件超時后的處理方法。

            DelayQueue類里描述的則是具體的實現方法。首先是一些對這個隊列進行的基本操作:addEntry實現的是在隊列中增加事件節點removeEntry實現的是在隊列中刪除某事件節點;updateEntry實現的則是更新某事件的觸發時間;而findEntryByToken則是根據節點的標識查找相應的事件。在此類中最常用的方法應該是synchronize,它實現的就是將整個事件隊列和當前系統時間進行同步,檢測有無事件已經被觸發,如果觸發并調用handleAlarm方法對相應事件進行處理。而屬性fLastSyncTime表示的就是上次同步的系統時間,其實一般情況下,方法synchronize的實現方法其實就是簡單地把隊列上第一個事件節點存儲的時間差減去當前系統時間和上次同步時間的差。

            附:相關類結構:

            =================================================================

            ==> 相關typedef定義

            typedef void TaskFunc(void* clientData);

            typedef void* TaskToken;

            // 下面Timeval類有涉及

            #ifdef TIME_BASE

            typedef TIME_BASE time_base_seconds;

            #else

            typedef long time_base_seconds;

            #endif

            ==> 相關類的說明(由于有些類很大,故不會完整貼出,故用說明)

            ///// A "Timeval" can be either an absolute time, or a time interval /////

            class Timeval {

            public:

              time_base_seconds seconds() const {

                return fTv.tv_sec;

              }

              time_base_seconds seconds() {

                return fTv.tv_sec;

              }

              time_base_seconds useconds() const {

            return fTv.tv_usec;

              }

            int operator>=(Timeval const& arg2) const; // >=為基礎,推算出其余條件判斷(<=<</span>、>等)的真假

              int operator<=(Timeval const& arg2) const {

                return arg2 >= *this;

              }

              int operator<</b>(Timeval const& arg2) const {

                return !(*this >= arg2);

              } 

            int operator>(Timeval const& arg2) const {

                return arg2 < *this;

              }

              int operator==(Timeval const& arg2) const {

                return *this >= arg2 && arg2 >= *this;

              }

              int operator!=(Timeval const& arg2) const {

                return !(*this == arg2);

              }

              void operator+=(class DelayInterval const& arg2);

              void operator-=(class DelayInterval const& arg2);

              // returns ZERO iff arg2 >= arg1

            protected:

              Timeval_r(time_base_seconds seconds, time_base_seconds useconds) {

                fTv.tv_sec = seconds; fTv.tv_usec = useconds;

              }

            private:

              time_base_seconds& secs() {

                return (time_base_seconds&)fTv.tv_sec;

              }

              time_base_seconds& usecs() {

                return (time_base_seconds&)fTv.tv_usec;

              }

              struct timeval fTv; // 看到,所有的所有,其實是在為timeval這個結構體封裝了一系列操作函數

            };

            ++++++++++++++++++++++++++++++++++++++++++

            // 下面這個類用以處理自197011日以來的絕對時間

            class EventTime: public Timeval {

            public:

              EventTime(unsigned secondsSinceEpoch = 0,

                unsigned usecondsSinceEpoch = 0)

                // We use the Unix standard epoch: January 1, 1970

                : Timeval_r(secondsSinceEpoch, usecondsSinceEpoch) {}

            };

            class DelayQueueEntry { // 通過它來鏈接所有的事件信息,組成隊列(見下面DelayQueue類)

            public:

              virtual ~DelayQueueEntry();

              intptr_t token() {

                return fToken;

              }

            protected: // abstract base class

              DelayQueueEntry(DelayInterval delay);

              virtual void handleTimeout();

            private:

              friend class DelayQueue;

              DelayQueueEntry* fNext;

              DelayQueueEntry* fPrev;

              DelayInterval fDeltaTimeRemaining;

              intptr_t fToken;

              static intptr_t tokenCounter;

            };

            class DelayQueue: public DelayQueueEntry {

            public:

              DelayQueue();

              virtual ~DelayQueue();

              void addEntry(DelayQueueEntry* newEntry); // returns a token for the entry

              void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);

              void updateEntry(intptr_t tokenToFind, DelayInterval newDelay);

              void removeEntry(DelayQueueEntry* entry); // but doesn't delete it

              DelayQueueEntry* removeEntry(intptr_t tokenToFind); // but doesn't delete it

              DelayInterval const& timeToNextAlarm();

              void handleAlarm();

            private:

              DelayQueueEntry* head() { return fNext; } // 返回DelayQueueEntry類中的fNext隊頭成員

              DelayQueueEntry* findEntryByToken(intptr_t token);

              void synchronize(); // bring the 'time remaining' fields up-to-date

              EventTime fLastSyncTime;

            };

            ////////// A subclass of DelayQueueEntry,

            //////////     used to implement BasicTaskScheduler0::scheduleDelayedTask()

            class AlarmHandler: public DelayQueueEntry {

            public:

              AlarmHandler(TaskFunc* proc, void* clientData, DelayInterval timeToDelay)

                : DelayQueueEntry(timeToDelay), fProc(proc), fClientData(clientData) {

              }

            private: // redefined virtual functions

              virtual void handleTimeout() {

                (*fProc)(fClientData);

                DelayQueueEntry::handleTimeout();

              }

            private:

              TaskFunc* fProc;

              void* fClientData;

            };

            posted on 2013-09-10 04:14 楊粼波 閱讀(1433) 評論(0)  編輯 收藏 引用

            99久久婷婷国产一区二区| 久久国产精品无码网站| 嫩草伊人久久精品少妇AV| 久久99精品国产一区二区三区| 成人亚洲欧美久久久久| 中文字幕久久精品 | 久久精品无码一区二区日韩AV| 色欲综合久久躁天天躁| 久久精品aⅴ无码中文字字幕重口| 亚洲国产精品久久久久| 精品伊人久久大线蕉色首页| 日本久久久久久中文字幕| 亚洲精品国产美女久久久| 国产亚州精品女人久久久久久| 伊人久久大香线焦AV综合影院 | 久久久国产精品网站| 久久影院久久香蕉国产线看观看| 中文字幕日本人妻久久久免费| 久久成人影院精品777| 久久精品一区二区三区AV| 国产精品亚洲综合专区片高清久久久| 一本色道久久综合狠狠躁| 久久精品国产精品亚洲艾草网美妙| 久久久久国产精品熟女影院| 亚洲国产香蕉人人爽成AV片久久| 精品国产乱码久久久久久1区2区| 亚洲欧洲久久av| 国产亚州精品女人久久久久久 | 久久综合给合久久狠狠狠97色69 | 中文字幕久久波多野结衣av| 久久亚洲2019中文字幕| 中文字幕一区二区三区久久网站| 蜜臀av性久久久久蜜臀aⅴ| 久久综合九色综合网站| 伊人精品久久久久7777| 色综合久久88色综合天天 | 激情久久久久久久久久| 国産精品久久久久久久| 精品国产乱码久久久久久浪潮| 品成人欧美大片久久国产欧美| 亚洲欧美精品伊人久久|