金慶的專欄
C++博客
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
::
423 隨筆 :: 0 文章 :: 454 評論 :: 0 Trackbacks
公告
我的隨筆
我的評論
我參與的隨筆
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
(502)
1. C/C++(166)
(rss)
2. 網游開發(108)
(rss)
3. Golang(20)
(rss)
4. Linux/Unix(30)
(rss)
5. 軟工與管理(44)
(rss)
6. Python(23)
(rss)
7. Erlang(18)
(rss)
8. Rust(16)
(rss)
9. 其它(77)
(rss)
隨筆檔案
(423)
2023年1月 (1)
2022年11月 (1)
2022年10月 (2)
2022年9月 (1)
2022年4月 (6)
2022年1月 (2)
2021年12月 (4)
2021年11月 (6)
2021年10月 (2)
2021年9月 (2)
2021年8月 (7)
2021年7月 (2)
2021年5月 (2)
2021年3月 (1)
2021年2月 (2)
2021年1月 (1)
2020年12月 (1)
2020年10月 (1)
2020年9月 (5)
2020年8月 (1)
2020年7月 (1)
2020年6月 (1)
2020年4月 (2)
2020年3月 (3)
2020年2月 (3)
2020年1月 (1)
2019年12月 (1)
2019年9月 (2)
2019年4月 (2)
2019年1月 (1)
2018年12月 (1)
2018年11月 (3)
2018年10月 (1)
2018年9月 (3)
2018年8月 (3)
2018年7月 (2)
2018年6月 (4)
2018年5月 (4)
2018年4月 (4)
2018年3月 (1)
2018年1月 (2)
2017年12月 (2)
2017年11月 (3)
2017年10月 (3)
2017年8月 (7)
2017年7月 (1)
2017年6月 (1)
2017年5月 (3)
2017年4月 (3)
2017年3月 (3)
2017年2月 (2)
2017年1月 (2)
2016年12月 (5)
2016年11月 (2)
2016年10月 (2)
2016年9月 (1)
2016年8月 (6)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (2)
2016年3月 (2)
2016年1月 (3)
2015年12月 (2)
2015年11月 (2)
2015年10月 (1)
2015年8月 (2)
2015年7月 (1)
2015年6月 (1)
2015年5月 (4)
2015年4月 (3)
2015年3月 (4)
2015年2月 (5)
2015年1月 (4)
2014年12月 (3)
2014年11月 (3)
2014年10月 (2)
2014年9月 (3)
2014年8月 (1)
2014年4月 (4)
2014年3月 (1)
2014年2月 (4)
2014年1月 (5)
2013年12月 (5)
2013年11月 (5)
2013年9月 (2)
2013年8月 (2)
2013年7月 (2)
2013年6月 (2)
2013年5月 (1)
2013年1月 (2)
2012年12月 (1)
2012年11月 (1)
2012年9月 (1)
2012年8月 (3)
2012年7月 (2)
2012年6月 (1)
2012年4月 (3)
2012年3月 (2)
2012年2月 (3)
2012年1月 (2)
2011年11月 (2)
2011年10月 (3)
2011年9月 (2)
2011年8月 (2)
2011年7月 (3)
2011年6月 (2)
2011年5月 (3)
2011年1月 (2)
2010年12月 (1)
2010年11月 (2)
2010年10月 (2)
2010年9月 (3)
2010年8月 (2)
2010年7月 (3)
2010年6月 (1)
2010年5月 (3)
2010年4月 (3)
2010年3月 (5)
2010年2月 (4)
2010年1月 (4)
2009年12月 (2)
2009年11月 (3)
2009年10月 (4)
2009年9月 (3)
2009年8月 (2)
2009年7月 (4)
2009年6月 (1)
2009年5月 (3)
2009年4月 (4)
2009年3月 (2)
2009年2月 (5)
2009年1月 (1)
2008年12月 (7)
2008年11月 (4)
2008年10月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (3)
2008年6月 (4)
2008年5月 (6)
2008年4月 (7)
2008年3月 (6)
2008年1月 (5)
2007年12月 (7)
2007年11月 (4)
2007年10月 (5)
2007年9月 (6)
2007年8月 (8)
2007年7月 (5)
相冊
公告照片
搜索
積分與排名
積分 - 653959
排名 - 25
最新評論
1.?re: boost::asio::spawn 將一統C++網絡庫
asio 成為C++首選網絡庫
--linda
2.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--金慶
3.?re: mingw編譯OrzNet
能發送一個mingw編譯好的OrzNet庫給我嗎? liuweiqcxy@163.com
謝謝!
--劉威
4.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
5.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
閱讀排行榜
1.?"multiple definition of" 錯誤(11016)
2.?SVN中邪惡的replace(10938)
3.?VS2005編譯libevent(10404)
4.?混音算法的學習與研究(10183)
5.?C調用lua腳本的效率測試(9003)
評論排行榜
1.?VC6正在被拋棄(35)
2.?VS2005編譯libevent(21)
3.?"multiple definition of" 錯誤(18)
4.?C++引用優于指針(17)
5.?ACE與ASIO之間關于Socket編程的比較(16)
自動刪除的定時器隊列
自動刪除的定時器隊列
(金慶的專欄)
網游服務器中使用定時器隊列代替角色遍歷可以提高性能,同時簡代邏輯。
游戲主循環中不必遍歷角色,只需觸發定時器動作。
主循環代碼如:
RootTimerQueue & rTimerQueue = Singleton<RootTimerQueue>();
do
{
UpdateTimeNow(); // update m_rNow
bool bTimerActed = rTimerQueue.TryToTickOne(m_rNow);
size_t nCount = HandleMessages();
if (0 == nCount)
{
if (m_bStopped) break;
if (bTimerActed) continue;
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
}
} while (true);
定時器運行與主循環線程。
RootTimerQueue為根隊列,單件,各功能的定時器為RootTimerQueue的子隊列。
定時器隊列需要一個自動刪除功能。
如角色相關的一大堆定時器動作需要在角色退出時自動刪除。
不然定時器動作在角色不存在時或重新登錄后觸發,會造成錯誤。
如果采用角色臨時ID(會話ID), 每次登錄都不相同,
可以避免錯誤,定時器動作就成為一個空動作。
但是該動作一直存在于隊列中會占用內存,無法清除。
解決方案是每個角色擁有一個自己的定時器隊列,該隊列為根隊列的子隊列。
角色退出時,子隊列的刪除會自動刪除所有的定時器動作。
class TimerQueue : boost::noncopyable
{
public:
typedef boost::function<void ()> Action;
typedef UInt64 TimerId;
public:
TimerId Insert(time_t tStart, const Action & act, unsigned int nIntervalSec = 0);
void Erase(TiemrId id);
public:
void SetParent(TimerQueue * p);
public:
bool TryToTickOne(time_t tNow);
};
時間精度為秒,因為現在的應用只需秒即可,可以稍加改動替換成高精度時間。
定時器動作分為2類:
1. 只做一次,nIntervalSec為0表示只做一次。
2. 重復做,nIntervalSec非0.
添加定時器只有一個開始時間(有需要可以加個結束時間)。
開始時間可以是登錄前的一個時間點,這樣就可以計算離線期間所發生的改變。
通過SetParent()將自己設為某個隊列的子隊列。
實現部分如下:
class TimerQueue : boost::nonecopyable
{
private:
void TickOne();
void SetNextTime();
void EraseParentTimer();
private:
typedef UInt64 TimerSeq;
private:
struct TimerItem
{
time_t t;
TimerSeq seq;
Action act;
unsigned int nIntervalSec;
};
private:
void InsertItem(const TimerItem & itm);
TimerItem Pop();
private:
struct TimerOrder {};
typedef boost::multi_index::multi_index_container
<
TimerItem,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_unique<
boost::multi_index::tag<TimerOrder>,
boost::multi_index::composite_key<
TimerItem,
boost::multi_index::member<TimerItem, time_t, &TimerItem::t>,
boost::multi_index::member<IimerItem, TimerSeq, &TimerItem::seq>
> // composite_key
>, // ordered_unique
boost::multi_index::hashed_unique<
boost::multi_index::tag<TimerId>,
boost::multi_index::member<TimerItem, TimerSeq, &TimerItem::seq>
> // hashed_unique
> // indexed_by
> TimerItemSet;
typedef TimerItemSet::index<TimerOrder>::type QueueByTime;
typedef TimerItemSet::index<TimerId>::type QueueById;
private:
TimerItemSet m_set;
private:
TimerSeq m_seq;
time_t m_tNext;
TimerQueue * m_pParent;
TimerId m_idParentTimer; // Used to erase timer from parent
};
TimerItemSet以時間和序號排序,允許時間相同,但序號是唯一的。
并且也能用ID號索引,用于刪除定時器。
TimerQueue.cpp
TimerQueue::TimerQueue()
: m_seq(0) // 0 is illegal
, m_pParent(NULL)
, m_idParentTimer(0)
{
SetNextTime();
}
TimerQueue::~TimerQueue()
{
EraseParentTimer();
}
TimerQueue::TimerId TimerQueue::Insert(
time_t tStart, const Action & act, unsigned int nIntervalSec)
{
TimerItem itm = {tStart, ++m_seq, act, nIntervalSec};
InsertItem(itm);
return itm.seq;
}
TimerQueue::Erase(TimerId id)
{
SetById & rSet = mset.get<TimerId>();
SetById::iterator itr = rSet.find(id);
if (itr == rSet.end())
return;
time_t t = (*itr).t;
TimerSeq seqErase = (*itr).seq; // Used to set next time.
TimerSeq seqFront = m_set.get<TimerOrder>().begin()->seq;
rSet.erase(itr);
if (seqErase == seqFront)
SetNextTime();
}
void TimerQueue::SetParent(TimerQueue * p)
{
EraseParentTimer()
m_pParent = p
SetNextTime();
}
bool TimerQueue::TryToTickOne(time_t tNow)
{
if (tNow < m_tNext)
return false;
if (m_set.empty())
return false;
TickOne();
return true;
}
void TimerQueue::TickOne()
{
BOOST_ASSERT(!m_set.empty());
TimerItem itm = Pop();
itm.act();
if (0 == itm.nIntervalSec)
return;
itm.t += itm.nIntervalSec;
InsertItem(itm);
}
TimerQueue::TimerItem TimerQueue::Pop()
{
BOOST_ASSERT(!m_set.empty());
QueueByTime & rQ = m_set.get<TimerOrder>();
QueueByTime::iterator itr = rQ.begin();
TimerItem itm = *itr;
rQ.erase(itr);
BOOST_ASSERT(itm.t == m_tNext);
SetNextTime();
return itm;
}
void TimerQueue::SetNextTime()
{
EraseParentTimer();
if (m_set.empty())
{
m_tNext = std::numeric_limits<time_t>::max();
return;
}
time_t tNew = (*m_set.get<TimeOrder>().begin()).t;
m_tNext = tNew;
if (NULL == m_pParent)
return;
m_idParentTimer = m_pParent->Insert(m_tNext,
boost::bind(&TimerQueue::TickOne, this));
BOOST_ASSERT(m_idParentTimer); // 0 is illegal ID.
}
void TimerQueue::EraseParentTimer()
{
if (m_pParent && m_idParentTimer)
m_pParent->Erase(m_idParentTimer);
m_idParentTimer = 0;
}
void TimerQueue::InsertItem(const TimerIten & itm)
{
m_set.insert(itm);
if (itm.t < m_tNext)
SetNextTiem();
}
RootTimerQueue是TimerQueue的子類。
另外再派生子類
class LogicTimerQueue : public TimerQueue
{
public:
LogicTimerQueue();
};
LogicTimerQueue::LogicTimerQueue()
{
SetParent(&Singleton<RootTimerQueue>());
}
這樣就讓每個LogicTimerQueue成為根隊列的子隊列。
玩家類中就可以添加LogicTimerQueue成員,來執行定時動作。
class Player
{
public:
Player()
{
...
// Check mail every hour.
m_timer->Insert(time(NULL),
boost::bind(&Player::CheckMail, this),
3600);
...
}
...
private:
LogicTimerQueue m_timer;
...
};
posted on 2012-06-20 13:28
金慶
閱讀(1139)
評論(2)
編輯
收藏
引用
所屬分類:
1. C/C++
、
2. 網游開發
評論
#
re: 自動刪除的定時器隊列
2012-11-14 09:08
zozoiiiiii
class Player
{
Player(){注冊定時器}
~Player(){注銷定時器}
}
利用構造函數和析構函數來保證角色退出時定時器的注冊和注銷,不很簡單么,干嘛要那么復雜
回復
更多評論
#
re: 自動刪除的定時器隊列
2012-11-14 19:23
金慶
@zozoiiiiii
沒錯,是在~Player()中注銷的。上面的代碼是如何注銷。假設某個Player注冊了上千個定時器,需要在~Player()中調用上千次注銷方法嗎?
回復
更多評論
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
How are dtLinks created in NavMesh
C++ parameter passing rules
Naming Conventions for Accessors
Visual Studio 2019 Compiler Hangs
Fbx File Format Identifier
查找內存錯誤
std::thread 中的異常會丟失調用棧
用賦值代替 protobuf CopyFrom()
vs2017 linux 編譯輸出改成 vs 格式
為 LiteIDE 添加選中標記
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 金慶
中文字幕无码精品亚洲资源网久久
|
色综合久久无码中文字幕
|
亚洲AV无码久久精品狠狠爱浪潮
|
中文字幕亚洲综合久久
|
99精品久久精品
|
97久久综合精品久久久综合
|
久久人人爽人人人人爽AV
|
久久午夜免费视频
|
久久精品国产2020
|
亚洲香蕉网久久综合影视
|
一级a性色生活片久久无少妇一级婬片免费放
|
影音先锋女人AV鲁色资源网久久
|
人妻精品久久久久中文字幕
|
欧美午夜A∨大片久久
|
久久影院午夜理论片无码
|
一极黄色视频久久网站
|
一本一本久久A久久综合精品
|
亚洲精品无码久久久久sm
|
久久精品国产亚洲一区二区
|
999久久久免费国产精品播放
|
久久久久久国产精品免费免费
|
久久精品综合网
|
国产综合久久久久
|
国产精品99久久久久久www
|
久久精品成人免费国产片小草
|
四虎国产精品成人免费久久
|
国产精品无码久久久久久
|
国产成人久久777777
|
久久亚洲AV无码精品色午夜
|
久久精品中文字幕有码
|
久久精品久久久久观看99水蜜桃
|
欧洲人妻丰满av无码久久不卡
|
99久久精品免费观看国产
|
久久久久久久久久久久久久
|
999久久久无码国产精品
|
国产精品VIDEOSSEX久久发布
|
久久SE精品一区二区
|
久久精品亚洲乱码伦伦中文
|
亚洲va久久久噜噜噜久久
|
久久久久久噜噜精品免费直播
|
精品久久久久久亚洲精品
|